860. 柠檬水找零:
代码思路
写得好烂
class Solution:
def lemonadeChange(self, bills: List[int]) -> bool:
from collections import defaultdict
hash_set = defaultdict(int)
for i in range(len(bills)):
if bills[i] == 5:
hash_set[bills[i]] += 1
else:
to_round_up = bills[i] - 5
if to_round_up == 15:
if hash_set[10] != 0:
to_round_up = 15 - 10
if hash_set[5] != 0:
hash_set[10] -= 1
hash_set[5] -= 1
hash_set[bills[i]] += 1
else:
return False
elif hash_set[5] != 0:
if hash_set[5] >= 3:
hash_set[5] -= 3
hash_set[bills[i]] += 1
else:
return False
else:
return False
elif to_round_up == 10:
if hash_set[5] > 0:
hash_set[5] -= 1
hash_set[bills[i]] += 1
else:
return False
else: # to_round_up = 5
if hash_set[5] > 0:
hash_set[5] -= 1
hash_set[bills[i]] += 1
else:
return False
return True
406. 根据身高重建队列:
代码思路
按身高和后面的数量二次排序后,你可以按照数量逐个放心地插入。
class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
sort_people = sorted(people, key=lambda x: x[1])
sort_people = sorted(sort_people, key=lambda x: x[0], reverse=True)
result = []
for i in range(len(sort_people)):
result.insert(sort_people[i][1], sort_people[i])
return result
452. 用最少数量的箭引爆气球:
代码思路
dp,抽象成最长递增子序列。也是dp[i]就是严格以nums[i]结尾的最长长度。超时!!!!!
class Solution:
def findMinArrowShots(self, points: List[List[int]]) -> int:
points.sort(key=lambda x: x[0])
i = 1
n = len(points)
dp = [0 for i in range(n)]
dp[0] = 1
while i < n:
for j in list(range(i))[::-1]:
if points[j][1] < points[i][0]:
dp[i] = max(dp[i], dp[j]+1)
if dp[i] == 0:
dp[i] = 1
i += 1
return max(dp)