Range Sum Query – Immutable | LeetCode 303

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard

Here is the full implementation.

/**
 * @param {number[]} nums
 */
var NumArray = function(nums) {
    this.runningTotal = [0];
    
    for (let i = 0; i < nums.length; i++) {
        this.runningTotal[i + 1] = nums[i] + this.runningTotal[i];
    }
};

/** 
 * @param {number} i 
 * @param {number} j
 * @return {number}
 */
NumArray.prototype.sumRange = function(i, j) {
    return this.runningTotal[j + 1] - this.runningTotal[i];
};