Categories
未分类

LeetCode 347. Top K Frequent Elements

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;
    }
};

 

Categories
未分类

LeetCode 110. Balanced Binary Tree

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;
    }
};

 

Categories
未分类

LeetCode 128. Longest Consecutive Sequence

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;
    }
};

 

Categories
未分类

Boost up your ShadowSocks server with BBR

在尝试各种SS优化之后,我发现唯一对我服务器有用的是Google的BBR——一个TCP拥塞控制算法。
具体的一些评价,可以参考知乎的问题《Linux Kernel 4.9 中的 BBR 算法与之前的 TCP 拥塞控制相比有什么优势?
Ubuntu下的简单部署:

  1. 确认一下你的内核版本
    uname -r

    如果返回的是>=4.9的版本,那么直接跳到第4

  2. 下载内核安装包。最新版的内核可以去http://kernel.ubuntu.com/~kernel-ppa/mainline/ 查看,这边下载的是4.13版
    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
  3. 安装新的内核(确保你有sudo权限)
    dpkg -i linux-headers-*.deb
    dpkg -i linux-image-*.deb

    然后删除系统里原来的内核,首先确认一下删除的版本,运行这个命令找到旧版本的内核

    dpkg -l | grep linux-image

    删掉

    apt-get remove <旧内核映像文件> --purge

    别忘了更新grub,不然引导不来了

    update-grub
    reboot #重启
  4. 配置sysctl启用BBR
    编辑/etc/sysctl.conf , 在文件末尾加上下面两行

    net.core.default_qdisc=fq
    net.ipv4.tcp_congestion_control=bbr

    然后键入sysctl -p 令配置生效

 

 
参考:

  1. https://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next.git/commit/?id=0f8782ea14974ce992618b55f0c041ef43ed0b78
  2. https://www.zxavier.com/shadowsocks%E4%BC%98%E5%8C%96.html
Categories
未分类

人生哲理

除非你能忽视周遭环境,做你承诺过要做的事,否则你就会永远受到这个世界的控制。
——《肠子》恰克·帕拉尼克

Categories
未分类

LeetCode 29. Divide Two Integers

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;
		}
	}
};

 
 
 

Categories
不学无术 未分类

SRTP学分认定办法

SRTP学分认定办法(附附件表格) (3)
 
测试附件上传