> Uploading knowledge... _
[░░░░░░░░░░░░░░░░░░░░░░░░] 0%
blog logo
> CHICIO CODING_Pixels. Code. Unplugged.

Range Sum Query - Immutable

Leetcode Problem 303: Range Sum Query - Immutable

Problem Summary

Design a NumArray class that preprocesses an integer array nums at construction time to efficiently answer multiple range sum queries. Each query sumRange(left, right) returns the sum of elements from index left to right inclusive.

  • NumArray(nums) — Initializes the object with the integer array.
  • sumRange(left, right) — Returns the sum of nums[left] through nums[right] inclusive.

Constraints:

  • The array has between 1 and 10,000 elements, each in the range [-100,000, 100,000].
  • Query indices satisfy 0 <= left <= right < nums.length.
  • At most 10,000 queries will be made.

Techniques

  • Array
  • Design
  • Prefix Sum

Solution

class NumArray {
    private prefixSum: number[]

    constructor(nums: number[]) {
        let sum = 0;
        this.prefixSum = nums.map(num => sum += num);

        console.log(this.prefixSum)
    }

    sumRange(left: number, right: number): number {
        if (left === 0) {
            return this.prefixSum[right]
        } 

        return this.prefixSum[right] - this.prefixSum[left - 1]
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * var obj = new NumArray(nums)
 * var param_1 = obj.sumRange(left,right)
 */