From 72c7c40cc816af6b33e8dc8be39188d1fe9738af Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 26 Nov 2024 23:44:10 +0300 Subject: [PATCH 1/5] feat: add solutions to lc problem: No.2924 --- .../2900-2999/2924.Find Champion II/README.md | 57 +++++++++++++++++++ .../2924.Find Champion II/README_EN.md | 57 +++++++++++++++++++ .../2924.Find Champion II/Solution.js | 14 +++++ .../2924.Find Champion II/Solution2.js | 9 +++ .../2924.Find Champion II/Solution2.ts | 9 +++ 5 files changed, 146 insertions(+) create mode 100644 solution/2900-2999/2924.Find Champion II/Solution.js create mode 100644 solution/2900-2999/2924.Find Champion II/Solution2.js create mode 100644 solution/2900-2999/2924.Find Champion II/Solution2.ts diff --git a/solution/2900-2999/2924.Find Champion II/README.md b/solution/2900-2999/2924.Find Champion II/README.md index 06297b5a2df91..29a1889a2874b 100644 --- a/solution/2900-2999/2924.Find Champion II/README.md +++ b/solution/2900-2999/2924.Find Champion II/README.md @@ -182,6 +182,63 @@ function findChampion(n: number, edges: number[][]): number { } ``` +#### JavaScript + +```js +function findChampion(n, edges) { + const indeg = Array(n).fill(0); + for (const [_, v] of edges) { + ++indeg[v]; + } + let [ans, cnt] = [-1, 0]; + for (let i = 0; i < n; ++i) { + if (indeg[i] === 0) { + ++cnt; + ans = i; + } + } + return cnt === 1 ? ans : -1; +} +``` + + + + + + + +### Solution 2 + + + +#### TypeScript + +```ts +function findChampion(n: number, edges: number[][]): number { + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value! : -1; +} +``` + +#### JavaScript + +```js +function findChampion(n, edges) { + const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; +} +``` + diff --git a/solution/2900-2999/2924.Find Champion II/README_EN.md b/solution/2900-2999/2924.Find Champion II/README_EN.md index 24c47c075f9de..f7c66061a9baa 100644 --- a/solution/2900-2999/2924.Find Champion II/README_EN.md +++ b/solution/2900-2999/2924.Find Champion II/README_EN.md @@ -180,6 +180,63 @@ function findChampion(n: number, edges: number[][]): number { } ``` +#### JavaScript + +```js +function findChampion(n, edges) { + const indeg = Array(n).fill(0); + for (const [_, v] of edges) { + ++indeg[v]; + } + let [ans, cnt] = [-1, 0]; + for (let i = 0; i < n; ++i) { + if (indeg[i] === 0) { + ++cnt; + ans = i; + } + } + return cnt === 1 ? ans : -1; +} +``` + + + + + + + +### Solution 2 + + + +#### TypeScript + +```ts +function findChampion(n: number, edges: number[][]): number { + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value! : -1; +} +``` + +#### JavaScript + +```js +function findChampion(n, edges) { + const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; +} +``` + diff --git a/solution/2900-2999/2924.Find Champion II/Solution.js b/solution/2900-2999/2924.Find Champion II/Solution.js new file mode 100644 index 0000000000000..4cfe3b010727b --- /dev/null +++ b/solution/2900-2999/2924.Find Champion II/Solution.js @@ -0,0 +1,14 @@ +function findChampion(n, edges) { + const indeg = Array(n).fill(0); + for (const [_, v] of edges) { + ++indeg[v]; + } + let [ans, cnt] = [-1, 0]; + for (let i = 0; i < n; ++i) { + if (indeg[i] === 0) { + ++cnt; + ans = i; + } + } + return cnt === 1 ? ans : -1; +} diff --git a/solution/2900-2999/2924.Find Champion II/Solution2.js b/solution/2900-2999/2924.Find Champion II/Solution2.js new file mode 100644 index 0000000000000..1ff3ff21e5c69 --- /dev/null +++ b/solution/2900-2999/2924.Find Champion II/Solution2.js @@ -0,0 +1,9 @@ +function findChampion(n, edges) { + const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; +} diff --git a/solution/2900-2999/2924.Find Champion II/Solution2.ts b/solution/2900-2999/2924.Find Champion II/Solution2.ts new file mode 100644 index 0000000000000..4c8575c10db7b --- /dev/null +++ b/solution/2900-2999/2924.Find Champion II/Solution2.ts @@ -0,0 +1,9 @@ +function findChampion(n: number, edges: number[][]): number { + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); + + for (const [_, v] of edges) { + vertexes.delete(v); + } + + return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value! : -1; +} From 5f6691842a1c7a19cdebdc3259604fb4fb99743c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 27 Nov 2024 09:09:18 +0800 Subject: [PATCH 2/5] Update Solution2.js --- solution/2900-2999/2924.Find Champion II/Solution2.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/solution/2900-2999/2924.Find Champion II/Solution2.js b/solution/2900-2999/2924.Find Champion II/Solution2.js index 1ff3ff21e5c69..3a6d88aa90675 100644 --- a/solution/2900-2999/2924.Find Champion II/Solution2.js +++ b/solution/2900-2999/2924.Find Champion II/Solution2.js @@ -1,9 +1,7 @@ function findChampion(n, edges) { - const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); - + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); for (const [_, v] of edges) { vertexes.delete(v); } - return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; } From 190798fcac91064cd460b26a51a597dca0262c45 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 27 Nov 2024 09:09:46 +0800 Subject: [PATCH 3/5] Update README.md --- solution/2900-2999/2924.Find Champion II/README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/solution/2900-2999/2924.Find Champion II/README.md b/solution/2900-2999/2924.Find Champion II/README.md index 29a1889a2874b..e5f825678c0bd 100644 --- a/solution/2900-2999/2924.Find Champion II/README.md +++ b/solution/2900-2999/2924.Find Champion II/README.md @@ -229,12 +229,10 @@ function findChampion(n: number, edges: number[][]): number { ```js function findChampion(n, edges) { - const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); - + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); for (const [_, v] of edges) { vertexes.delete(v); } - return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; } ``` From 661b35dc4227fae834027132333173e921eb20d2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 27 Nov 2024 09:10:08 +0800 Subject: [PATCH 4/5] Update README.md --- solution/2900-2999/2924.Find Champion II/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solution/2900-2999/2924.Find Champion II/README.md b/solution/2900-2999/2924.Find Champion II/README.md index e5f825678c0bd..7968945e547f5 100644 --- a/solution/2900-2999/2924.Find Champion II/README.md +++ b/solution/2900-2999/2924.Find Champion II/README.md @@ -207,7 +207,7 @@ function findChampion(n, edges) { -### Solution 2 +### 方法二 From eb9d6ff73da30cffe167aa5ab24d3664e7885116 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 27 Nov 2024 09:10:30 +0800 Subject: [PATCH 5/5] Update README_EN.md --- solution/2900-2999/2924.Find Champion II/README_EN.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/solution/2900-2999/2924.Find Champion II/README_EN.md b/solution/2900-2999/2924.Find Champion II/README_EN.md index f7c66061a9baa..46cc175366d7e 100644 --- a/solution/2900-2999/2924.Find Champion II/README_EN.md +++ b/solution/2900-2999/2924.Find Champion II/README_EN.md @@ -227,12 +227,10 @@ function findChampion(n: number, edges: number[][]): number { ```js function findChampion(n, edges) { - const vertexes = new Set() < number > Array.from({ length: n }, (_, i) => i); - + const vertexes = new Set(Array.from({ length: n }, (_, i) => i)); for (const [_, v] of edges) { vertexes.delete(v); } - return vertexes.size === 1 ? vertexes[Symbol.iterator]().next().value : -1; } ```