From 10940f22441cb26e0e985290592d935a50b4807c Mon Sep 17 00:00:00 2001 From: masx200 <34191203+masx200@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:09:43 +0800 Subject: [PATCH 1/3] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1b1be528..ed0c0ed3 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Step 2. Add the dependency 展开查看 +https://leetcode-cn.com/problems/assign-cookies/ + https://leetcode-cn.com/problems/evaluate-the-bracket-pairs-of-a-string/ https://leetcode.cn/problems/find-the-middle-index-in-array/ From d8c2a160cddffa38e4b08225d465d612fb86ffc0 Mon Sep 17 00:00:00 2001 From: masx200 <34191203+masx200@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:11:51 +0800 Subject: [PATCH 2/3] Create index.ts --- assign-cookies/index.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 assign-cookies/index.ts diff --git a/assign-cookies/index.ts b/assign-cookies/index.ts new file mode 100644 index 00000000..2beaa3b4 --- /dev/null +++ b/assign-cookies/index.ts @@ -0,0 +1,16 @@ +function findContentChildren(g: number[], s: number[]): number { + g.sort((a, b) => a - b); + s.sort((a, b) => a - b); + const m = g.length, n = s.length; + let count = 0; + for (let i = 0, j = 0; i < m && j < n; i++, j++) { + while (j < n && g[i] > s[j]) { + j++; + } + if (j < n) { + count++; + } + } + return count; +} +export default findContentChildren From 8e8e078273e06d5beee8fa4aea826b4bef40a2dc Mon Sep 17 00:00:00 2001 From: masx200 <34191203+masx200@users.noreply.github.com> Date: Thu, 12 Jan 2023 09:43:36 +0800 Subject: [PATCH 3/3] Update index.ts --- assign-cookies/index.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/assign-cookies/index.ts b/assign-cookies/index.ts index 2beaa3b4..1914c287 100644 --- a/assign-cookies/index.ts +++ b/assign-cookies/index.ts @@ -1,16 +1,14 @@ function findContentChildren(g: number[], s: number[]): number { - g.sort((a, b) => a - b); - s.sort((a, b) => a - b); - const m = g.length, n = s.length; - let count = 0; - for (let i = 0, j = 0; i < m && j < n; i++, j++) { - while (j < n && g[i] > s[j]) { - j++; + g = g.sort((a, b) => a - b) + s = s.sort((a, b) => a - b) + let result = 0 + let index = s.length - 1 + for(let i = g.length - 1; i >= 0; i--) { + if(index >= 0 && s[index] >= g[i]) { + result++ + index-- } - if (j < n) { - count++; - } - } - return count; + } + return result } export default findContentChildren