A live poll has options numbered 0 to n - 1. Votes keep arriving, and the results panel shows totals for groups of neighboring options. Write a class PollTally:
new PollTally(n) starts every option at 0 votes.vote(option, count) adds count votes to option.total(left, right) returns the votes for options left to right, inclusive.Both methods should be O(log n).
Each example lists the calls in order, then each call's arguments. The first call creates the object with new PollTally(…); the output lists what each call returns (null for the constructor and for methods that return nothing).
Input: ["PollTally","vote","vote","vote","total","total"] [[5],[0,3],[2,4],[4,1],[0,2],[1,4]] Output: [null,null,null,null,7,5]
Options 0 to 2 have 3 + 0 + 4 = 7 votes; options 1 to 4 have 0 + 4 + 0 + 1 = 5.
Input: ["PollTally","total"] [[3],[0,2]] Output: [null,0]
No votes yet.
1 <= n <= 10^51 <= count <= 10000 <= left <= right < n10^5 calls.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #687 Live Poll Tally. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.