[Code Random Records] Brushing Questions Day36

1. No overlapping interval

435. Non-overlapping intervals

First sort from small to large. In fact, this question is still to find the common area, but the question requires us to delete as few intervals as possible. Therefore, what we need to delete must be areas with a large span and other common intervals. So every time the right range is updated, the smallest range needs to be considered.

1.if(intervals[i][0]<end), indicating that there are repeated intervals, then we need ret++, and update the smaller right range end=(intervals[i][1]>end)?end:intervals [i][1];

2.if(intervals[i][0]>=end), indicating that it is a different interval, then we can update the range on the right end=intervals[i][1];

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if(intervals.size()==0)
            return 0;
        sort(intervals.begin(),intervals.end());
        int ret = 0;
        int end = intervals[0][1];
        for(int i=1;i<intervals.size();i++)
        {
            if(intervals[i][0]<end)
            {
                ret++;
                end=(intervals[i][1]>end)?end:intervals[i][1];
            }
            else
                end=intervals[i][1];
        }
        return ret;
    }
};

2. Divide letter intervals

763. Divide Alphabet Ranges

1. First of all, we must record the last position of each English letter that has appeared. Since English letters appear repeatedly, a hash table is used for deduplication processing to traverse the entire string, and um[s[i]]=i is to record the last position of each character.

2. Set up begin and end to record the current left and right ranges.

3. Traverse the string once, if i>=end&&end<um.find(s[i])->second+1, it means that it is a new character at this time, then update begin and end: begin=end, begin is above The rightmost one time; end=um.find(s[i])->second+1 is the position corresponding to the last occurrence of the current character. ret.push_back(end-begin), but it should be noted that the push element at this time is only how many letters are in the maximum range of the current letter at the current position, which is not required by the real question

4.if(i<end), it means that the current letter is still within a range, but the last position of this letter needs to be compared with end, because the end at this time is the last position of the last letter, not the entire non- For repeated characters, then end=(end>um.find(s[i])->second+1)?end:um.find(s[i])->second+1; is to update the current rightmost range, So whether it is updated or not, we will update the last element of ret, that is, ret[ret.size()-1]=end-begin; can meet the current adjustment requirements

class Solution {
public:
    vector<int> partitionLabels(string s) {
        vector<int> ret;
        unordered_map<char,int> um;
        int begin = 0;
        int end = 0;
        for(int i=0;i<s.size();i++)
            um[s[i]]=i;
        for(int i=0;i<s.size();i++)
        {
            if(i<end)
            {
                end=(end>um.find(s[i])->second+1)?end:um.find(s[i])->second+1;
                ret[ret.size()-1]=end-begin;
            }
            else if(end<um.find(s[i])->second+1)
            {
                begin=end;
                end=um.find(s[i])->second+1;
                ret.push_back(end-begin);
            } 
        }
        return ret;
    }
};

3. Combine intervals

56. Combine intervals

1. Sort to get the range of order

2.if(i==0), ret.push_back(intervals[i]); If i==0, then directly push the first range to ret at this time

3. In other cases, there are only two possibilities:

if(ret[ret.size()-1][1]>=intervals[i][0]): Indicates that there is a repeated range at this time, then int end = (ret[ret.size()-1][ 1]>intervals[i][1])?ret[ret.size()-1][1]:intervals[i][1]; Determine the rightmost range, since we will modify the last ret, Then first keep the current left range int begin = ret[ret.size()-1][0]; unchanged, modify the current last retret.pop_back(); update the current current range at this time ret.push_back( {begin, end});

else: It means that there is no public area at this time, so you need to open up a new space and then let the later judge whether there is any public space. The idea at this time is the same as i==0, ret.push_back(intervals[i]);

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ret;
        sort(intervals.begin(),intervals.end());
        for(int i=0;i<intervals.size();i++)
        {
            if(i==0)
                ret.push_back(intervals[i]);
            else
            {
                if(ret[ret.size()-1][1]>=intervals[i][0])
                {
                    int end = (ret[ret.size()-1][1]>intervals[i][1])?ret[ret.size()-1][1]:intervals[i][1];
                    int begin = ret[ret.size()-1][0];
                    ret.pop_back();
                    ret.push_back({begin,end});
                }
                else
                    ret.push_back(intervals[i]);
            }
        }
        return ret;
    }
};

Guess you like

Origin blog.csdn.net/m0_63488627/article/details/130840075