Advertisement

Range Sum Query - Mutable - LeetCode 307 Solution

Range Sum Query - Mutable - Complete Solution Guide

Range Sum Query - Mutable is LeetCode problem 307, a Medium 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 types: Update the value of an element in nums . 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 . void update(int index, int val) Updates the value of nums[index] to be val . int sumRange(int left, int right) Returns the sum of the elements of nums between indices left

Detailed Explanation

The problem asks us to implement a `NumArray` class that can handle two types of queries on an integer array `nums`: updating the value of an element at a given index and calculating the sum of elements within a given range (inclusive). The `NumArray` class should be initialized with the initial array `nums`, and we need to efficiently implement both `update` and `sumRange` operations, adhering to the given constraints. Essentially, we need a data structure that allows for both point updates and range queries on an array.

Solution Approach

The provided code implements the `NumArray` class using a Binary Indexed Tree (BIT). The BIT is used to store prefix sums in a way that allows for both efficient updates and range sum queries. During initialization, the BIT is constructed based on the input array `nums`. The `update` operation updates the BIT to reflect the change in value at a given index. The `sumRange` operation calculates the sum of elements within the specified range by utilizing the prefix sum property of the BIT. Specifically, it finds the sum from index 0 up to `right` and subtracts the sum from index 0 up to `left - 1` to get the desired range sum.

Step-by-Step Algorithm

  1. Step 1: **Initialization:** The `NumArray` class is initialized with the input array `nums`. A BIT (Binary Indexed Tree) is created with a size of `n + 1`, where `n` is the length of `nums`.
  2. Step 2: **BIT Construction:** The BIT is constructed by iterating through the `nums` array and updating the BIT for each element. The `_update_bit` helper function is used for this, which iteratively adds the element's value to the appropriate nodes in the BIT.
  3. Step 3: **Update Operation:** The `update(index, val)` function updates the value at the given `index` in the `nums` array and propagates this change to the BIT. The `delta` is computed as the difference between the `val` and the old value at `index`. This delta is then used to update the BIT using the `_update_bit` function.
  4. Step 4: **Sum Range Operation:** The `sumRange(left, right)` function calculates the sum of the elements within the range `[left, right]` (inclusive). It achieves this by calculating the prefix sum up to `right` using the `_query_bit` function and subtracting the prefix sum up to `left - 1` using the `_query_bit` function. The result is the sum of the desired range.
  5. Step 5: **Query BIT ( _query_bit):** This helper function calculates the prefix sum up to a given `index`. It iteratively traverses the BIT, adding the values of the nodes that contribute to the prefix sum.
  6. Step 6: **Update BIT ( _update_bit):** This helper function updates the BIT when a value at a specific `index` in the original array changes. It iteratively traverses the BIT, adding the `delta` to the appropriate nodes to reflect the change.

Key Insights

  • Insight 1: A naive approach of iterating through the range for `sumRange` and direct assignment for `update` would result in O(n) time complexity for `sumRange`, which is inefficient given the number of queries. Therefore, using a more efficient data structure or algorithm is necessary.
  • Insight 2: Binary Indexed Tree (BIT), also known as Fenwick Tree, is an efficient data structure for solving range query problems where both point updates and prefix sum queries are required. BIT can perform both operations in O(log n) time.
  • Insight 3: The BIT data structure requires a one-based indexing scheme, meaning we will need to shift the indices by one when accessing it from the original zero-based `nums` array.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Divide and Conquer, Design, Binary Indexed Tree, Segment Tree.

Companies

Asked at: Google.