알고리즘 공부 2020. 8. 20. 22:10

https://leetcode.com/problems/range-sum-query-immutable/

 

Range Sum Query - Immutable - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

JS로 간단하게 연습삼아 짜본 코드를 생각나는 대로 올려보자.

 

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

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

/** 
 * Your NumArray object will be instantiated and called as such:
 * var obj = new NumArray(nums)
 * var param_1 = obj.sumRange(i,j)
 */
posted by Sense.J
: