20210322: A collection of typical questions about greedy thinking

A collection of typical questions about Greedy Thoughts

Write in front

  1. Greedy thinking often lies in thinking about the point of greed, and does not require the ability to write certain code structures with bare hands. Personally, what is required is that you are keenly aware of where this point of greed is.
  2. List these typical topics as a review and review.

Question list

    1. Distribute cookies

Insert picture description here
2. 376. Swing sequence
Insert picture description here
3. 402. Remove the K-digit number
Insert picture description here
4. 55. Jump game
Insert picture description here
5. 45. Jump game II
Insert picture description here
6. 452. Use the least number of arrows to detonate the balloon.
Insert picture description here
Not intuitive, give a more intuitive picture:Insert picture description here

Thinking analysis

  1. Distribute cookies: The greedy point lies in: the smallest possible cookie to satisfy as many children as possible.
  2. Swing sequence: The greedy point is that only the length of the swing is calculated. This involves a state machine similar to a digital circuit. To be precise, it is a rice-grain state machine, which is very useful in this type of problem. Recommended to master
  3. Remove the K-digit number: The greedy point is: go from the high position to the low position, compare the size when you go, and go to the big one to meet expectations.
  4. Jumping game: The greedy point is: how to not waste the jumping distance, then we can consider pushing the starting point backward from the end point, and finally see if the starting point index is 0.
  5. .Jumping game II: The greedy point is: the same as the previous question, but still not wasting the jumping distance, then we consider whether the current jump is a necessary jump, the comments are more detailed, see the code.
  6. Use the least number of arrows to detonate the balloon: The greedy point is whether it is necessary to increase archers, and maintain the optimal shooting range of a single archer.

Code

    1. Distribute cookies
class Solution {
    
    
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
    
    
        // 排序操作
        sort(g.begin(),g.end());
        sort(s.begin(),s.end());

        // 遍历
        int child = 0;
        int candy = 0;
        while (child < g.size() && candy < s.size()) {
    
    
            if (g[child] <= s[candy] ) {
    
    
                child++;
            }
            candy++;
        }
        return child;
    }
};
    1. Swing sequence
class Solution {
    
    
public:
    int wiggleMaxLength(vector<int>& nums) {
    
    
        int len = nums.size();
        if (len < 2) {
    
    
            return len;
        }
        static const int BEGIN = 0;
        static const int UP = 1;
        static const int DOWN = 2;
        int STATE = BEGIN;
        int max_length = 1;
        for (int i = 1; i < len; ++i) {
    
    
            switch (STATE) {
    
    
                case BEGIN:
                    if (nums[i - 1] < nums[i]) {
    
    
                        STATE = UP;
                        max_length++;
                    } else if (nums[i - 1] > nums[i]) {
    
    
                        STATE = DOWN;
                        max_length++;
                    }
                    break;
                case UP:
                    if (nums[i - 1] > nums[i]) {
    
    
                        STATE = DOWN;
                        max_length++;
                    }
                    break;
                case DOWN:
                    if (nums[i - 1] < nums[i]) {
    
    
                        STATE = UP;
                        max_length++;
                    }
                    break;
            }
        }
        return max_length;
    }
};
    1. Remove K digits
class Solution {
    
    
public:
    string removeKdigits(string num, int k) {
    
    
        vector<int> S;
        string result = "";
        for (int i = 0; i < num.size(); ++i) {
    
    
            int number = num[i] - '0';
            while ( (S.size() != 0) &&  (number < S[S.size() - 1]) && (k > 0) ) {
    
    
                S.pop_back();
                k--;
            }
            if (number != 0 || S.size() != 0) {
    
    
                S.push_back(number);
            }
        }

        while (S.size() != 0 && k > 0) {
    
    
            S.pop_back();
            k--;
        }
        for (int i = 0;i < S.size(); ++i) {
    
    
            result.append(1,('0'+S[i]) );
        }
        if (result == "") {
    
    
            result = "0";
        }
        return result;
    }
};
    1. Jumping game
class Solution {
    
    
public:
    bool canJump(vector<int>& nums) {
    
    
        int lastPos = nums.size() - 1;
		for (int i = nums.size() - 2; i >= 0; i--) {
    
    
			if (i + nums[i] >= lastPos) {
    
    
				lastPos = i;
			}
		}
		return lastPos == 0;
    }
};
    1. Jump game II
class Solution {
    
    
public:
    int jump(vector<int>& nums) {
    
    
        // 特况处理
        if (nums.size() <= 1) {
    
    
            return 0;
        }

        // 遍历,使用两个变量存储当前可以到达的最大距离和是否需要必要跳跃
        int cur_max = nums[0];
        int pre_max = nums[0];
        int res = 1;
        for (int i = 1; i < nums.size(); ++i) {
    
    
            // 更新cur_max:之前需要依次必要跳跃,res增加1,并同步更新当前最远距离
            if (i > cur_max) {
    
    
                res++;
                cur_max = pre_max;
            }
            
            // 更新pre_max以判断其是否需要用于更新当前最大距离
            if (nums[i] + i > pre_max)  {
    
    
                pre_max = nums[i] + i;
            }
        }
        return res;
    }
};
    1. Use the least number of arrows to detonate the balloon
bool cmp(const vector<int>& a,const vector<int>& b){
    
    
    return a[1] < b[1];
}

class Solution {
    
    
public:
    int findMinArrowShots(vector<vector<int>>& points) {
    
    
        if (points.size() == 0) {
    
    
            return 0;
        }
        // 根据左端点进行排序
        sort(points.begin(),points.end(),cmp);
        // 维护当前弓箭手最优射击区间
        int shoot_num = 1;
        int shoot_begin = points[0][0];
        int shoot_end = points[0][1];

        for (int i = 1; i < points.size(); ++i) {
    
    
            if (points[i][0] <= shoot_end) {
    
    
                shoot_begin = points[i][0];
                if (shoot_end > points[i][1]) {
    
    
                    shoot_end = points[i][1];
                }
            } else {
    
    
                shoot_num++;
                shoot_begin = points[i][0];
                shoot_end = points[i][1];
            }
        }
        return shoot_num;
    }
};

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/115103178