Skip to content

Commit 200bbce

Browse files
authoredMay 22, 2024
feat: update solutions to lc problems: No.2225,2705 (doocs#2873)
* No.2225.Find Players With Zero or One Losses * No.2705.Compact Object
1 parent c04d5e2 commit 200bbce

File tree

13 files changed

+159
-269
lines changed

13 files changed

+159
-269
lines changed
 

‎solution/2200-2299/2225.Find Players With Zero or One Losses/README.md

+52-89
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,15 @@ tags:
8282

8383
<!-- solution:start -->
8484

85-
### 方法一:哈希表
85+
### 方法一:哈希表 + 排序
86+
87+
我们用一个哈希表 $\text{cnt}$ 记录每个玩家输掉的比赛场次。
88+
89+
然后遍历哈希表,将输掉 $0$ 场比赛的玩家放入 $\text{ans}[0]$,将输掉 $1$ 场比赛的玩家放入 $\text{ans}[1]$。
90+
91+
最后将 $\text{ans}[0]$ 和 $\text{ans}[1]$ 按照升序排序,返回结果。
92+
93+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为比赛场次数。
8694

8795
<!-- tabs:start -->
8896

@@ -92,16 +100,14 @@ tags:
92100
class Solution:
93101
def findWinners(self, matches: List[List[int]]) -> List[List[int]]:
94102
cnt = Counter()
95-
for a, b in matches:
96-
if a not in cnt:
97-
cnt[a] = 0
98-
cnt[b] += 1
103+
for winner, loser in matches:
104+
if winner not in cnt:
105+
cnt[winner] = 0
106+
cnt[loser] += 1
99107
ans = [[], []]
100-
for u, v in cnt.items():
108+
for x, v in sorted(cnt.items()):
101109
if v < 2:
102-
ans[v].append(u)
103-
ans[0].sort()
104-
ans[1].sort()
110+
ans[v].append(x)
105111
return ans
106112
```
107113

@@ -111,19 +117,14 @@ class Solution:
111117
class Solution {
112118
public List<List<Integer>> findWinners(int[][] matches) {
113119
Map<Integer, Integer> cnt = new HashMap<>();
114-
for (int[] m : matches) {
115-
int a = m[0], b = m[1];
116-
cnt.putIfAbsent(a, 0);
117-
cnt.put(b, cnt.getOrDefault(b, 0) + 1);
120+
for (var e : matches) {
121+
cnt.putIfAbsent(e[0], 0);
122+
cnt.merge(e[1], 1, Integer::sum);
118123
}
119-
List<List<Integer>> ans = new ArrayList<>();
120-
ans.add(new ArrayList<>());
121-
ans.add(new ArrayList<>());
122-
for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
123-
int u = entry.getKey();
124-
int v = entry.getValue();
125-
if (v < 2) {
126-
ans.get(v).add(u);
124+
List<List<Integer>> ans = List.of(new ArrayList<>(), new ArrayList<>());
125+
for (var e : cnt.entrySet()) {
126+
if (e.getValue() < 2) {
127+
ans.get(e.getValue()).add(e.getKey());
127128
}
128129
}
129130
Collections.sort(ans.get(0));
@@ -139,18 +140,19 @@ class Solution {
139140
class Solution {
140141
public:
141142
vector<vector<int>> findWinners(vector<vector<int>>& matches) {
142-
unordered_map<int, int> cnt;
143-
for (auto& m : matches) {
144-
int a = m[0], b = m[1];
145-
if (!cnt.count(a)) cnt[a] = 0;
146-
++cnt[b];
143+
map<int, int> cnt;
144+
for (auto& e : matches) {
145+
if (!cnt.contains(e[0])) {
146+
cnt[e[0]] = 0;
147+
}
148+
++cnt[e[1]];
147149
}
148150
vector<vector<int>> ans(2);
149-
for (auto& [u, v] : cnt) {
150-
if (v < 2) ans[v].push_back(u);
151+
for (auto& [x, v] : cnt) {
152+
if (v < 2) {
153+
ans[v].push_back(x);
154+
}
151155
}
152-
sort(ans[0].begin(), ans[0].end());
153-
sort(ans[1].begin(), ans[1].end());
154156
return ans;
155157
}
156158
};
@@ -161,17 +163,16 @@ public:
161163
```go
162164
func findWinners(matches [][]int) [][]int {
163165
cnt := map[int]int{}
164-
for _, m := range matches {
165-
a, b := m[0], m[1]
166-
if _, ok := cnt[a]; !ok {
167-
cnt[a] = 0
166+
for _, e := range matches {
167+
if _, ok := cnt[e[0]]; !ok {
168+
cnt[e[0]] = 0
168169
}
169-
cnt[b]++
170+
cnt[e[1]]++
170171
}
171172
ans := make([][]int, 2)
172-
for u, v := range cnt {
173+
for x, v := range cnt {
173174
if v < 2 {
174-
ans[v] = append(ans[v], u)
175+
ans[v] = append(ans[v], x)
175176
}
176177
}
177178
sort.Ints(ans[0])
@@ -185,14 +186,16 @@ func findWinners(matches [][]int) [][]int {
185186
```ts
186187
function findWinners(matches: number[][]): number[][] {
187188
const cnt: Map<number, number> = new Map();
188-
for (const [a, b] of matches) {
189-
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
190-
cnt.set(b, (cnt.get(b) || 0) + 1);
189+
for (const [winner, loser] of matches) {
190+
if (!cnt.has(winner)) {
191+
cnt.set(winner, 0);
192+
}
193+
cnt.set(loser, (cnt.get(loser) || 0) + 1);
191194
}
192195
const ans: number[][] = [[], []];
193-
for (let [u, v] of cnt.entries()) {
196+
for (const [x, v] of cnt) {
194197
if (v < 2) {
195-
ans[v].push(u);
198+
ans[v].push(x);
196199
}
197200
}
198201
ans[0].sort((a, b) => a - b);
@@ -210,14 +213,16 @@ function findWinners(matches: number[][]): number[][] {
210213
*/
211214
var findWinners = function (matches) {
212215
const cnt = new Map();
213-
for (const [a, b] of matches) {
214-
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
215-
cnt.set(b, (cnt.get(b) || 0) + 1);
216+
for (const [winner, loser] of matches) {
217+
if (!cnt.has(winner)) {
218+
cnt.set(winner, 0);
219+
}
220+
cnt.set(loser, (cnt.get(loser) || 0) + 1);
216221
}
217222
const ans = [[], []];
218-
for (let [u, v] of cnt.entries()) {
223+
for (const [x, v] of cnt) {
219224
if (v < 2) {
220-
ans[v].push(u);
225+
ans[v].push(x);
221226
}
222227
}
223228
ans[0].sort((a, b) => a - b);
@@ -230,46 +235,4 @@ var findWinners = function (matches) {
230235

231236
<!-- solution:end -->
232237

233-
<!-- solution:start -->
234-
235-
### 方法二
236-
237-
<!-- tabs:start -->
238-
239-
#### JavaScript
240-
241-
```js
242-
/**
243-
* @param {number[][]} matches
244-
* @return {number[][]}
245-
*/
246-
var findWinners = function (matches) {
247-
const onlyWins = new Set(),
248-
oneLose = new Set(),
249-
moreLosses = new Set();
250-
251-
for (const [winner, loser] of matches) {
252-
if (!moreLosses.has(loser)) {
253-
if (oneLose.has(loser)) {
254-
oneLose.delete(loser);
255-
moreLosses.add(loser);
256-
} else {
257-
onlyWins.delete(loser);
258-
oneLose.add(loser);
259-
}
260-
}
261-
262-
if (!moreLosses.has(winner) && !oneLose.has(winner)) {
263-
onlyWins.add(winner);
264-
}
265-
}
266-
267-
return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)];
268-
};
269-
```
270-
271-
<!-- tabs:end -->
272-
273-
<!-- solution:end -->
274-
275238
<!-- problem:end -->

0 commit comments

Comments
 (0)
Please sign in to comment.