开始第四天的练习,感觉今天的题都挺好玩的。
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$20bill. 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
billswherebills[i]is the bill theithcustomer pays, returntrueif you can provide every customer with the correct change, orfalseotherwise.
需要找零的有$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). Eachpeople[i] = [hi, ki]represents theithperson of heighthiwith exactlykiother people in front who have a height greater than or equal tohi.Reconstruct and return the queue that is represented by the input array
people. The returned queue should be formatted as an arrayqueue, wherequeue[j] = [hj, kj]is the attributes of thejthperson 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
pointswherepoints[i] = [xstart, xend]denotes a balloon whose horizontal diameter stretches betweenxstartandxend. 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
xstartandxendis burst by an arrow shot atxifxstart <= 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