From 1b915d57bf54c46f070b2852f12407e5ea2d6492 Mon Sep 17 00:00:00 2001 From: ironartisan Date: Thu, 26 Aug 2021 20:14:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00455.=E5=88=86=E5=8F=91?= =?UTF-8?q?=E9=A5=BC=E5=B9=B2Python3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index f377b99e..1e1c4afe 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -156,6 +156,7 @@ class Solution { Python: ```python3 class Solution: + # 思路1:优先考虑胃饼干 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -165,6 +166,20 @@ class Solution: res += 1 return res ``` +```python +class Solution: + # 思路2:优先考虑胃口 + def findContentChildren(self, g: List[int], s: List[int]) -> int: + g.sort() + s.sort() + start, count = len(s) - 1, 0 + for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口 + if start >= 0 and g[index] <= s[start]: + start -= 1 + count += 1 + return count +``` + Go: ```golang //排序后,局部最优