Times in the Lead
Ballots are counted one at a time in the order given by ballots; each item is the number of the candidate chosen on that ballot. After each ballot is counted, candidate x is in the lead if x has strictly more than half of the ballots counted so far.
Return how many times x is in the lead, checking once after every ballot. An empty ballots gives 0.
Example 1
ballots [1,2,1,1,2]
x 1
returns 4
After each ballot candidate 1 has 1 of 1, 1 of 2, 2 of 3, 3 of 4 and 3 of 5. All but the second check are strict majorities.
Example 2
ballots [2,2,1]
x 1
returns 0
Candidate 1 gets 1 of 3 at best, never more than half.
Constraints
0 <= len(ballots) <= 1000 <= ballots[i] <= 990 <= x <= 99
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Arrays problems
- Valid Mountain ArrayEasy
- Best Rotation ScoreMedium
- Best Stretch Around a RingMedium
- Fewest Swaps to SortMedium
- Insert IntervalMedium
- Longest HillMedium
- Maximum SubarrayMedium
- Next PermutationMedium