You are given an array of integers votes
where each integer represents the number of votes a candidate received. We need to rank the candidates by the number of votes they received from highest to lowest.
Return a string of all the candidates sorted by the number of votes they received. If two or more candidates receive the same number of votes, then order them alphabetically. For example, if votes = ["ABC", "ACB", "ABC", "ACB", "ACB"] then candidate A received 5 votes, candidate B received 2 votes, and candidate C received 3 votes.
The solution should be alphabetically ordered string. For example, if votes = ["ABC", "ACB", "ABC", "ACB", "ACB"] then return "ACB". If votes = ["WXYZ","XYZW"] then return "XWYZ".
Example 1:
Input: votes = ["ABC","ACB","ABC","ACB","ACB"]
Output: "ACB"
Explanation:
Candidate A receives 5 votes.
Candidate B receives 2 votes.
Candidate C receives 3 votes.
The order is ACB.
Example 2:
Input: votes = ["WXYZ","XYZW"]
Output: "XWYZ"
Explanation:
Candidate W receives 1 vote.
Candidate X receives 1 vote.
Candidate Y receives 1 vote.
Candidate Z receives 1 vote.
The order is ordered alphabetically: WXYZ.
In the end, we need to sort them according to the number of votes they received.
Example 3:
Input: votes = ["ZMNAGUEDSJYLBOPHRQICWFXTVK"]
Output: "ZMNAGUEDSJYLBOPHRQICWFXTVK"
Explanation:
Only one round of votes.
The order is ZMNAGUEDSJYLBOPHRQICWFXTVK.
Constraints:
1 <= votes.length <= 1000
1 <= votes[i].length <= 26
votes[i]
are upper-case English letters.votes[i]
are distinct.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for finding the first unique number involves checking each number to see if it is unique. We accomplish this by comparing each number with every other number in the list. The process continues until a unique number is discovered or all numbers have been examined.
Here's how the algorithm would work step-by-step:
def find_first_unique_number_brute_force(number_list):
for current_index in range(len(number_list)):
is_unique = True
# Compare the current number to all other numbers
for comparison_index in range(len(number_list)):
# Skip comparing the number to itself
if current_index == comparison_index:
continue
if number_list[current_index] == number_list[comparison_index]:
# If a match is found, the number is not unique
is_unique = False
break
# If the number is unique, return it
if is_unique:
return number_list[current_index]
# If no unique number is found, return None
return None
The best way to find the first unique number is to keep track of how many times we've seen each number. We can update these counts as we look at the list, and also keep track of the numbers in the order we saw them.
Here's how the algorithm would work step-by-step:
class FirstUnique:
def __init__(self, numbers):
self.number_counts = {}
self.number_queue = []
for number in numbers:
self.add(number)
def showFirstUnique(self):
# Iterate to find the first unique number
while self.number_queue and self.number_counts[self.number_queue[0]] > 1:
self.number_queue.pop(0)
if self.number_queue:
return self.number_queue[0]
else:
return -1
def add(self, number):
# Increment the count for the number.
if number in self.number_counts:
self.number_counts[number] += 1
else:
self.number_counts[number] = 1
# Only add to queue if seen for the first time
self.number_queue.append(number)
Case | How to Handle |
---|---|
Empty input array | Return -1 immediately as there are no elements to check. |
Input array with one element | Check if the single element's count is one, returning it, otherwise -1. |
Input array with all identical elements | The counts of all elements will be greater than 1, so return -1. |
Large input array with a single unique element at the very end | The solution should iterate through the entire array efficiently, not timing out or using excessive memory. |
Input array contains both positive and negative numbers, including zero | The counting mechanism (e.g., hash map) must correctly handle non-positive numbers. |
Input array contains duplicate numbers with one unique number | The count of the unique number will be equal to one while the others are greater than one. |
Input array with maximum integer values that lead to overflow if summed directly | The solution should not sum the array elements directly, and the counts should be stored in a data structure that doesn't cause an overflow (e.g., long). |
All elements in the input appear an even number of times | Return -1 when iteration is complete because no numbers have a count of one. |