From 49690a1712142e0739797b34c7422b155b6ae4e5 Mon Sep 17 00:00:00 2001 From: WmW Date: Fri, 12 Jan 2024 16:43:39 +0800 Subject: [PATCH] =?UTF-8?q?update=20455.=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2;=20=E4=BB=A3=E7=A0=81=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0455.分发饼干.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 778adc94..da4fd392 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -73,9 +73,9 @@ public: } }; ``` -* 时间复杂度:O(nlogn) -* 空间复杂度:O(1) +- 时间复杂度:O(nlogn) +- 空间复杂度:O(1) 从代码中可以看出我用了一个 index 来控制饼干数组的遍历,遍历饼干并没有再起一个 for 循环,而是采用自减的方式,这也是常用的技巧。 @@ -119,9 +119,9 @@ public: } }; ``` -* 时间复杂度:O(nlogn) -* 空间复杂度:O(1) +- 时间复杂度:O(nlogn) +- 空间复杂度:O(1) 细心的录友可以发现,这种写法,两个循环的顺序改变了,先遍历的饼干,在遍历的胃口,这是因为遍历顺序变了,我们是从小到大遍历。 @@ -177,7 +177,9 @@ class Solution { ``` ### Python + 贪心 大饼干优先 + ```python class Solution: def findContentChildren(self, g, s): @@ -192,7 +194,9 @@ class Solution: return result ``` + 贪心 小饼干优先 + ```python class Solution: def findContentChildren(self, g, s): @@ -229,7 +233,7 @@ func findContentChildren(g []int, s []int) int { ### Rust ```rust -pub fn find_content_children(mut children: Vec, mut cookie: Vec) -> i32 { +pub fn find_content_children(mut children: Vec, mut cookies: Vec) -> i32 { children.sort(); cookies.sort(); @@ -378,7 +382,9 @@ object Solution { } } ``` + ### C# + ```csharp public class Solution {