860.柠檬水找零
题目链接:https://leetcode.cn/problems/lemonade-change/
这道题目条件限定的比较多,我认为其实算暴力了 唯一贪心的地方是20优先找10+5而不是5+5+5
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five, ten = 0, 0
for bill in bills:
if bill == 5:
five += 1
elif bill == 10:
if five <= 0:
return False
five -= 1
ten += 1
else:
if ten <= 0:
if five < 3:
return False
five -= 3
else:
ten -= 1
if five <= 0 :
return False
five -= 1
return True
406.根据身高重建队列
题目链接:https://leetcode.cn/problems/queue-reconstruction-by-height/
先按身高高的排序
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (-x[0],x[1]))
res = []
for i in people:
res.insert(i[1],i)
return res
452. 用最少数量的箭引爆气球
题目链接:https://leetcode.cn/problems/minimum-number-of-arrows-to-burst-balloons/
对于边界的控制要小心,如果超了就代表肯定要多一支箭
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
if len(points) == 0: return 0
count = 1
points.sort(key=lambda x: x[0])
for i in range(1, len(points)):
if points[i][0] > points[i-1][1]:
count += 1
else:
points[i][1] = min(points[i][1], points[i-1][1])
return count