Skip to the code
cf.
+25 XP

#43 Page View Counter

medium

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.

Both methods may be called many 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).

Example 1

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.

Example 2

Input: ["ViewCounter","busiest","busiest"]
[[[5,5,5]],[1],[3]]
Output: [null,0,0]

Every 1-day stretch ties, so the smallest start, 0, wins.

Constraints

  • 1 <= views.length <= 10^5
  • -10^4 <= views[i] <= 10^4
  • 0 <= left <= right < views.length
  • 1 <= length <= views.length
Saved in this browser

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).