Skip to content

Commit 5dec785

Browse files
committed
completed 2225 - find players with zero or one losses in Go
1 parent 8895d6f commit 5dec785

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 2225. Find Players with Zero or One Losses
2+
3+
You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
4+
5+
Return a list answer of size 2 where:
6+
7+
answer[0] is a list of all players that have not lost any matches.
8+
answer[1] is a list of all players that have lost exactly one match.
9+
The values in the two lists should be returned in increasing order.
10+
11+
Note:
12+
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+
Example 1:
18+
19+
Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
20+
Output: [[1,2,10],[4,5,7,8]]
21+
Explanation:
22+
Players 1, 2, and 10 have not lost any matches.
23+
Players 4, 5, 7, and 8 each have lost one match.
24+
Players 3, 6, and 9 each have lost two matches.
25+
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
26+
27+
28+
Example 2:
29+
30+
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
31+
Output: [[1,2,5,6],[]]
32+
Explanation:
33+
Players 1, 2, 5, and 6 have not lost any matches.
34+
Players 3 and 4 each have lost two matches.
35+
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
36+
37+
38+
Constraints:
39+
40+
1 <= matches.length <= 105
41+
matches[i].length == 2
42+
1 <= winneri, loseri <= 105
43+
w inneri != loseri
44+
All matches[i] are unique.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module findPlayersWithZeroOrOneLosses
2+
3+
go 1.23.2
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package solution
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestWinnders(t *testing.T) {
9+
t.Run("test case one", func(t *testing.T) {
10+
matches := [][]int{{1, 3}, {2, 3}, {3, 6}, {5, 6}, {5, 7}, {4, 5}, {4, 8}, {4, 9}, {10, 4}, {10, 9}}
11+
wanted := [][]int{{1, 2, 10}, {4, 5, 7, 8}}
12+
13+
result := findWinners(matches)
14+
15+
if !reflect.DeepEqual(result, wanted) {
16+
t.Errorf("got %v, wanted %v", result, wanted)
17+
}
18+
})
19+
20+
t.Run("test case two", func(t *testing.T) {
21+
matches := [][]int{{1, 2}, {1, 3}, {2, 3}, {2, 4}}
22+
wanted := [][]int{{1}, {2, 4}}
23+
24+
result := findWinners(matches)
25+
26+
if !reflect.DeepEqual(result, wanted) {
27+
t.Errorf("got %v, wanted %v", result, wanted)
28+
}
29+
})
30+
31+
t.Run("test case three", func(t *testing.T) {
32+
matches := [][]int{{1, 2}, {2, 1}, {1, 2}, {2, 1}}
33+
wanted := [][]int{{}, {}}
34+
35+
result := findWinners(matches)
36+
37+
if !reflect.DeepEqual(result, wanted) {
38+
t.Errorf("got %v, wanted %v", result, wanted)
39+
}
40+
})
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package solution
2+
3+
import "slices"
4+
5+
func findWinners(matches [][]int) [][]int {
6+
losses := make(map[int]int)
7+
8+
// iterate over each match in matches
9+
for _, match := range matches {
10+
// take the first element as winner, second as loser
11+
winner, loser := match[0], match[1]
12+
13+
// check if the winner is already in the losses map
14+
if _, ok := losses[winner]; !ok {
15+
// first time seeing this winner
16+
losses[winner] = 0
17+
}
18+
19+
// increment the loss count for the loser
20+
losses[loser]++
21+
}
22+
23+
// slice for winners with no losses
24+
noLoss := make([]int, 0)
25+
// slice for winners with one loss
26+
oneLoss := make([]int, 0)
27+
28+
// categorize players based on their losses
29+
for player, lossCount := range losses {
30+
if lossCount == 0 {
31+
noLoss = append(noLoss, player)
32+
} else if lossCount == 1 {
33+
oneLoss = append(oneLoss, player)
34+
}
35+
}
36+
37+
// sort both slices
38+
slices.Sort(noLoss)
39+
slices.Sort(oneLoss)
40+
41+
// return the result as a 2D slice
42+
return [][]int{noLoss, oneLoss}
43+
}

0 commit comments

Comments
 (0)