LeetCode(力扣)455. 分发饼干Python

LeetCode20. 有效的括号

题目链接

https://leetcode.cn/problems/assign-cookies/![在这里插入图片描述](https://file.jishuzhan.net/article/1701080909790318594/04dd97581b7243328b93651ff221052e.png)

代码

从大遍历

python 复制代码
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        index = len(s) - 1
        result = 0
        for i in range(len(g) - 1, -1, -1):
            if s[index] >= g[i] and index >= 0:
                result += 1
                index -= 1
        return result

从小遍历

python 复制代码
class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        index = 0
        for i in range(len(s)):
            if index < len(g) and g[index] <= s[i]:
                index += 1
        return index
相关推荐
nenchoumi31192 分钟前
AirSim/Cosys-AirSim 游戏开发(一)XBox 手柄 Windows + python 连接与读取
windows·python·xbox
GoodStudyAndDayDayUp3 分钟前
初入 python Django 框架总结
数据库·python·django
星辰大海的精灵11 分钟前
基于Dify+MCP实现通过微信发送天气信息给好友
人工智能·后端·python
精灵vector16 分钟前
Agent短期记忆的几种持久化存储方式
人工智能·python
kaiaaaa18 分钟前
算法训练第十一天
数据结构·算法
?!71420 分钟前
算法打卡第18天
c++·算法
北京_宏哥31 分钟前
🔥Python零基础从入门到精通详细教程4-数据类型的转换- 上篇
前端·python·面试
springfe010133 分钟前
构建大顶堆
前端·算法
乾巫宇宙国监察特使41 分钟前
Python的设计模式
python·测试
Hockor1 小时前
写给前端的 Python 教程四(列表/元组)
前端·后端·python