Skip to content

Commit 5ad54a5

Browse files
add 1366
1 parent 6bd8336 commit 5ad54a5

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium||DP, Linked List, Tree
12+
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1366.java) | |Medium||Array, Sort
1213
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1365.java) | |Easy||Array, HashTable
1314
|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1362.java) | |Medium||Math
1415
|1361|[Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1361.java) | |Medium||Graph
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
6+
/**
7+
* 1366. Rank Teams by Votes
8+
*
9+
* In a special ranking system, each voter gives a rank from highest to lowest to all teams participated in the competition.
10+
* The ordering of teams is decided by who received the most position-one votes.
11+
* If two or more teams tie in the first position, we consider the second position to resolve the conflict,
12+
* if they tie again, we continue this process until the ties are resolved. If two or more teams are still tied after considering all positions,
13+
* we rank them alphabetically based on their team letter.
14+
* Given an array of strings votes which is the votes of all voters in the ranking systems. Sort all teams according to the ranking system described above.
15+
* Return a string of all teams sorted by the ranking system.
16+
*
17+
* Example 1:
18+
* Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
19+
* Output: "ACB"
20+
* Explanation: Team A was ranked first place by 5 voters. No other team was voted as first place so team A is the first team.
21+
* Team B was ranked second by 2 voters and was ranked third by 3 voters.
22+
* Team C was ranked second by 3 voters and was ranked third by 2 voters.
23+
* As most of the voters ranked C second, team C is the second team and team B is the third.
24+
*
25+
* Example 2:
26+
* Input: votes = ["WXYZ","XYZW"]
27+
* Output: "XWYZ"
28+
* Explanation: X is the winner due to tie-breaking rule.
29+
* X has same votes as W for the first position but X has one vote as second position while W doesn't have any votes as second position.
30+
*
31+
* Example 3:
32+
* Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
33+
* Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
34+
* Explanation: Only one voter so his votes are used for the ranking.
35+
*
36+
* Example 4:
37+
* Input: votes = ["BCA","CAB","CBA","ABC","ACB","BAC"]
38+
* Output: "ABC"
39+
* Explanation:
40+
* Team A was ranked first by 2 voters, second by 2 voters and third by 2 voters.
41+
* Team B was ranked first by 2 voters, second by 2 voters and third by 2 voters.
42+
* Team C was ranked first by 2 voters, second by 2 voters and third by 2 voters.
43+
* There is a tie and we rank teams ascending by their IDs.
44+
*
45+
* Example 5:
46+
* Input: votes = ["M","M","M","M"]
47+
* Output: "M"
48+
* Explanation: Only team M in the competition so it has the first rank.
49+
*
50+
* Constraints:
51+
* 1 <= votes.length <= 1000
52+
* 1 <= votes[i].length <= 26
53+
* votes[i].length == votes[j].length for 0 <= i, j < votes.length.
54+
* votes[i][j] is an English upper-case letter.
55+
* All characters of votes[i] are unique.
56+
* All the characters that occur in votes[0] also occur in votes[j] where 1 <= j < votes.length.
57+
* */
58+
public class _1366 {
59+
public static class Solution1 {
60+
class Node {
61+
int[] count = new int[26];
62+
char c;
63+
64+
public Node(char c) {
65+
this.c = c;
66+
}
67+
}
68+
69+
public String rankTeams(String[] votes) {
70+
Node[] nodes = new Node[26];
71+
for (int i = 0; i < 26; i++) {
72+
nodes[i] = new Node((char) (i + 'A'));
73+
}
74+
for (String vote : votes) {
75+
for (int i = 0; i < vote.length(); i++) {
76+
nodes[vote.charAt(i) - 'A'].count[i]++;
77+
}
78+
}
79+
Arrays.sort(nodes, new Comparator<Node>() {
80+
@Override
81+
public int compare(Node o1, Node o2) {
82+
for (int i = 0; i < 26; i++) {
83+
if (o1.count[i] != o2.count[i]) {
84+
return o2.count[i] - o1.count[i];
85+
}
86+
}
87+
return o1.c - o2.c;
88+
}
89+
});
90+
StringBuilder sb = new StringBuilder();
91+
for (int i = 0; i < votes[0].length(); i++) {
92+
sb.append(nodes[i].c);
93+
}
94+
return sb.toString();
95+
}
96+
97+
}
98+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1366;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1366Test {
10+
private static _1366.Solution1 solution1;
11+
private static String[] votes;
12+
13+
@Test
14+
public void test1() {
15+
solution1 = new _1366.Solution1();
16+
votes = new String[]{"ABC", "ACB", "ABC", "ACB", "ACB"};
17+
assertEquals("ACB", solution1.rankTeams(votes));
18+
}
19+
20+
@Test
21+
public void test2() {
22+
solution1 = new _1366.Solution1();
23+
votes = new String[]{"WXYZ", "XYZW"};
24+
assertEquals("XWYZ", solution1.rankTeams(votes));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
solution1 = new _1366.Solution1();
30+
votes = new String[]{"ZMNAGUEDSJYLBOPHRQICWFXTVK"};
31+
assertEquals("ZMNAGUEDSJYLBOPHRQICWFXTVK", solution1.rankTeams(votes));
32+
}
33+
34+
@Test
35+
public void test4() {
36+
solution1 = new _1366.Solution1();
37+
votes = new String[]{"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"};
38+
assertEquals("ABC", solution1.rankTeams(votes));
39+
}
40+
41+
@Test
42+
public void test5() {
43+
solution1 = new _1366.Solution1();
44+
votes = new String[]{"M", "M", "M", "M"};
45+
assertEquals("M", solution1.rankTeams(votes));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)