860. Lemonade Change 406. Queue Reconstruction by Height 452. Minimum Number of Arrows to Burst Ba

860. Lemonade Change

At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you and order one at a time (in the order specified by bills). Each customer will only buy one lemonade and pay with either a $5$10, or $20 bill. You must provide the correct change to each customer so that the net transaction净交易 is that the customer pays $5.

Note that you do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

 AC: XD

class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        f_bill = 0
        t_bill = 0

        for i in range(len(bills)):
            if bills[i] == 5:
                f_bill += 5
            elif bills[i] == 10 and f_bill > 0:
                t_bill += 10
                f_bill -=5
            elif f_bill > 0 and t_bill >0:
                t_bill -= 10
                f_bill -= 5
            elif f_bill >= 15 and t_bill == 0:
                f_bill -= 15
            else:
                return False    
        return True

406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes属性 of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).

 

 

Time complexity: O(nlog n + n^2)
Space complexity: O(n)

1. people.sort(key=lambda x: (-x[0], x[1])): Sort by height x[0] in reverse order -, if same height, sort by x[1]

2. que.insert(p[1],p): insert p by p[1]          list.insert(index, element)

class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
    	# 先按照h维度的身高顺序从高到低排序。确定第一个维度
        # lambda返回的是一个元组:当-x[0](维度h)相同时,再根据x[1](维度k)从小到大排序
        people.sort(key=lambda x: (-x[0], x[1]))
        que = []
	
	# 根据每个元素的第二个维度k,贪心算法,进行插入
        # people已经排序过了:同一高度时k值小的排前面。
        for p in people:
            que.insert(p[1], p)
        return que

452. Minimum Number of Arrows to Burst Balloons

There are some spherical/ˈsfɪrɪkəl/球形 balloons气球 taped粘 onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal/ˌhɒrɪˈzɒntəl/ diameter水平直径 stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

Arrows箭 can be shot up directly vertically纵向 (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst爆 by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.

 

 my solution:

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0:
            return 0

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

        for p in points[1:]:
            if p[0] > end:
                count += 1
                end = p[1]
   
            elif p[0] < end and p[1] < end:
                end = p[1]
            
        return count

Quicksort:

Time complexity: O(nlog n)
Space complexity: O(1)

class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        if len(points) == 0: return 0
        points.sort(key=lambda x: x[0])
        result = 1
        for i in range(1, len(points)):
            if points[i][0] > points[i - 1][1]: # 气球i和气球i-1不挨着,注意这里不是>=
                result += 1     
            else:
                points[i][1] = min(points[i - 1][1], points[i][1]) # 更新重叠气球最小右边界
        return result
class Solution: # 不改变原数组
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points.sort(key = lambda x: x[0])
        sl,sr = points[0][0],points[0][1]
        count = 1
        for i in points:
            if i[0]>sr:
                count+=1
                sl,sr = i[0],i[1]
            else:
                sl = max(sl,i[0])
                sr = min(sr,i[1])
        return count

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值