-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 3 ms (76.10%), Memory - 21.6 MB (6…
….28%)
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<p>Given an integer array <code>nums</code>, handle multiple queries of the following type:</p> | ||
|
||
<ol> | ||
<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> | ||
</ol> | ||
|
||
<p>Implement the <code>NumArray</code> class:</p> | ||
|
||
<ul> | ||
<li><code>NumArray(int[] nums)</code> Initializes the object with the integer array <code>nums</code>.</li> | ||
<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> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input</strong> | ||
["NumArray", "sumRange", "sumRange", "sumRange"] | ||
[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] | ||
<strong>Output</strong> | ||
[null, 1, -1, -3] | ||
|
||
<strong>Explanation</strong> | ||
NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); | ||
numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1 | ||
numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1 | ||
numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li> | ||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> | ||
<li><code>0 <= left <= right < nums.length</code></li> | ||
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>sumRange</code>.</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Approach 3: Prefix Sum | ||
|
||
# Time: O(1) | ||
# Space: O(n) | ||
|
||
class NumArray: | ||
|
||
def __init__(self, nums: List[int]): | ||
self.prefix_sum = [0] * (len(nums) + 1) | ||
for i in range(len(nums)): | ||
self.prefix_sum[i + 1] = self.prefix_sum[i] + nums[i] | ||
|
||
def sumRange(self, left: int, right: int) -> int: | ||
return self.prefix_sum[right + 1] - self.prefix_sum[left] | ||
|
||
|
||
|
||
# Your NumArray object will be instantiated and called as such: | ||
# obj = NumArray(nums) | ||
# param_1 = obj.sumRange(left,right) |