Day35|leetcode 860. Lemonade change, 406. Rebuild the queue based on height, 452. Detonate the balloon with the least number of arrows

leetcode 860. Lemonade change

Topic Link: 860. Lemonade Change - LeetCode

Video link: Greedy algorithm, it looks complicated, but the logic is fixed! LeetCode: 860. Lemonade change_哔哩哔哩_bilibili

topic overview

At the lemonade stand, a glass of lemonade sells for  5 US dollars. Customers line up to buy your product, (in  bills order of bill payment) one drink at a time.

Each customer just buys a glass of lemonade and pays you  5 dollars, 10 dollars, or  20 dollars. You have to give each customer the correct change, which means the net transaction is each customer paying you  5 dollars.

Note that you don't have any change on hand at first.

Give you an array of integers  bills , which  bills[i] is  i the bill paid by the first customer. If you can give each customer correct change, return  true , otherwise return  false .

Example 1:

Input: bills = [5,5,5,10,20]
 Output: true
 Explanation:
 For the first 3 customers, we collect 3 $5 bills in order. 
For the 4th customer, we take a $10 bill and give back $5. 
With the 5th customer, we return a $10 bill and a $5 bill. 
Since all customers got correct change, we output true.

train of thought

In fact, if you think about this question carefully, there are only three situations. The first is that the customer pays $5, and we can keep it at ease; the second is that the customer pays $10, and we have to change $5; the third situation That is, the customer pays 20 dollars, and we either give change one 10 and one 5, or we give change 3 5 dollars.

So it can be found from this that $5 is omnipotent, and $20 has no effect on change, so the greed of this question is here (the third case), and the priority is to find out when the change is $10. Keep $5.

Code

class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
        int five = 0,ten = 0,twenty = 0;
        for(int bill : bills) {
            if(bill == 5) five++;
            if(bill == 10) {
                if(five <= 0) return false;
                ten++;
                five--;
            }
            if(bill == 20) {
                if(ten > 0 && five > 0) {
                    five--;
                    ten--;
                    twenty++;//写不写都行,毕竟找零用不上
                }
                else if(five >= 3){
                    five -= 3;
                    twenty++;
                }
                else return false;
            }
        }
        return true;

    }
};

Leetcode 406. Rebuild the queue according to the height

Topic link: 406. Rebuilding the queue based on height - LeetCode

Video link: Greedy algorithm, don’t be greedy on both sides, you will lose sight of the other | LeetCode: 406. Rebuild the queue according to the height_哔哩哔哩_bilibili

topic overview

Suppose a group of people in random order stand in a queue, and the array  people represents the attributes of some people in the queue (not necessarily in order). Each  people[i] = [hi, ki] represents  i the height of the first person  hi , and   there is  exactly one  person whose ki height is greater than or equal  to the front.hi

Please reconstruct and return  people the queue represented by the input array. The returned queue should be formatted as an array  queue , where  is  the property of the first person queue[j] = [hj, kj] in the queue  (  the person who is at the front of the queue).jqueue[0]

Example 1:

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
 Output: [[5,0], [7,0],[5,2],[6,1],[4,4],[7,1]]
 Explanation: 
The height of the person numbered 0 is 5, and there is no taller or same person row in front of him. 
The person numbered 1 has a height of 7, and there is no person who is taller or the same in front of him. 
The height of the person numbered 2 is 5, and there are 2 people with higher or the same height in front of him, that is, people numbered 0 and 1. 
The height of the person numbered 3 is 6, and there is 1 person who is taller or the same height in front of him, that is, the person numbered 1. 
The height of the person numbered 4 is 4, and there are 4 people with higher or the same height in front of him, that is, people numbered 0, 1, 2, and 3. 
The height of the person numbered 5 is 7, and there is 1 person who is taller or the same height in front of him, that is, the person numbered 1. 
So [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.

train of thought

When encountering two dimensions, you must use one dimension and one dimension, but which of the two dimensions should be ranked first must be determined by yourself. After the trial, you will find that if you first determine k and then determine h, the final result is not suitable. The meaning of the question, so we need to determine h first and then k.

If you determine h first, you must pay attention to the heights must be arranged from large to small. The reason for the arrangement from large to small is to not be affected by the height behind, and k only needs to be inserted according to the subscript.

406. Rebuild the queue based on height

 

Code

class Solution {
public:
    static bool cmp(const vector<int>& a,const vector<int>& b) {
        if(a[0] == b[0]) return a[1] < b[1];
        return a[0] > b[0];
    }
    vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
        sort(people.begin(),people.end(),cmp);
        vector<vector<int>> que;
        for(int i = 0;i < people.size();i++) {
            int position = people[i][1];
            que.insert(que.begin() + position,people[i]);
        }
        return que;

    }
};

leetcode 452. Detonate the balloon with the least number of arrows

Topic Link: 452. Detonate Balloons with the Least Number of Arrows - LeetCode

Video Link: Greedy Algorithm, Judging Overlapping Interval Problems | LeetCode: 452. Use the least number of arrows to detonate the balloon_哔哩哔哩_bilibili

topic overview

There are spherical balloons attached to a wall represented by an XY plane. Balloons on the wall are recorded in an array of integers  points , which points[i] = [xstart, xend] represent  balloons with horizontal diameters between xstart and  . xendYou don't know the exact y coordinate of the balloon.

A bow and arrow can be shot perfectly vertically from different points along the x-axis   . Shoot an arrow at the coordinates  x . If there is a balloon whose diameter starts and ends at coordinates  xstart, xendand satisfies   xstart ≤ x ≤ xend, the balloon will be  detonated  . There is no limit to the number of bows that can be shot   . Once the bow and arrow is shot, it can move forward indefinitely.

Given an array  points , return the minimum  number of arrows  that must be shot to explode all balloons  .

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
 Output: 2
 Explanation: The balloon can be burst with 2 arrows: 
- shot at x=6 Arrows, pop balloons [2,8] and [1,6]. 
- Shoot the arrow at x = 11, breaking the balloons [10,16] and [7,12].

train of thought

The local optimum is to shoot all overlapping balloons with one arrow, and the global optimum is to shoot all the balloons with the least number of arrows.

One of the difficulties in this question is the range of the balloons. You must first sort the range of the balloons according to the left boundary or the right boundary from small to large (the picture below is in order from small to large on the left boundary), if the next balloon If the left border is larger than the right border of the previous balloon, then an arrow is definitely needed. If there is an overlap, the border must be updated first, because you don't know whether the border of the next balloon will overlap.

452. Explode a balloon with the fewest number of arrows

 

Code

class Solution {
private:
    static bool cmp(const vector<int>& a,const vector<int>& b) {
        return a[0] < b[0];
    }
public:
    int findMinArrowShots(vector<vector<int>>& points) {
        if(points.size() == 0) return 0;
        sort(points.begin(),points.end(),cmp);
        int result = 1;
        for(int i = 1;i < points.size();i++) {
            if(points[i][0] > points[i - 1][1]) {
                result++;
            }
            else {
                points[i][1] = min(points[i - 1][1],points[i][1]);
            }
        }
        return result;

    }
};

Guess you like

Origin blog.csdn.net/m0_74583479/article/details/132258034