代码随想录算法训练营DAY36|860.柠檬水找零、406.根据身高重建队列、452. 用最少数量的箭引爆气球

860.柠檬水找零

  • 题目链接:860.柠檬水找零
  • 注意20美元的情况有两种找零方法,优先选择10+5,两种方法count减少的方式不同
  • 无需记录20美元数量,因为不会用20美元找零
class Solution(object):
    def lemonadeChange(self, bills):
        """
        :type bills: List[int]
        :rtype: bool
        """
        count = [0 for i in range(2)]

        for j in range(len(bills)):
            if bills[j]==5:
                count[0]+=1
            elif bills[j]==10:
                if count[0]==0:
                    print(j+1, count)
                    return False
                else:
                    count[0]-=1
                    count[1]+=1
            elif bills[j]==20:
                if (count[0]<3 and count[1]==0) or count[0]==0:
                    print(j+1, count)
                    return False
                elif count[1]==0:
                    count[0]-=3
                elif count[1]!=0:
                    count[1]-=1
                    count[0]-=1
                            
        return True

406.根据身高重建队列

people.sort()=[[7, 0], [7, 1], [6, 1], [5, 0], [5, 2], [4, 4]]
que=[[7, 0]]
[[7, 0], [7, 1]] 
[[7, 0], [6, 1], [7, 1]] 
[[5, 0], [7, 0], [6, 1], [7, 1]]
[[5, 0], [7, 0], [5, 2], [6, 1], [7, 1]] 
[[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
class Solution(object):
    def reconstructQueue(self, people):
        """
        :type people: List[List[int]]
        :rtype: List[List[int]]
        """
        people.sort(key=lambda x: (-x[0], x[1]))    #h从大到小,k从小到大
        que = []

        for p in people:
           que.insert(p[1],p)
        
        return que

452. 用最少数量的箭引爆气球

  • 题目链接:
  • points要先sort!
class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """

        points.sort(key=lambda x: x[0])
        intervals = [points[0]]

        for point in points:
            if point[0]>intervals[-1][1]:    #point在所有区间右端
                intervals.append(point)
            elif point[1]<intervals[0][0]:   #point在所有区间左端
                intervals.insert(0, point)
            else:
                for i in range(len(intervals)):
                    if point[0]>intervals[i][1]:
                        continue
                    if point[1]<intervals[i][0]:
                        intervals.insert(i, point)
                    else:
                        if point[0]>intervals[i][0]:
                            intervals[i][0]=point[0]
                        if point[1]<intervals[i][1]:
                            intervals[i][1]=point[1]
                    break

        return len(intervals)
  • 因为已经sort了 所以无需判断左端点
class Solution(object):
    def findMinArrowShots(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """

        if len(points)==0: return 0

        result=1
        points.sort(key=lambda x: x[0])

        for i in range(len(points)):
            if points[i][0]>points[i-1][1]:
                result+=1
            else:
                points[i][1]=min(points[i-1][1], points[i][1])

        return result
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值