LEETCODE-DAY35


title: LEETCODE-DAY35

date: 2024-03-26 16:11:08
tags:

今日内容:860.柠檬水找零、406.根据身高重建队列、452. 用最少数量的箭引爆气球

T1

python 复制代码
class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        table={5:0,10:0,20:0}
        for i in range(len(bills)):
            if bills[i]==5:
                table[5]+=1
            elif bills[i]==10:
                if table[5]<=0:
                    return False
                else:
                    table[10]+=1
                    table[5]-=1
            elif bills[i]==20:
                if table[5]<=0 or table[10]<=0:
                    return False 
                else: 
                    table[20]+=1
                    table[10]-=1
                    table[5]-=1
        return True

输入

bills =

5,5,10,20,5,5,5,5,5,5,5,5,5,10,5,5,20,5,20,5

输出

false

预期结果

true

python 复制代码
class Solution:
    def lemonadeChange(self, bills: List[int]) -> bool:
        table={5:0,10:0,20:0}
        for i in range(len(bills)):
            if bills[i]==5:
                table[5]+=1
            elif bills[i]==10:
                if table[5]<=0:
                    return False
                else:
                    table[10]+=1
                    table[5]-=1
            elif bills[i]==20:
                if (5*table[5]+10*table[10]<15) or table[5]<=0:
                    return False 
                else: 
                    table[20]+=1
                    table[10]-=1
                    table[5]-=1
        return True
        

AC

T2

本题主要难点在于如何将数组按照h从大到小,h相同时k从小到大的方式进行排序

python 复制代码
class Solution:
    def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
        people.sort(key=lambda x:(-x[0], x[1]))
        queue=list()
        for i in range(len(people)):
            queue.insert(people[i][1],people[i])
        return queue

T3

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points.sort()
        l=len(points)
        for i in range(l):
            if i>0 and points[i][0]<points[i-1][1]:
                points[i][1]=points[i-1][1]
                points.pop(i-1)

        return len(points)

IndexError: list index out of range

~~~~~~^^^

if i>0 and points[i][0]<points[i-1][1]:

队列长度减小了

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points.sort()
        l=len(points)
        for i in range(l):
            if i>0 and i<len(points) and points[i][0]<points[i-1][1]:
                points[i][1]=points[i-1][1]
                points.pop(i-1)

        return len(points)

输入

points =

\[1,2\],\[2,3\],\[3,4\],\[4,5\]

输出

4

预期结果

2

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points.sort()
        l=len(points)
        for i in range(l):
            if i>0 and i<len(points) and points[i][0]<=points[i-1][1]:
                points[i][1]=points[i-1][1]
                points.pop(i-1)

        return len(points)

points =

\[3,9\],\[7,12\],\[3,8\],\[6,8\],\[9,10\],\[2,9\],\[0,9\],\[3,9\],\[0,6\],\[2,8\]

输出

5

预期结果

2

问题在于points pop之后指标也会产生变化,所以需要定义一个变量来记录重叠次数

思路:没有重叠时需射l箭,每有一个重叠少射一箭

python 复制代码
class Solution:
    def findMinArrowShots(self, points: List[List[int]]) -> int:
        points.sort()
        l=len(points)
        j=0
        for i in range(l):
            if i>0 and points[i][0]<=points[i-1][1]:
                points[i][1]=min(points[i-1][1],points[i][1])
                j+=1

        return l-j

AC

python 复制代码
相关推荐
用户50408278583920 分钟前
1. RAG 权威指南:从本地实现到生产级优化的全面实践
算法
Python×CATIA工业智造1 小时前
详细页智能解析算法:洞悉海量页面数据的核心技术
爬虫·算法·pycharm
Swift社区2 小时前
Swift 解 LeetCode 321:拼接两个数组中的最大数,贪心 + 合并全解析
开发语言·leetcode·swift
无聊的小坏坏2 小时前
力扣 239 题:滑动窗口最大值的两种高效解法
c++·算法·leetcode
黎明smaly2 小时前
【排序】插入排序
c语言·开发语言·数据结构·c++·算法·排序算法
YuTaoShao3 小时前
【LeetCode 热题 100】206. 反转链表——(解法一)值翻转
算法·leetcode·链表
YuTaoShao3 小时前
【LeetCode 热题 100】142. 环形链表 II——快慢指针
java·算法·leetcode·链表
CCF_NOI.3 小时前
(普及−)B3629 吃冰棍——二分/模拟
数据结构·c++·算法
运器1234 小时前
【一起来学AI大模型】支持向量机(SVM):核心算法深度解析
大数据·人工智能·算法·机器学习·支持向量机·ai·ai编程
Zedthm4 小时前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode