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 3 * 10^4 times on up to 3 * 10^4 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).
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.
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.
1 <= nums.length <= 3 * 10^4-100 <= nums[i], value <= 1000 <= index < nums.length0 <= left <= right < nums.lengthRun 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 #684 Editable Chart Range Sums. 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.