wisdompeak / LeetCode

This repository contains the solutions and explanations to the algorithm problems on LeetCode. Only medium or above are included. All are written in C++/Python and implemented by myself. The problems attempted multiple times are labelled with hyperlinks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

354 题测试数据加强了

Anudorannador opened this issue · comments

354 题,Russian Doll Envelopes 的数据貌加强了,$1 \leqslant \text{envelopes.length} \leqslant 10^5$ ,$O(N^2)$ 的算法过不了了,我直接把老师你的代码(v1版本)复制粘贴上去直接 TLE 了。(我自己之前提交了AC过的 $O(N^2)$ 的代码现在也 TLE 了。

@Anudorannador

This should work:

`
class Solution {
public:
static bool comp(vector& A, vector& B){

    if(A[0] == B[0]){
        return A[1] > B[1];
    }
    return A[0] < B[0];
}

static bool comp1(const vector<int>& A, const vector<int>& B){
    return A[1] < B[1];
}

int maxEnvelopes(vector<vector<int>>& nums) {
    int n = nums.size();
    sort(begin(nums), end(nums), comp);
    vector<vector<int>> ans;
    ans.push_back(nums[0]);
    for(int i = 1; i < n; i++){
        if(nums[i][0] > ans.back()[0] and nums[i][1] > ans.back()[1]){
            ans.push_back(nums[i]);
        }else{
            int idx = lower_bound(begin(ans), end(ans), nums[i], comp1) - ans.begin();
            ans[idx] = nums[i];
        }
    }
    for(auto x: ans){
        cout << x[0] << " " << x[1] << "\n";
    }
    return ans.size();
}

};

`

这个库里贴的代码不一定都是AC的,只是用来说明时间复杂度的影响。