忙于处理各种工作内外事情,没有什么可以写的,所以强行写一篇更新。

忙于处理各种工作内外事情,没有什么可以写的,所以强行写一篇更新。
https://leetcode.com/problems/top-k-frequent-elements/description/
hash map + heap
Time complicity: O(N + logk)
struct KvPair { int key = 0; int val = 0; }; class Solution { public: vector<int> topKFrequent(vector<int>& nums, int k) { unordered_map<int, int> counts; for (int num : nums) { counts[num]++; } auto comp = [](const KvPair& lhs, const KvPair& rhs){ return lhs.val > rhs.val; }; priority_queue<KvPair, vector<KvPair>, decltype(comp)> pri_q(comp); for (auto& it : counts) { pri_q.push({it.first, it.second}); if (pri_q.size() > k) pri_q.pop(); } vector<int> results; while (!pri_q.empty()) { results.push_back(pri_q.top().key); pri_q.pop(); } return results; } };
https://leetcode.com/problems/balanced-binary-tree/description/
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode* root, int currDepth) { int left_depth = (root->left == nullptr) ? currDepth : maxDepth(root->left, currDepth + 1); int right_depth = (root->right == nullptr) ? currDepth : maxDepth(root->right, currDepth + 1); if (left_depth == -1 || right_depth == -1) return -1; if (left_depth - right_depth > 1 || right_depth - left_depth > 1) return -1; return max(left_depth, right_depth); } bool isBalanced(TreeNode* root) { if (root == nullptr) return true; if (maxDepth(root, 0) == -1) return false; return true; } };
https://leetcode.com/problems/longest-consecutive-sequence/description/
class Solution { public: int longestConsecutive(vector<int>& nums) { unordered_set<int> all_nums; unordered_map<int, int> unions_rev; // <end_idx, start_idx> unordered_map<int, int> unions; // <start_idx, end_idx> int max_length = 0; for (int num : nums) { auto it_r_union = unions_rev.find(num - 1); auto it_union = unions.find(num + 1); int start_idx = num; int end_idx = num; if (all_nums.find(num) != all_nums.end()) continue; if (it_union != unions.end() && it_r_union != unions_rev.end()) { // [RRR] num [SSS] start_idx = it_r_union->second; end_idx = it_union->second; unions_rev.erase(it_r_union); unions.erase(start_idx); unions.erase(it_union); unions_rev.erase(end_idx); } else if (it_union != unions.end()) { // num [SSS] end_idx = it_union->second; unions.erase(it_union); unions_rev.erase(end_idx); } else if (it_r_union != unions_rev.end()) { // [RRR] num start_idx = it_r_union->second; unions_rev.erase(it_r_union); unions.erase(start_idx); } unions[start_idx] = end_idx; unions_rev[end_idx] = start_idx; if (end_idx - start_idx + 1 > max_length) { max_length = end_idx - start_idx + 1; } all_nums.insert(num); } return max_length; } };
在尝试各种SS优化之后,我发现唯一对我服务器有用的是Google的BBR——一个TCP拥塞控制算法。
具体的一些评价,可以参考知乎的问题《Linux Kernel 4.9 中的 BBR 算法与之前的 TCP 拥塞控制相比有什么优势?》
Ubuntu下的简单部署:
uname -r
如果返回的是>=4.9的版本,那么直接跳到第4步
wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13/linux-headers-4.13.0-041300_4.13.0-041300.201709031731_all.deb wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13/linux-headers-4.13.0-041300-generic_4.13.0-041300.201709031731_amd64.deb wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13/linux-image-4.13.0-041300-generic_4.13.0-041300.201709031731_amd64.deb
dpkg -i linux-headers-*.deb dpkg -i linux-image-*.deb
然后删除系统里原来的内核,首先确认一下删除的版本,运行这个命令找到旧版本的内核
dpkg -l | grep linux-image
删掉
apt-get remove <旧内核映像文件> --purge
别忘了更新grub,不然引导不来了
update-grub reboot #重启
net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr
然后键入sysctl -p 令配置生效
除非你能忽视周遭环境,做你承诺过要做的事,否则你就会永远受到这个世界的控制。
——《肠子》恰克·帕拉尼克
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
边界条件:比如被除数、除数是0,另外结果的正负号要注意。
除法可以用减法实现,不过如果干减的话会很费时间,所以要想想怎么加速。
容易知道,一个数左移一位相当于乘以2。我们可以一次次把除数乘以2试探一下,到底最多能减掉 2^n*除数 多少次,这就相当于做了2^n次除法。由于试探的时候一次次做了乘方,所以试探的次数是log级别的,比原来一个个减要快多了。
class Solution { public: int divide(int dividend, int divisor) { if (dividend == 0) return 0; if (divisor == 0) return INT_MAX; bool posResult = true; if ((dividend > 0 && divisor < 0) || (dividend < 0 && divisor >0)) posResult = false; long long lDividend = dividend; if (lDividend < 0) lDividend = ~(lDividend-1); long long lDivisor = divisor; if (lDivisor < 0) lDivisor = ~(lDivisor-1); long long currDivisor = lDivisor; long long accRate = 1; long long result = 0; while (lDividend >= lDivisor) { while ((currDivisor << 1) < lDividend) { currDivisor <<= 1; accRate <<= 1; } while (currDivisor > lDividend) { currDivisor >>= 1; accRate >>= 1; } lDividend -= currDivisor; result += accRate; } if (posResult) { if (result > INT_MAX) { return INT_MAX; } else { return (int)result; } } else { result = -result; if (result < INT_MIN) return INT_MIN; else return (int)result; } } };
SRTP学分认定办法(附附件表格) (3)
测试附件上传