860.柠檬水找零
题目描述: 860.柠檬水找零.
解法
贪心
class Solution(object):
def lemonadeChange(self, bills):
five,ten = 0, 0
for bill in bills:
if bill == 5:
five += 1
if bill == 10:
five -= 1
ten += 1
if bill == 20:
if ten >=1:
ten -= 1
five -= 1
else:
five -= 3
if five < 0:
return False
return True
没什么难度,优先消耗10块的
406.根据身高重建队列
题目描述: 406.根据身高重建队列.
解法
贪心
class Solution(object):
def reconstructQueue(self, people):
people.sort(key=lambda x:(-x[0],x[1]))
que = []
for p in people:
que.insert(p[1],p)
return que
整体思路真的很难,先用身高排序,然后k值小的在前面,最后直接从大到小满足k值的要求即可。
452. 用最少数量的箭引爆气球
题目描述: 452. 用最少数量的箭引爆气球.
解法
贪心
class Solution(object):
def findMinArrowShots(self, points):
points.sort(key=lambda x: x[0])
count = 1
ls,rs = points[0]
for i in range(1,len(points)):
if points[i][0] <= rs:
ls, rs = points[i][0],min(rs,points[i][1])
else:
count += 1
ls,rs = points[i]
return count
基本思路想到了,但是不需要单独设置一个用于存储交集的列表,使用两个指针来维护最后一个交集的区间即可。