Skip to content

Commit 77f56e2

Browse files
authored
feat: add js/ts solution to lc problem: No.2225 (doocs#2224)
No.2225.Find Players With Zero or One Losses
1 parent 34024d7 commit 77f56e2

File tree

4 files changed

+128
-19
lines changed

4 files changed

+128
-19
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,57 @@ func findWinners(matches [][]int) [][]int {
167167
}
168168
```
169169

170+
### **TypeScript**
171+
172+
```ts
173+
function findWinners(matches: number[][]): number[][] {
174+
const cnt: Map<number, number> = new Map();
175+
for (const [a, b] of matches) {
176+
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
177+
cnt.set(b, (cnt.get(b) || 0) + 1);
178+
}
179+
const ans: number[][] = [[], []];
180+
for (let [u, v] of cnt.entries()) {
181+
if (v < 2) {
182+
ans[v].push(u);
183+
}
184+
}
185+
ans[0].sort((a, b) => a - b);
186+
ans[1].sort((a, b) => a - b);
187+
return ans;
188+
}
189+
```
190+
170191
### **JavaScript**
171192

172193
```js
194+
/**
195+
* @param {number[][]} matches
196+
* @return {number[][]}
197+
*/
198+
var findWinners = function (matches) {
199+
const cnt = new Map();
200+
for (const [a, b] of matches) {
201+
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
202+
cnt.set(b, (cnt.get(b) || 0) + 1);
203+
}
204+
const ans = [[], []];
205+
for (let [u, v] of cnt.entries()) {
206+
if (v < 2) {
207+
ans[v].push(u);
208+
}
209+
}
210+
ans[0].sort((a, b) => a - b);
211+
ans[1].sort((a, b) => a - b);
212+
return ans;
213+
};
214+
```
215+
216+
```js
217+
/**
218+
* @param {number[][]} matches
219+
* @return {number[][]}
220+
*/
173221
var findWinners = function (matches) {
174222
const onlyWins = new Set(),
175223
oneLose = new Set(),

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

+48
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,57 @@ func findWinners(matches [][]int) [][]int {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function findWinners(matches: number[][]): number[][] {
162+
const cnt: Map<number, number> = new Map();
163+
for (const [a, b] of matches) {
164+
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
165+
cnt.set(b, (cnt.get(b) || 0) + 1);
166+
}
167+
const ans: number[][] = [[], []];
168+
for (let [u, v] of cnt.entries()) {
169+
if (v < 2) {
170+
ans[v].push(u);
171+
}
172+
}
173+
ans[0].sort((a, b) => a - b);
174+
ans[1].sort((a, b) => a - b);
175+
return ans;
176+
}
177+
```
178+
158179
### **JavaScript**
159180

160181
```js
182+
/**
183+
* @param {number[][]} matches
184+
* @return {number[][]}
185+
*/
186+
var findWinners = function (matches) {
187+
const cnt = 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);
191+
}
192+
const ans = [[], []];
193+
for (let [u, v] of cnt.entries()) {
194+
if (v < 2) {
195+
ans[v].push(u);
196+
}
197+
}
198+
ans[0].sort((a, b) => a - b);
199+
ans[1].sort((a, b) => a - b);
200+
return ans;
201+
};
202+
```
203+
204+
```js
205+
/**
206+
* @param {number[][]} matches
207+
* @return {number[][]}
208+
*/
161209
var findWinners = function (matches) {
162210
const onlyWins = new Set(),
163211
oneLose = new Set(),
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1+
/**
2+
* @param {number[][]} matches
3+
* @return {number[][]}
4+
*/
15
var findWinners = function (matches) {
2-
const onlyWins = new Set(),
3-
oneLose = new Set(),
4-
moreLosses = new Set();
5-
6-
for (const [winner, loser] of matches) {
7-
if (!moreLosses.has(loser)) {
8-
if (oneLose.has(loser)) {
9-
oneLose.delete(loser);
10-
moreLosses.add(loser);
11-
} else {
12-
onlyWins.delete(loser);
13-
oneLose.add(loser);
14-
}
15-
}
16-
17-
if (!moreLosses.has(winner) && !oneLose.has(winner)) {
18-
onlyWins.add(winner);
6+
const cnt = new Map();
7+
for (const [a, b] of matches) {
8+
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
9+
cnt.set(b, (cnt.get(b) || 0) + 1);
10+
}
11+
const ans = [[], []];
12+
for (let [u, v] of cnt.entries()) {
13+
if (v < 2) {
14+
ans[v].push(u);
1915
}
2016
}
21-
22-
return [[...onlyWins].sort((a, b) => a - b), [...oneLose].sort((a, b) => a - b)];
17+
ans[0].sort((a, b) => a - b);
18+
ans[1].sort((a, b) => a - b);
19+
return ans;
2320
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findWinners(matches: number[][]): number[][] {
2+
const cnt: Map<number, number> = new Map();
3+
for (const [a, b] of matches) {
4+
cnt.set(a, cnt.has(a) ? cnt.get(a) : 0);
5+
cnt.set(b, (cnt.get(b) || 0) + 1);
6+
}
7+
const ans: number[][] = [[], []];
8+
for (let [u, v] of cnt.entries()) {
9+
if (v < 2) {
10+
ans[v].push(u);
11+
}
12+
}
13+
ans[0].sort((a, b) => a - b);
14+
ans[1].sort((a, b) => a - b);
15+
return ans;
16+
}

0 commit comments

Comments
 (0)