860. 柠檬水找零
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five = 0
ten = 0
for i in bills:
if i == 5:
five +=1
elif i == 10:
if five>=1:
ten+=1
five-=1
else:
return False
else:
if ten>=1 and five>=1:
ten-=1
five-=1
elif five>=3:
five -= 3
else:
return False
return True
406. 根据身高重建队列
这道题类似分糖果那道题,分糖果要考虑左边和右边,这道题要考虑身高和排序,也是分成两部分来看,先看身高,这里有个技巧是从大到小排序,会方便很多,然后再考虑排序的事。另外要学习排序函数sorted()
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
result = []
people=sorted(people,key = lambda x : (-x[0],x[1]))
for i in people:
result.insert(i[1],i)
return result
452. 用最少数量的箭引爆气球
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points = sorted(points,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],points[i-1][1])
return result