【Let's Button Scrubbing Questions | Day 23】

Table of contents

Foreword:

56. Merge Intervals - LeetCode

738. Monotonically Increasing Numbers - LeetCode

Summarize:


Foreword:

        Today we still beat the greedy algorithm .

56. Merge Intervals - LeetCode

The array intervals represents a collection of several intervals, and a single interval is intervals[i] = [starti, endi]. Please merge all overlapping intervals and return an array of non-overlapping intervals that exactly covers all intervals in the input.

 Problem-solving ideas: In fact, this problem is very similar to the minimum bow and arrow shooting balloons and non-overlapping intervals we brushed before. In essence, the intervals are sorted first, and then if the right interval of the previous interval is greater than or equal to the next interval , then we overlap the two intervals, and the new right interval takes the maximum value of the two right intervals.

The big framework is to judge the entire array. If it overlaps, merge it, and then judge whether it overlaps with the next array. If it does not overlap, we store the current interval in the array and judge the next interval.

class Solution {
public:
   static bool cmp (const vector<int> &a,const vector<int>& b)
    {
        return a[0]<b[0];
    }

    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> result;
        if(intervals.size()==0) return result;
        sort(intervals.begin(),intervals.end(),cmp);
      

        result.push_back(intervals[0]);

        for(int i=1;i<intervals.size();i++)
        {
            if(intervals[i][0]<=result.back()[1])
            {
                result.back()[1]=max(intervals[i][1],result.back()[1]);
                //只更新左边界,因为右边界我们进行过排序,右边界就是从大到小进行排序的。
            }
            else
            {
                result.push_back(intervals[i]);
            }
        }        
        return result;
    }
};

738. Monotonically Increasing Numbers - LeetCode

An integer is said to be monotonically increasing if and only if the numbers x and y in each adjacent digit satisfy x <= y.

Given an integer n, returns the largest number less than or equal to n that increases monotonically.

 The data given in this question is relatively simple. Normal people may think of violently splitting the numbers for judgment, but the violent solution will time out.

Violent solution is to traverse this value from large to small, and if the obtained value meets the requirements, we will output this value.

Violent solution: it will time out 

class Solution {
private:
    
    bool checkNum(int num) {
        int max = 10;
        while (num) {
            int t = num % 10;
            if (max >= t) max = t;
            else return false;
            num = num / 10;
        }
        return true;
    }
public:
    int monotoneIncreasingDigits(int N) {
        for (int i = N; i > 0; i--) { // 从大到小遍历
            if (checkNum(i)) return i;
        }
        return 0;
    }
};

 Greedy algorithm idea:

Analyze this number bit by bit. If the previous bit is less than the next bit, then the previous bit will be reduced by one, and the latter bit will become the maximum value of 9. As for the traversal order, we traverse from the back to the front, because from the back to the front we meet the conditions that we require the number to increase (if you don't understand, you can try it manually).

class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string str = to_string(n);
       
        int flag=str.size();
        for(int i=str.size()-1;i>0;i--)
        {
            if(str[i-1]>str[i])
            {
                
            str[i-1]--;
               flag = i;

            }
        }
        for(int i=flag;i<str.size();i++)
        {
            str[i]='9';
        }

          return stoi(str);
    }   
};

Summarize:

        When doing a problem, you should first jump out of the framework of the algorithm, and first think about how to solve this problem, whether it is a violent algorithm or something, first try to solve the problem, and then talk about optimizing the time complexity or something.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

Guess you like

Origin blog.csdn.net/fckbb/article/details/132073057