Build a class for an analytics widget. new ViewCounter(views) receives the daily view counts once; they never change.
total(left, right) returns the sum of views[left..right], both ends included.busiest(length) returns the start index of the length-day stretch with the largest total. If several tie, return the smallest start index.total may be called up to 100,000 times and busiest up to 100 times, so do the heavy work once in the constructor.
Each example lists the calls in order, then each call's arguments. The first call creates the object with new ViewCounter(…); the output lists what each call returns (null for the constructor and for methods that return nothing).
Input: ["ViewCounter","total","busiest","total"] [[[3,9,2,7,7,1]],[1,3],[2],[0,5]] Output: [null,18,3,29]
9 + 2 + 7 = 18. The 2-day totals are 12, 11, 9, 14, 8, so the busiest starts at 3. All days add to 29.
Input: ["ViewCounter","busiest","busiest"] [[[5,5,5]],[1],[3]] Output: [null,0,0]
Every 1-day stretch ties, so the smallest start, 0, wins.
1 <= views.length <= 10^5-10^4 <= views[i] <= 10^40 <= left <= right < views.length1 <= length <= views.length10^5 calls to total and at most 100 calls to busiest.Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #43 Page View Counter. 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.