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

LeetCode20. 有效的括号

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/assign-cookies/LeetCode(力扣)455. 分发饼干Python_第1张图片

代码

从大遍历

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

从小遍历

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

你可能感兴趣的:(leetcode,python,算法,职场和发展)