2021/8/21

极客时间 3讲;内容较多 消化吃力

笔试一场

两个leetcode困难题目

2场面试

[//]: # (支持粘贴图片啦🎉🎉🎉)
[//]: # (保存的笔记可以在小程序中查看)

```
class Solution {
public:
    int dp[100100];
    int sum[100100];int a[100100],b[100100];
    void merge(vector<int>& nums,int l,int r,int mid){
        int top = 0;
        int l1 = mid + 1,s = l;
        while(true){
            if(l <= mid && l1 <= r){
                if(nums[l] <= nums[l1]){
                    dp[sum[l]] += l1 - (mid + 1);
                    a[++ top] = nums[l];
                    b[top] = sum[l];
                    ++ l;
                } else if(nums[l] > nums[l1]){
                   // dp[sum[l1]] += l - s;
                    a[++ top] = nums[l1];
                    b[top] = sum[l1];
                    ++ l1;
                } 
            } else if(l <= mid){
                dp[sum[l]] += l1 - (mid + 1);
                a[++ top] = nums[l];
                b[top] = sum[l];
                 ++ l;
            } else if(l1 <= r){
                a[++ top] = nums[l1];
                b[top] = sum[l1];
                 ++ l1;
            } else break;
        } 
        for(int i = s; i <= r; i ++){
            nums[i] = a[i - s + 1];
            sum[i] = b[i - s + 1];
        }
    }
    void dfs(vector<int>& nums,int l,int r){
        if(l == r){
            return ;
        } 
        if(l + 1 == r){
            merge(nums,l,r,l);
            return ;
        }
        int mid = (l + r) >> 1;
        dfs(nums,l,mid);
        dfs(nums,mid + 1,r);
        merge(nums,l,r,mid);
        return ;
    }
    vector<int> countSmaller(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i ++) sum[i] = i;
        dfs(nums,0,nums.size() - 1);
        vector<int> ans;
        for(int i = 0; i < nums.size(); i ++) ans.push_back(dp[i]);
        return ans;
    }
};

[//]: # (支持粘贴图片啦🎉🎉🎉)
[//]: # (保存的笔记可以在小程序中查看)

```
class Solution {
public:
    int dp[5010];
    int maxEnvelopes(vector<vector<int>>& envelopes) {
        
        sort(envelopes.begin(), envelopes.end(), [](const auto& e1, const auto& e2) {
            return e1[0] < e2[0] || (e1[0] == e2[0] && e1[1] > e2[1]);
        });
        int top = 0;
        for(int i = 0; i < envelopes.size(); i ++){
           if(top == 0 || dp[top] < envelopes[i][1]){
               dp[++ top] = envelopes[i][1];
           } else {
               int id = lower_bound(dp + 1,dp + top + 1,envelopes[i][1]) - dp;
               dp[id] = envelopes[i][1];
           }
          
        }

        return top;
    }
};
```
```

  

 

 

 

posted @ 2021-08-22 22:03  风生  阅读(33)  评论(0编辑  收藏  举报