Skip to content

Commit

Permalink
Update 0303-range-sum-query-immutable.js
Browse files Browse the repository at this point in the history
Converted to class based.
  • Loading branch information
aadil42 authored Jul 1, 2023
1 parent caef263 commit 1b4919f
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions javascript/0303-range-sum-query-immutable.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
/**
* https://leetcode.com/problems/range-sum-query-immutable/
* @param {number[]} nums
*/
var NumArray = function(nums) {
this.arr = nums;
};
class NumArray {
constructor(nums) {
this.arr = nums;
}

/**
* https://leetcode.com/problems/range-sum-query-immutable/description/
* Time O(n) | Space O(1)
* @param {number} left
* @param {number} right
* @return {number}
*/
NumArray.prototype.sumRange = function(left, right) {

let total = 0;
for(let i = left; i < right + 1; i++) {
total += this.arr[i];
/**
* Time O(n) | Space O(1)
* @param {number} left
* @param {number} right
* @return {number}
*/
sumRange(left, right) {
let total = 0;
for (let i = left; i < right + 1; i++) {
total += this.arr[i];
}
return total;
}
return total
};
}

/**
* Your NumArray object will be instantiated and called as such:
Expand Down

0 comments on commit 1b4919f

Please sign in to comment.