[Likou Brush Questions | Day 20]

Table of contents

Foreword:

406. Rebuild Queue Based on Height - LeetCode

452. Detonate a Balloon with the Least Number of Arrows - LeetCode

Summarize:


Foreword:

Today's greedy algorithm topic

406. Rebuild Queue Based on Height - LeetCode

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] means that the height of the i-th person is hi, and there are exactly ki people whose height is greater than or equal to hi in front.

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

Many friends are directly confused when they see this question, and they don’t know the requirements of the question. Here I will explain it in detail: The meaning of this question is: each element in the people array has (a,b), a indicates the height of the current person, and b indicates how many people are taller than him or the same height (from left to right) in the arranged sequence. own height).

In fact, this question has a certain degree of similarity with the candy distribution problem we have brushed up before. For example, we have to consider two dimensions. To distribute candy, we must consider both the number of people on the left and the number of people on the right. This is to consider not only the number of people meeting the requirements, but also the height meeting the requirements, and our macroscopic idea of ​​​​solving the problem is the same: step by step solution!

We use a case to explain to you:

people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]

At this point, we first sort the heights (from high to low):

 Then at this time, the person in front of each person must be a person whose height is greater than or equal to his own height. At this time, it is good to insert according to the number of people in front of him in the given array, because at this time, all the people in front of him are greater than or equal to his height. The insertion will not affect other numbers, because we insert from the back to the front, so when the insertion is added, the left and right sides of oneself must be larger than itself, that is to say, the inserted element will not affect the number of people who are greater than or equal to their height in front of the latter element.

For example: [7,0][7,1][6,1]

Because at this time, the requirement of a height of 6 can only have one person taller than himself, then insert it behind [7,0] and become [7,0][6,1][7,1], and the insertion of [6,1] will not affect the number of elements larger than himself in front of [7,1].

According to this idea, the final result can be obtained:

solution: 

class Solution {
public:
   static bool compare(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(),compare);
        
        vector<vector<int>>que;
          for(int i=0;i<people.size();i++)
          {
              int poision = people[i][1];
              que.insert(que.begin() + poision, people[i]);
          }
          
            return que;
      }
};

452. Detonate a Balloon with the Least Number of Arrows - LeetCode

There are spherical balloons attached to a wall represented by an XY plane. The balloons on the wall are recorded in the integer array points , where points[i] = [xstart, xend] means balloons with horizontal diameter between xstart and xend. You 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 coordinate x, if there is a balloon whose diameter starts and ends at coordinates xstart, xend, and 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.

 This question is too abstract, so let’s use a drawing to explain it: the horizontal line is equivalent to a balloon. Our bow and arrow can only be launched perpendicular to the x-axis. As long as it passes through the horizontal line, it is considered to be an explosion. Find the minimum number of bows and arrows that can make the balloon explode. If we want to shoot the most balloons with fewer bows and arrows, we must first try to make the balloons overlap, and then let the bows and arrows shoot at positions with more overlapping areas, and let the balloons overlap as much as possible, that is, to sort the balloons. First, sort [10,16], [2,8], [1,6], [7,12]. The sorting order does not matter, it does not matter from large to small or from small to large.

 And the least way to get these sorted balloons to explode is:

Release the bow and arrow at these two positions, and you can shoot all the balloons with the minimum number of bows and arrows.

The logic of the code is:

         If the left boundary of this balloon is smaller than the right boundary of the previous balloon, then the two balloons can be shot together.

class Solution {
public:
    int findMinArrowShots(const vector<vector<int>>& points) {
        if (points.empty()) return 0;

        int result = 1;
        int prevEnd = points[0][1];
        int i = 1;
        while (i < points.size()) {
            if (points[i][0] > prevEnd) {
                ++result;
                prevEnd = points[i][1];
                ++i;
            }
            else {
                prevEnd = min(prevEnd, points[i][1]);
                ++i;
            }
        }
        return result;
    }
};

Summarize:

It is difficult to innovate in the thinking behind the greedy algorithm question, mainly because of the difficulty in code implementation.

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/131776945