代码随想录算法训练营Day35 | 贪心算法(4/6) LeetCode 860.柠檬水找零 406.根据身高重建队列 452. 用最少数量的箭引爆气球

开始第四天的练习,感觉今天的题都挺好玩的。

第一题

860. Lemonade Change

At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5$10, or $20 bill. You must provide the correct change to each customer so that the net transaction is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

需要找零的有$10和$20,用于找零的是$5。由这个可以看出来,$5是最好的,能给$10和$20找零,但是$10只能给$20找零。

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        five = 0
        ten = 0
        twenty = 0
        for bill in bills:
            # Receive $5
            if bill == 5:
                five += 1
            
            # Receive $10
            if bill == 10:
                if five <= 0:
                    return False
                ten += 1
                five -= 1
            
            # Receive $20
            if bill == 20:
                if five > 0 and ten > 0:
                    five -= 1
                    ten -= 1

                elif five >= 3:
                    five -= 3
                else:
                    return False
        
        return True

第二题

406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

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

有两个维度,h和k,如果同时考虑的话,会顾此失彼。

在按照身高从大到小排序后:

局部最优:优先按身高高的people的k来插入。插入操作过后的people满足队列属性

全局最优:最后都做完插入操作,整个队列满足题目队列属性

class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people.sort(key=lambda x: (-x[0], x[1]))
        que = []
        for p in people:
            que.insert(p[1], p)
        return que

第三题

452. Minimum Number of Arrows to Burst Balloons

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

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

二维数组用来表示气球的宽度,当有气球在宽度重叠时,一箭就可以射穿重叠的气球。

为了让气球重叠起来,可以先进行排序。

如果气球重叠了,重叠气球中右边边界的最小值 之前的区间一定需要一个弓箭。

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0: return 0
        points.sort(key=lambda x: x[0])
        result = 1
        for i in range(1, len(points)):
            if points[i][0] > points[i - 1][1]:
                result += 1     
            else:
                points[i][1] = min(points[i - 1][1], points[i][1]) 
        return result

你可能感兴趣的:(LeetCode,贪心算法,leetcode,算法)