Given an integer array nums, handle multiple queries of the following types:
nums.nums between indices left and right inclusive where left <= right.Implement the NumArray class:
NumArray(int[] nums) Initializes the object with the integer array nums.void update(int index, int val) Updates the value of nums[index] to be val.int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]). Input
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output
[null, 9, null, 8]
Explanation
update operation, we simply change the value at the designated index.sumRange(i, j) operation, we iterate through the array from index i to j and calculate the sum.This approach sacrifices performance for simplicity, offering an easy-to-understand solution at the cost of increased time complexity for range queries.
update: O(1)sumRange: O(n), where n is the number of elements between left and right.update and sumRange operations can be conducted in O(log n) time, balancing between brute force direct access and high efficiency.update: O(log n)sumRange: O(log n)update: O(log n)sumRange: O(log n)