T181-T185 greedy algorithm

T181: Wiggle Subsequence

1. Dynamic planning
Establish two dp arrays. p,q.p[i],q[i] represents the maximum length of subsequence whose difference between the last two bits is positive or negative when it reaches the i-th bit. Traverse from j to I, update P [i], Q [i]

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        if(nums.empty()) return 0;
        vector<int> p(nums.size(),1); 
        vector<int> q(nums.size(),1); 
        for(int i=1;i<nums.size();i++){
            for(int j=0;j<i;j++){
                if(nums[i]>nums[j]) p[i]=max(p[i],q[j]+1);
                else if(nums[i]<nums[j]) q[i]=max(q[i],p[j]+1);
            }
        }
        return max(q.back(),p.back());
    }
};

2. Greedy algorithm
Maintain two quantities p,q, respectively, indicating that the tailing is positive and the tailing is negative. Traverse. If the current bit is larger than the previous bit, then p=q+1; otherwise, q=p+1. Return the smaller of p,q and n

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int p=1,q=1,n=nums.size();
        for(int i=1;i<n;i++){
            if(nums[i]>nums[i-1]) p=q+1;
            else if(nums[i]<nums[i-1]) q=p+1;
        }
        return min(n,max(p,q));
    }
};

T182: is subsequence is subsequence

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int i=0;
        for(int j=0;j<t.size();j++){
            if(s[i]==t[j])i++;
        }
        return i==s.size();
    }
};

T183:

Use string to construct incremental monotone stack. When the front position is smaller than the top element of the stack, pop up the top of the stack until k is 0 or the stack is empty or the current value is larger than the top of the stack. Put the current value into the stack. At this time, k may not be 0,res length must be greater than keep,resize removes the tail. Traverse the final result, and remove 0 of the head

class Solution {
public:
    string removeKdigits(string num, int k) {
        string res;
        int n=num.size(),keep=n-k;
        for(char c:num){
            while(k&&res.size()&&c<res.back()){
                res.pop_back();k--;
            }
            res.push_back(c);
        }
        res.resize(keep);
        while(!res.empty()&&res[0]=='0') res.erase(res.begin());
        return res.empty()?"0":res;
    }
};

Do not remove the 0 of the header at last, judge when adding, if the current bit is 0 but res is empty, do not enter the stack. After traversing, because some 0 may not be added, even if k is not 0,res length is less than keep, so it cannot resize, but pop up from the tail until k is 0

class Solution {
public:
    string removeKdigits(string num, int k) {
        string res;
        int n=num.size(),keep=n-k;
        for(char c:num){
            while(k&&res.size()&&c<res.back()){
                res.pop_back();k--;
            }
            if(c!='0'||res.size()) res.push_back(c);
        }
        while(res.size()&&k--) res.pop_back();
        
        return res.empty()?"0":res;
    }
};

T184: Queue Reconstruction by Height

Determine the relative position from big to small. Sort the height from big to small and the same height times from small to large, determine the relative position between them, and insert the small one into the result list from the head

class Solution {
public:
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(),people.end(),[](vector<int> &a,vector<int> &b){
            return a[0]>b[0]||(a[0]==b[0]&&a[1]<b[1]);
        });
        vector<vector<int>> res;
        for(auto a:people){
            res.insert(res.begin()+a[1],a);
        }
        return res;
    }
};

T185:

First, the overlapping interval is found according to the sequence of interval head, that is, the end of the previous interval is larger than the head of the current interval. At this time, res plus one. In order to determine which interval to delete, in order to ensure the minimum interval to be removed as a whole, the interval with larger end value is removed

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        sort(intervals.begin(),intervals.end());
        int res=0,n=intervals.size(),last=0;
        for(int i=1;i<n;i++){
            if(intervals[i][0]<intervals[last][1]){
                res++;
                if(intervals[i][1]<intervals[last][1]) last=i;
            }else last=i;
        }
        return res;
    }
};
class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if(intervals.empty()) return 0;
        sort(intervals.begin(),intervals.end());
        int res=0,n=intervals.size(),lastend=intervals[0][1];
        for(int i=1;i<n;i++){
            int t=lastend>intervals[i][0]?1:0;
            res+=t;
            lastend=t==1?min(lastend,intervals[i][1]):intervals[i][1];
        }
        return res;
    }
};
Published 100 original articles, won praise 9, visited 20000+
Private letter follow

Tags: less

Posted on Tue, 17 Mar 2020 00:40:36 -0400 by kushaljutta