From 28e086011918529fa871be8e225b4435c21d28d3 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Mon, 15 Jan 2024 12:26:02 +0000 Subject: [PATCH] feat: add js/ts solution to lc problem: No.2225 No.2225.Find Players With Zero or One Losses --- .../README.md | 48 +++++++++++++++++++ .../README_EN.md | 48 +++++++++++++++++++ .../Solution.js | 35 +++++++------- .../Solution.ts | 16 +++++++ 4 files changed, 128 insertions(+), 19 deletions(-) create mode 100644 solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.ts diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md b/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md index 6c05ddab6a731..50e92760d9ec9 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/README.md @@ -167,9 +167,57 @@ func findWinners(matches [][]int) [][]int { } ``` +### **TypeScript** + +```ts +function findWinners(matches: number[][]): number[][] { + const cnt: Map = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans: number[][] = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); + } + } + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; +} +``` + ### **JavaScript** ```js +/** + * @param {number[][]} matches + * @return {number[][]} + */ +var findWinners = function (matches) { + const cnt = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); + } + } + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; +}; +``` + +```js +/** + * @param {number[][]} matches + * @return {number[][]} + */ var findWinners = function (matches) { const onlyWins = new Set(), oneLose = new Set(), diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/README_EN.md b/solution/2200-2299/2225.Find Players With Zero or One Losses/README_EN.md index 1d6d6d2d886f6..e2facd09c0171 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/README_EN.md +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/README_EN.md @@ -155,9 +155,57 @@ func findWinners(matches [][]int) [][]int { } ``` +### **TypeScript** + +```ts +function findWinners(matches: number[][]): number[][] { + const cnt: Map = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans: number[][] = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); + } + } + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; +} +``` + ### **JavaScript** ```js +/** + * @param {number[][]} matches + * @return {number[][]} + */ +var findWinners = function (matches) { + const cnt = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); + } + } + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; +}; +``` + +```js +/** + * @param {number[][]} matches + * @return {number[][]} + */ var findWinners = function (matches) { const onlyWins = new Set(), oneLose = new Set(), diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.js b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.js index 95b4e5be4f721..292001b599bed 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.js +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.js @@ -1,23 +1,20 @@ +/** + * @param {number[][]} matches + * @return {number[][]} + */ var findWinners = function (matches) { - const onlyWins = new Set(), - oneLose = new Set(), - moreLosses = new Set(); - - for (const [winner, loser] of matches) { - if (!moreLosses.has(loser)) { - if (oneLose.has(loser)) { - oneLose.delete(loser); - moreLosses.add(loser); - } else { - onlyWins.delete(loser); - oneLose.add(loser); - } - } - - if (!moreLosses.has(winner) && !oneLose.has(winner)) { - onlyWins.add(winner); + const cnt = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); } } - - return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)]; + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; }; diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.ts b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.ts new file mode 100644 index 0000000000000..fef448bf32ccf --- /dev/null +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.ts @@ -0,0 +1,16 @@ +function findWinners(matches: number[][]): number[][] { + const cnt: Map = new Map(); + for (const [a, b] of matches) { + cnt.set(a, cnt.has(a) ? cnt.get(a) : 0); + cnt.set(b, (cnt.get(b) || 0) + 1); + } + const ans: number[][] = [[], []]; + for (let [u, v] of cnt.entries()) { + if (v < 2) { + ans[v].push(u); + } + } + ans[0].sort((a, b) => a - b); + ans[1].sort((a, b) => a - b); + return ans; +}