LeetCode 303. Range Sum Query - Immutable [Algorithm + Code Explained ]

preview_player
Показать описание
One of the most frequently asked coding interview questions on Dynamic Programming in companies like Google, Facebook, Amazon, LinkedIn, Microsoft, Uber, Apple, Adobe etc.

LeetCode : Range Sum Query - Immutable

Question : Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:
Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) - 1
sumRange(2, 5) - -1
sumRange(0, 5) - -3
Note:
You may assume that the array does not change.
There are many calls to sumRange function.

Рекомендации по теме
Комментарии
Автор

you have good understanding in this concept that will help others as well

rakeshreddyabbireddy
Автор

*Python*

class NumArray:
def __init__(self, nums: List[int]):
n = len(nums)
self.dp = [0] * (n + 1)
for i in range(1, n+1):
self.dp[i] = self.dp[i-1] + nums[i - 1]

def sumRange(self, left: int, right: int) -> int:
return self.dp[right + 1] - self.dp[left]

robogirlTinker
Автор

Thank you very much Tiwari. Amazing explanation.

mohammadyahya
Автор

Hey Jayati, thanks for the clear and precise explanation. Data points at 2:10 are really helpful i was wondering if you have it for all data structures? Please share it if you can .

codebhakt
Автор

excellent video !

Where can i get a cheat sheet for the patterns?

davidsierra
Автор

Can someone post solution in cpp with the same logic

kartiksharma
Автор

Basically dp[ ] is nothing but a prefix sum.

Dinesh_kuku
welcome to shbcf.ru