Skip to the code
+25 XP

#684 Editable Chart Range Sums

medium

A revenue chart lets users edit any day's value and drag a selection to see the total of the selected days. Write a class RangeSums:

  • new RangeSums(nums) starts with the daily values nums.
  • update(index, value) sets day index to value.
  • sumRange(left, right) returns the sum of days left to right, inclusive.

Both methods are called up to 10^5 times on up to 10^5 days, so each call 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 RangeSums(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["RangeSums","sumRange","update","sumRange"]
[[[1,3,5]],[0,2],[1,2],[0,2]]
Output: [null,9,null,8]

1 + 3 + 5 = 9; after day 1 becomes 2, the total is 1 + 2 + 5 = 8.

Example 2

Input: ["RangeSums","sumRange","sumRange","sumRange"]
[[[5,8,6,3,2,7,2,6]],[2,6],[0,7],[4,4]]
Output: [null,20,39,2]

Days 2 to 6 sum to 6 + 3 + 2 + 7 + 2 = 20.

Constraints

  • 1 <= nums.length <= 10^5
  • -100 <= nums[i], value <= 100
  • 0 <= index < nums.length
  • 0 <= left <= right < nums.length
  • At most 10^5 calls.
Saved in this browser

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