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 <= 10001 <= votes[i].length <= 26votes[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:
Imagine you are given a long line of people entering a room one by one, and you need to find the very first person who never shows up again. The brute force strategy involves closely examining each person in the room and checking the entire rest of the line to see if they ever reappear.
Here's how the algorithm would work step-by-step:
def find_first_unique_number(waiting_line):
# We scan each person from the start to check if they ever appear anywhere else later
for current_index in range(len(waiting_line)):
current_person = waiting_line[current_index]
is_person_unique = True
for check_index in range(len(waiting_line)):
if current_index != check_index and waiting_line[check_index] == current_person:
is_person_unique = False
break
# If no duplicate was found during our full scan, this is our first unique person
if is_person_unique:
return current_person
# If the loop finishes without returning, everyone in the room has a duplicate
return -1To efficiently find the first unique number as items arrive, we combine a tracking book with an organized waiting line. This clever setup lets us instantly look up how many times a number has appeared while always keeping the current unique champion ready at the front.
Here's how the algorithm would work step-by-step:
from collections import deque
class FirstUnique:
def __init__(self, initial_numbers):
self.frequency_map = {}
self.waiting_line = deque()
for current_number in initial_numbers:
self.add(current_number)
def show_first_unique(self):
# We clean the front of the queue to ensure the current champion is truly unique.
while self.waiting_line:
potential_champion = self.waiting_line[0]
if self.frequency_map[potential_champion] == 1:
return potential_champion
self.waiting_line.popleft()
return -1
def add(self, incoming_number):
# We update or initialize the frequency record to track how often numbers appear.
if incoming_number not in self.frequency_map:
self.frequency_map[incoming_number] = 1
self.waiting_line.append(incoming_number)
else:
self.frequency_map[incoming_number] += 1
# Numbers appearing multiple times are ignored in the waiting line to save search time.| Case | How to Handle |
|---|---|
| Empty initial stream provided to the constructor | Initialize empty underlying data structures so subsequent additions and queries function correctly without null pointer exceptions. |
| Stream contains all duplicate numbers with no unique elements | Maintain a tracking mechanism that returns negative one or an appropriate indicator when no unique number exists. |
| Adding a number that has already appeared multiple times | Update the frequency map and efficiently remove or bypass it in the uniqueness tracking structure. |
| A number appears only once initially, and then a duplicate is added later | Ensure the data structure safely removes the element from the tracking queue or set upon its second occurrence. |
| Large scale input stream with millions of add operations | Use a hash map combined with a doubly linked list or queue to guarantee constant time complexity for both operations. |
| Inputs containing negative numbers and zero alongside positive integers | Use hash-based data structures that natively support negative integer keys and zero without requiring index offset adjustments. |
| Querying for the first unique number when the tracking structure is currently empty | Return the predefined indicator value immediately to signal the absence of any unique elements. |