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 4a5522ee74b42..cf0e35f06dc3a 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 @@ -82,7 +82,15 @@ tags: -### 方法一:哈希表 +### 方法一:哈希表 + 排序 + +我们用一个哈希表 $\text{cnt}$ 记录每个玩家输掉的比赛场次。 + +然后遍历哈希表,将输掉 $0$ 场比赛的玩家放入 $\text{ans}[0]$,将输掉 $1$ 场比赛的玩家放入 $\text{ans}[1]$。 + +最后将 $\text{ans}[0]$ 和 $\text{ans}[1]$ 按照升序排序,返回结果。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为比赛场次数。 @@ -92,16 +100,14 @@ tags: class Solution: def findWinners(self, matches: List[List[int]]) -> List[List[int]]: cnt = Counter() - for a, b in matches: - if a not in cnt: - cnt[a] = 0 - cnt[b] += 1 + for winner, loser in matches: + if winner not in cnt: + cnt[winner] = 0 + cnt[loser] += 1 ans = [[], []] - for u, v in cnt.items(): + for x, v in sorted(cnt.items()): if v < 2: - ans[v].append(u) - ans[0].sort() - ans[1].sort() + ans[v].append(x) return ans ``` @@ -111,19 +117,14 @@ class Solution: class Solution { public List> findWinners(int[][] matches) { Map cnt = new HashMap<>(); - for (int[] m : matches) { - int a = m[0], b = m[1]; - cnt.putIfAbsent(a, 0); - cnt.put(b, cnt.getOrDefault(b, 0) + 1); + for (var e : matches) { + cnt.putIfAbsent(e[0], 0); + cnt.merge(e[1], 1, Integer::sum); } - List> ans = new ArrayList<>(); - ans.add(new ArrayList<>()); - ans.add(new ArrayList<>()); - for (Map.Entry entry : cnt.entrySet()) { - int u = entry.getKey(); - int v = entry.getValue(); - if (v < 2) { - ans.get(v).add(u); + List> ans = List.of(new ArrayList<>(), new ArrayList<>()); + for (var e : cnt.entrySet()) { + if (e.getValue() < 2) { + ans.get(e.getValue()).add(e.getKey()); } } Collections.sort(ans.get(0)); @@ -139,18 +140,19 @@ class Solution { class Solution { public: vector> findWinners(vector>& matches) { - unordered_map cnt; - for (auto& m : matches) { - int a = m[0], b = m[1]; - if (!cnt.count(a)) cnt[a] = 0; - ++cnt[b]; + map cnt; + for (auto& e : matches) { + if (!cnt.contains(e[0])) { + cnt[e[0]] = 0; + } + ++cnt[e[1]]; } vector> ans(2); - for (auto& [u, v] : cnt) { - if (v < 2) ans[v].push_back(u); + for (auto& [x, v] : cnt) { + if (v < 2) { + ans[v].push_back(x); + } } - sort(ans[0].begin(), ans[0].end()); - sort(ans[1].begin(), ans[1].end()); return ans; } }; @@ -161,17 +163,16 @@ public: ```go func findWinners(matches [][]int) [][]int { cnt := map[int]int{} - for _, m := range matches { - a, b := m[0], m[1] - if _, ok := cnt[a]; !ok { - cnt[a] = 0 + for _, e := range matches { + if _, ok := cnt[e[0]]; !ok { + cnt[e[0]] = 0 } - cnt[b]++ + cnt[e[1]]++ } ans := make([][]int, 2) - for u, v := range cnt { + for x, v := range cnt { if v < 2 { - ans[v] = append(ans[v], u) + ans[v] = append(ans[v], x) } } sort.Ints(ans[0]) @@ -185,14 +186,16 @@ func findWinners(matches [][]int) [][]int { ```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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans: number[][] = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); @@ -210,14 +213,16 @@ function findWinners(matches: number[][]): 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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); @@ -230,46 +235,4 @@ var findWinners = function (matches) { - - -### 方法二 - - - -#### JavaScript - -```js -/** - * @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); - } - } - - return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)]; -}; -``` - - - - - 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 9213f286c6d58..f588fa67165a0 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 @@ -80,7 +80,15 @@ Thus, answer[0] = [1,2,5,6] and answer[1] = []. -### Solution 1 +### Solution 1: Hash Table + Sorting + +We use a hash table `cnt` to record the number of matches each player has lost. + +Then we traverse the hash table, put the players who lost 0 matches into `ans[0]`, and put the players who lost 1 match into `ans[1]`. + +Finally, we sort `ans[0]` and `ans[1]` in ascending order and return the result. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of matches. @@ -90,16 +98,14 @@ Thus, answer[0] = [1,2,5,6] and answer[1] = []. class Solution: def findWinners(self, matches: List[List[int]]) -> List[List[int]]: cnt = Counter() - for a, b in matches: - if a not in cnt: - cnt[a] = 0 - cnt[b] += 1 + for winner, loser in matches: + if winner not in cnt: + cnt[winner] = 0 + cnt[loser] += 1 ans = [[], []] - for u, v in cnt.items(): + for x, v in sorted(cnt.items()): if v < 2: - ans[v].append(u) - ans[0].sort() - ans[1].sort() + ans[v].append(x) return ans ``` @@ -109,19 +115,14 @@ class Solution: class Solution { public List> findWinners(int[][] matches) { Map cnt = new HashMap<>(); - for (int[] m : matches) { - int a = m[0], b = m[1]; - cnt.putIfAbsent(a, 0); - cnt.put(b, cnt.getOrDefault(b, 0) + 1); + for (var e : matches) { + cnt.putIfAbsent(e[0], 0); + cnt.merge(e[1], 1, Integer::sum); } - List> ans = new ArrayList<>(); - ans.add(new ArrayList<>()); - ans.add(new ArrayList<>()); - for (Map.Entry entry : cnt.entrySet()) { - int u = entry.getKey(); - int v = entry.getValue(); - if (v < 2) { - ans.get(v).add(u); + List> ans = List.of(new ArrayList<>(), new ArrayList<>()); + for (var e : cnt.entrySet()) { + if (e.getValue() < 2) { + ans.get(e.getValue()).add(e.getKey()); } } Collections.sort(ans.get(0)); @@ -137,18 +138,19 @@ class Solution { class Solution { public: vector> findWinners(vector>& matches) { - unordered_map cnt; - for (auto& m : matches) { - int a = m[0], b = m[1]; - if (!cnt.count(a)) cnt[a] = 0; - ++cnt[b]; + map cnt; + for (auto& e : matches) { + if (!cnt.contains(e[0])) { + cnt[e[0]] = 0; + } + ++cnt[e[1]]; } vector> ans(2); - for (auto& [u, v] : cnt) { - if (v < 2) ans[v].push_back(u); + for (auto& [x, v] : cnt) { + if (v < 2) { + ans[v].push_back(x); + } } - sort(ans[0].begin(), ans[0].end()); - sort(ans[1].begin(), ans[1].end()); return ans; } }; @@ -159,17 +161,16 @@ public: ```go func findWinners(matches [][]int) [][]int { cnt := map[int]int{} - for _, m := range matches { - a, b := m[0], m[1] - if _, ok := cnt[a]; !ok { - cnt[a] = 0 + for _, e := range matches { + if _, ok := cnt[e[0]]; !ok { + cnt[e[0]] = 0 } - cnt[b]++ + cnt[e[1]]++ } ans := make([][]int, 2) - for u, v := range cnt { + for x, v := range cnt { if v < 2 { - ans[v] = append(ans[v], u) + ans[v] = append(ans[v], x) } } sort.Ints(ans[0]) @@ -183,14 +184,16 @@ func findWinners(matches [][]int) [][]int { ```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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans: number[][] = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); @@ -208,14 +211,16 @@ function findWinners(matches: number[][]): 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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); @@ -228,46 +233,4 @@ var findWinners = function (matches) { - - -### Solution 2 - - - -#### JavaScript - -```js -/** - * @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); - } - } - - return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)]; -}; -``` - - - - - diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp index 3771d8402358a..0e7d36e0601af 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp @@ -1,18 +1,19 @@ class Solution { public: vector> findWinners(vector>& matches) { - unordered_map cnt; - for (auto& m : matches) { - int a = m[0], b = m[1]; - if (!cnt.count(a)) cnt[a] = 0; - ++cnt[b]; + map cnt; + for (auto& e : matches) { + if (!cnt.contains(e[0])) { + cnt[e[0]] = 0; + } + ++cnt[e[1]]; } vector> ans(2); - for (auto& [u, v] : cnt) { - if (v < 2) ans[v].push_back(u); + for (auto& [x, v] : cnt) { + if (v < 2) { + ans[v].push_back(x); + } } - sort(ans[0].begin(), ans[0].end()); - sort(ans[1].begin(), ans[1].end()); return ans; } }; \ No newline at end of file diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.go b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.go index a0aaa6e8a0ed1..0a7b19410c00e 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.go +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.go @@ -1,16 +1,15 @@ func findWinners(matches [][]int) [][]int { cnt := map[int]int{} - for _, m := range matches { - a, b := m[0], m[1] - if _, ok := cnt[a]; !ok { - cnt[a] = 0 + for _, e := range matches { + if _, ok := cnt[e[0]]; !ok { + cnt[e[0]] = 0 } - cnt[b]++ + cnt[e[1]]++ } ans := make([][]int, 2) - for u, v := range cnt { + for x, v := range cnt { if v < 2 { - ans[v] = append(ans[v], u) + ans[v] = append(ans[v], x) } } sort.Ints(ans[0]) diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.java b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.java index 83aa756ab701d..0c467995f6ad0 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.java +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.java @@ -1,19 +1,14 @@ class Solution { public List> findWinners(int[][] matches) { Map cnt = new HashMap<>(); - for (int[] m : matches) { - int a = m[0], b = m[1]; - cnt.putIfAbsent(a, 0); - cnt.put(b, cnt.getOrDefault(b, 0) + 1); + for (var e : matches) { + cnt.putIfAbsent(e[0], 0); + cnt.merge(e[1], 1, Integer::sum); } - List> ans = new ArrayList<>(); - ans.add(new ArrayList<>()); - ans.add(new ArrayList<>()); - for (Map.Entry entry : cnt.entrySet()) { - int u = entry.getKey(); - int v = entry.getValue(); - if (v < 2) { - ans.get(v).add(u); + List> ans = List.of(new ArrayList<>(), new ArrayList<>()); + for (var e : cnt.entrySet()) { + if (e.getValue() < 2) { + ans.get(e.getValue()).add(e.getKey()); } } Collections.sort(ans.get(0)); 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 292001b599bed..7b191400739fa 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 @@ -4,14 +4,16 @@ */ 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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.py b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.py index f57340d610396..852d732458990 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.py +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.py @@ -1,14 +1,12 @@ class Solution: def findWinners(self, matches: List[List[int]]) -> List[List[int]]: cnt = Counter() - for a, b in matches: - if a not in cnt: - cnt[a] = 0 - cnt[b] += 1 + for winner, loser in matches: + if winner not in cnt: + cnt[winner] = 0 + cnt[loser] += 1 ans = [[], []] - for u, v in cnt.items(): + for x, v in sorted(cnt.items()): if v < 2: - ans[v].append(u) - ans[0].sort() - ans[1].sort() + ans[v].append(x) 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 index fef448bf32ccf..d71155bb1e879 100644 --- 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 @@ -1,13 +1,15 @@ 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); + for (const [winner, loser] of matches) { + if (!cnt.has(winner)) { + cnt.set(winner, 0); + } + cnt.set(loser, (cnt.get(loser) || 0) + 1); } const ans: number[][] = [[], []]; - for (let [u, v] of cnt.entries()) { + for (const [x, v] of cnt) { if (v < 2) { - ans[v].push(u); + ans[v].push(x); } } ans[0].sort((a, b) => a - b); diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution2.js b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution2.js deleted file mode 100644 index 622088640a253..0000000000000 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution2.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @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); - } - } - - return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)]; -}; diff --git a/solution/2700-2799/2705.Compact Object/README.md b/solution/2700-2799/2705.Compact Object/README.md index 159bfabead37e..b367915c2e754 100644 --- a/solution/2700-2799/2705.Compact Object/README.md +++ b/solution/2700-2799/2705.Compact Object/README.md @@ -85,9 +85,8 @@ function compactObject(obj: Obj): Obj { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {} as Obj); @@ -109,9 +108,8 @@ var compactObject = function (obj) { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {}); diff --git a/solution/2700-2799/2705.Compact Object/README_EN.md b/solution/2700-2799/2705.Compact Object/README_EN.md index 6397039224717..746b47f131101 100644 --- a/solution/2700-2799/2705.Compact Object/README_EN.md +++ b/solution/2700-2799/2705.Compact Object/README_EN.md @@ -83,9 +83,8 @@ function compactObject(obj: Obj): Obj { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {} as Obj); @@ -107,9 +106,8 @@ var compactObject = function (obj) { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {}); diff --git a/solution/2700-2799/2705.Compact Object/Solution.js b/solution/2700-2799/2705.Compact Object/Solution.js index eaecd7e3342e4..5ec1ade0674a1 100644 --- a/solution/2700-2799/2705.Compact Object/Solution.js +++ b/solution/2700-2799/2705.Compact Object/Solution.js @@ -10,9 +10,8 @@ var compactObject = function (obj) { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {}); diff --git a/solution/2700-2799/2705.Compact Object/Solution.ts b/solution/2700-2799/2705.Compact Object/Solution.ts index 612e63ca509b3..e911751578f53 100644 --- a/solution/2700-2799/2705.Compact Object/Solution.ts +++ b/solution/2700-2799/2705.Compact Object/Solution.ts @@ -8,9 +8,8 @@ function compactObject(obj: Obj): Obj { return obj.filter(Boolean).map(compactObject); } return Object.entries(obj).reduce((acc, [key, value]) => { - const compactedValue = compactObject(value); - if (compactedValue) { - acc[key] = compactedValue; + if (value) { + acc[key] = compactObject(value); } return acc; }, {} as Obj);