|
| 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 | +} |
0 commit comments