Advertisement

Range Sum Query - Immutable - LeetCode 303 Solution

Range Sum Query - Immutable - Complete Solution Guide

Range Sum Query - Immutable is LeetCode problem 303, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 <= 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] ). Example 1: Input ["NumArray", "sumRange",

Detailed Explanation

The problem asks you to design a data structure that efficiently handles range sum queries on an integer array. Given an array `nums`, you need to create a structure that allows you to quickly calculate the sum of elements within any given range [left, right] (inclusive) of the array. The input consists of the initial array `nums` and multiple queries specifying the `left` and `right` indices for the sum calculation. The output is a series of sums, one for each query.

Solution Approach

The provided solutions utilize the prefix sum technique. A prefix sum array is created during initialization. This array stores the cumulative sum of the input array at each index. When a `sumRange` query is made, the difference between the prefix sums at `right + 1` and `left` gives the sum of the elements in the specified range. This approach leverages the mathematical property that the sum of elements from index `left` to `right` is equal to the cumulative sum up to `right` minus the cumulative sum up to `left - 1`.

Step-by-Step Algorithm

  1. Step 1: Initialize the `NumArray` object with the input array `nums`. Create a prefix sum array `prefix_sums` of size `len(nums) + 1` and initialize the first element to 0.
  2. Step 2: Iterate through the input array `nums`. For each element `nums[i]`, calculate the cumulative sum by adding `nums[i]` to the previous cumulative sum in `prefix_sums`, and store the result in `prefix_sums[i + 1]`.
  3. Step 3: For each `sumRange` query with indices `left` and `right`, return `prefix_sums[right + 1] - prefix_sums[left]`. This efficiently calculates the sum of the specified range using the pre-computed prefix sums.

Key Insights

  • Insight 1: Using a prefix sum array drastically reduces the time complexity of each range sum query. Instead of iterating through the array for each query, we can calculate the sum in constant time.
  • Insight 2: The prefix sum array `prefix_sums[i]` stores the cumulative sum of elements from `nums[0]` to `nums[i-1]`. This allows us to calculate the sum of any range [left, right] as `prefix_sums[right + 1] - prefix_sums[left]`.
  • Insight 3: The space complexity is increased by using the prefix sum array, but this trade-off significantly improves query time. This is a classic space-time trade-off optimization.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(n)

Topics

This problem involves: Array, Design, Prefix Sum.

Companies

Asked at: Palantir Technologies.