Leetcode Range Sum Query - Immutable || Intuition + Example + Code

preview_player
Показать описание
Given an integer array nums, handle multiple queries of the following type:

Calculate the sum of the elements of nums between indices left and right inclusive where left less than equal to right.
Implement the NumArray class:

NumArray(int[] nums) Initializes the object with the integer array nums.
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]).
Рекомендации по теме
Комментарии
Автор

very nice explaination with clean code...
// good luck

shreyasg
Автор

You didnt need to make it so complex -> it can be done in a simple and easy to understand manner
class NumArray {
public:

vector<int> prefixSum;
NumArray(vector<int>& nums) {

int sum = 0;
for (int i = 0; i < nums.size(); i++) {

sum += nums[i];
this->prefixSum[i] = sum;
}
}

int sumRange(int left, int right) {

return (left - 1) >= 0 ? prefixSum[right] - prefixSum[left - 1]
: prefixSum[right];
}
};

aspiring_Sr.dev_Om
Автор

Agar 1 based indexing kare to kya change karna

adityadhanrajsinghara
Автор

Wao yr .. girls with such clean efficient coding 🔥

AmandeepSingh-wvyc
Автор

mam : accumulate name se koii function nhi hai leetcode me bata rha hai

Rahul-jziq
Автор

Kuch bhi matlab easy problem ka complex solution 😢

mdsaad
visit shbcf.ru