|
| 1 | +<h2><a href="https://leetcode.com/problems/range-sum-query-mutable/">307. Range Sum Query - Mutable</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code>, handle multiple queries of the following types:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li><strong>Update</strong> the value of an element in <code>nums</code>.</li> |
| 5 | + <li>Calculate the <strong>sum</strong> of the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> <strong>inclusive</strong> where <code>left <= right</code>.</li> |
| 6 | +</ol> |
| 7 | + |
| 8 | +<p>Implement the <code>NumArray</code> class:</p> |
| 9 | + |
| 10 | +<ul> |
| 11 | + <li><code>NumArray(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> |
| 12 | + <li><code>void update(int index, int val)</code> <strong>Updates</strong> the value of <code>nums[index]</code> to be <code>val</code>.</li> |
| 13 | + <li><code>int sumRange(int left, int right)</code> Returns the <strong>sum</strong> of the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> <strong>inclusive</strong> (i.e. <code>nums[left] + nums[left + 1] + ... + nums[right]</code>).</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong>Example 1:</strong></p> |
| 18 | + |
| 19 | +<pre><strong>Input</strong> |
| 20 | +["NumArray", "sumRange", "update", "sumRange"] |
| 21 | +[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]] |
| 22 | +<strong>Output</strong> |
| 23 | +[null, 9, null, 8] |
| 24 | + |
| 25 | +<strong>Explanation</strong> |
| 26 | +NumArray numArray = new NumArray([1, 3, 5]); |
| 27 | +numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9 |
| 28 | +numArray.update(1, 2); // nums = [1, 2, 5] |
| 29 | +numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8 |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li> |
| 37 | + <li><code>-100 <= nums[i] <= 100</code></li> |
| 38 | + <li><code>0 <= index < nums.length</code></li> |
| 39 | + <li><code>-100 <= val <= 100</code></li> |
| 40 | + <li><code>0 <= left <= right < nums.length</code></li> |
| 41 | + <li>At most <code>3 * 10<sup>4</sup></code> calls will be made to <code>update</code> and <code>sumRange</code>.</li> |
| 42 | +</ul> |
| 43 | +</div> |
0 commit comments