Skip to content

Commit 1e0e364

Browse files
committed
added problem 2225
1 parent d8c4ee4 commit 1e0e364

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem2225
2+
3+
import "sort"
4+
5+
/*
6+
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
7+
Return a list answer of size 2 where:
8+
answer[0] is a list of all players that have not lost any matches.
9+
answer[1] is a list of all players that have lost exactly one match.
10+
The values in the two lists should be returned in increasing order.
11+
12+
Note:
13+
You should only consider the players that have played at least one match.
14+
The testcases will be generated such that no two matches will have the same outcome.
15+
*/
16+
17+
func findWinners(matches [][]int) [][]int {
18+
var results = [][]int{{}, {}}
19+
var losses = make(map[int]int)
20+
for i := range matches {
21+
win := losses[matches[i][0]]
22+
losses[matches[i][0]] = win
23+
losses[matches[i][1]]++
24+
}
25+
for k, v := range losses {
26+
if v == 0 {
27+
results[0] = append(results[0], k)
28+
} else if v == 1 {
29+
results[1] = append(results[1], k)
30+
}
31+
}
32+
sort.Ints(results[0])
33+
sort.Ints(results[1])
34+
return results
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem2225
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input [][]int
12+
Expected [][]int
13+
}
14+
15+
var Results = []Result{
16+
{[][]int{{1, 3}, {2, 3}, {3, 6}, {5, 6}, {5, 7}, {4, 5}, {4, 8}, {4, 9}, {10, 4}, {10, 9}},
17+
[][]int{{1, 2, 10}, {4, 5, 7, 8}}},
18+
{[][]int{{2, 3}, {1, 3}, {5, 4}, {6, 4}},
19+
[][]int{{1, 2, 5, 6}, {}}},
20+
}
21+
22+
func TestFindWinners(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := findWinners(res.Input)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}
31+
32+
func BenchmarkFindWinners(b *testing.B) {
33+
for i := 0; i < b.N; i++ {
34+
for _, res := range Results {
35+
findWinners(res.Input)
36+
}
37+
}
38+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Each problem is in it's own directory, with test files. There are helper package
281281
| 2136 | [Earliest Possible Day of Full Bloom](https://leetcode.com/problems/earliest-possible-day-of-full-bloom) | [My Solution](./problems/problem2136) ||
282282
| 2148 | [Count Elements With Strictly Smaller and Greater Elements](https://leetcode.com/problems/count-elements-with-strictly-smaller-and-greater-elements) | [My Solution](./problems/problem2148) ||
283283
| 2165 | [Smallest Value of the Rearranged Number](https://leetcode.com/problems/smallest-value-of-the-rearranged-number/) | [My Solution](./problems/problem2165) ||
284+
| 2225 | [Find Players With Zero or One Losses](https://leetcode.com/problems/find-players-with-zero-or-one-losses/) | [My Solution](./problems/problem2225) ||
284285
| 2259 | [Remove Digit From Number to Maximize Result](https://leetcode.com/problems/remove-digit-from-number-to-maximize-result) | [My Solution](./problems/problem2259) ||
285286
| 2284 | [Sender With Largest Word Count](https://leetcode.com/problems/sender-with-largest-word-count) | [My Solution](./problems/problem2284) ||
286287
| 2413 | [Smallest Even Multiple](https://leetcode.com/problems/smallest-even-multiple) | [My Solution](./problems/problem2413) ||

0 commit comments

Comments
 (0)