반응형
난이도 - Easy
Intuition
subsums를 사용하면 쉽게 풀수있다. 캐시를 사용하는것이므로 sumRange()의 time complexity는 O(1)이다.
Code
class NumArray:
def __init__(self, nums: List[int]):
self.subsums = [0] * (len(nums) + 1)
for i in range(len(nums)):
self.subsums[i+1] = self.subsums[i] + nums[i]
def sumRange(self, left: int, right: int) -> int:
return self.subsums[right+1] - self.subsums[left]
# Your NumArray object will be instantiated and called as such:
# obj = NumArray(nums)
# param_1 = obj.sumRange(left,right)
Complexity
Time Complexity: O(n) - __init__, O(1) - sumRange()
Space Complexity: O(n)
반응형
'코딩 알고리즘 문제 > Leetcode' 카테고리의 다른 글
| 9. Palindrome Number (Math) (0) | 2025.10.23 |
|---|---|
| 2043. Simple Bank System (Array, Hash Table, Design, Simulation) (0) | 2025.10.23 |
| 480. Sliding Window Median (Array, Hash Table, Sliding Window, Heap (Priority Queue)) (0) | 2025.10.23 |
| 1060. Missing Element in Sorted Array (Array, Binary Search) (0) | 2025.10.23 |
| 3005. Count Elements With Maximum Frequency (Array, Hash Table, Counting) (0) | 2025.10.23 |