-
Notifications
You must be signed in to change notification settings - Fork 0
/
TournamentWinner.java
31 lines (30 loc) · 1.03 KB
/
TournamentWinner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
class Program {
// O(n) time | O(k) space - where n is the number of competitions and k is the number of teams
public String tournamentWinner(
ArrayList<ArrayList<String>> competitions, ArrayList<Integer> results) {
// Write your code here.
HashMap<String, Integer> scoreboard = new HashMap<>();
for (int idx = 0; idx < results.size(); idx++) {
ArrayList<String> competition = competitions.get(idx);
int result = results.get(idx);
if (result == 0) {
String awayTeam = competition.get(1);
scoreboard.put(awayTeam, 3 + scoreboard.getOrDefault(awayTeam, 0));
} else if (result == 1) {
String homeTeam = competition.get(0);
scoreboard.put(homeTeam, 3 + scoreboard.getOrDefault(homeTeam, 0));
}
}
String winnerCandidate = "Nobody";
int maxScore = -1;
for (Map.Entry<String, Integer> entry : scoreboard.entrySet()) {
int score = entry.getValue();
if (score > maxScore) {
winnerCandidate = entry.getKey();
maxScore = score;
}
}
return winnerCandidate;
}
}