3道题目
860. 柠檬水找零
406. 根据身高重建队列
452. 用最少数量的箭引爆气球
解题理解
一开始理解错了,以为手里的钱只要超过找零的就行,其实还需要确定手里的整钱有多少才行。可以初始化三张钞票的数量,在收到5块时,直接5++,收到10,先看5有没有能找的,没有就False,收到20先看10再看5(跟找钱逻辑一样),不够找就False。
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
five = ten = twenty = 0
for bill in bills:
if bill == 5:
five += 1
elif bill == 10:
ten += 1
if five > 0:
five -= 1
else:
return False
elif bill == 20:
twenty += 1
if ten > 0:
ten -= 1
if five > 0:
five -= 1
else:
return False
else:
if five >= 3:
five -= 3
else:
return False
return True
406
这道题说是跟昨天那道前后顺序排序有关,但感觉相关性不大,其中还需要利用插入排序才能确定最后的结果。
总体思路是,现根据身高从大到小排序,然后新建一个列表,利用插入排序,根据k_i从小到大排序。
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
people.sort(key=lambda x: (-x[0], x[1]))
res = []
for p in people:
res.insert(p[1], p)
return res
452
这个不断确定最小有边界的思路很巧妙,不太想得出来。
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key=lambda x: x[0])
res = 1
for i in range(1, len(points)):
if points[i][0] > points[i - 1][1]:
res += 1
else:
points[i][1] = min(points[i - 1][1], points[i][1])
return res