Solving Leetcode Interviews in Seconds with AI: Range Sum Query - Mutable
Introduction
In this blog post, we will explore how to solve the LeetCode problem "307" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
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 and right inclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]). Example 1: Input ["NumArray", "sumRange", "update", "sumRange"] [[[1, 3, 5]], [0, 2], [1, 2], [0, 2]] Output [null, 9, null, 8] Explanation NumArray numArray = new NumArray([1, 3, 5]); numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9 numArray.update(1, 2); // nums = [1, 2, 5] numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8 Constraints: 1 <= nums.length <= 3 104 -100 <= nums[i] <= 100 0 <= index < nums.length -100 <= val <= 100 0 <= left <= right < nums.length At most 3 104 calls will be made to update and sumRange.
Explanation
Here's an efficient solution using a Segment Tree to handle the queries.
- Segment Tree: The core idea is to preprocess the input array
numsinto a Segment Tree. Each node in the Segment Tree stores the sum of a range of elements in the original array. This allows for efficient range sum queries. - Update Propagation: When updating an element, the changes are propagated up the tree from the leaf node corresponding to the updated element to the root, ensuring all affected range sums are corrected.
Query Traversal: For range sum queries, we traverse the Segment Tree, only visiting the nodes that cover the query range. This significantly reduces the number of elements to sum compared to iterating through the original array.
Time & Space Complexity:
- Initialization: O(n), Update: O(log n), Query: O(log n)
- Storage: O(n)
Code
class NumArray:
def __init__(self, nums):
self.n = len(nums)
self.nums = nums
self.tree = [0] * (4 * self.n) # Allocate memory for the segment tree
self.build_tree(0, 0, self.n - 1)
def build_tree(self, node, start, end):
if start == end:
self.tree[node] = self.nums[start]
return
mid = (start + end) // 2
self.build_tree(2 * node + 1, start, mid)
self.build_tree(2 * node + 2, mid + 1, end)
self.tree[node] = self.tree[2 * node + 1] + self.tree[2 * node + 2]
def update(self, index, val):
self.update_tree(0, 0, self.n - 1, index, val)
def update_tree(self, node, start, end, index, val):
if start == end:
self.nums[index] = val # Update the original array
self.tree[node] = val
return
mid = (start + end) // 2
if index <= mid:
self.update_tree(2 * node + 1, start, mid, index, val)
else:
self.update_tree(2 * node + 2, mid + 1, end, index, val)
self.tree[node] = self.tree[2 * node + 1] + self.tree[2 * node + 2]
def sumRange(self, left, right):
return self.sum_range_tree(0, 0, self.n - 1, left, right)
def sum_range_tree(self, node, start, end, left, right):
if right < start or left > end:
return 0
if left <= start and end <= right:
return self.tree[node]
mid = (start + end) // 2
sum_left = self.sum_range_tree(2 * node + 1, start, mid, left, right)
sum_right = self.sum_range_tree(2 * node + 2, mid + 1, end, left, right)
return sum_left + sum_right