You are given an integer array nums
, and you are tasked with performing the following operations:
left
and right
inclusive, where left
and right
are given.Design an efficient data structure to implement the given operations.
Implement the NumArray
class:
NumArray(vector<int>& nums)
Initializes the object with the integer array nums
.void update(int index, int val)
Updates the value of nums[index]
to val
.int sumRange(int left, int right)
Returns the sum of elements between indices left and right inclusive.nums
can contain negative values.nums
will be in the range [1, 3 * 10^4]
.[1, 3 * 10^4]
.To efficiently handle range sum and update queries, we can use a data structure known as Segment Tree. This structure allows us to perform both update and sum range queries in O(log n) time.
Here’s a high-level strategy using a Segment Tree:
#include <vector>
class NumArray {
public:
NumArray(std::vector<int>& nums) : n(nums.size()), tree(2 * nums.size()) {
// Build the tree
buildTree(nums);
}
void update(int index, int val) {
index += n; // Convert to leaf index
tree[index] = val;
// Update the parents
while (index > 0) {
int left = index;
int right = index;
if (index % 2 == 0) {
right = index + 1;
} else {
left = index - 1;
}
// Parent updated to the sum of its children
tree[index / 2] = tree[left] + tree[right];
index /= 2;
}
}
int sumRange(int left, int right) {
left += n;
right += n;
int sum = 0;
while (left <= right) {
// If left is a right node, bring its value and move to parent's right node
if (left % 2 == 1) {
sum += tree[left];
left++;
}
// If right is a left node, bring its value and move to parent's left node
if (right % 2 == 0) {
sum += tree[right];
right--;
}
left /= 2;
right /= 2;
}
return sum;
}
private:
std::vector<int> tree;
int n;
void buildTree(const std::vector<int>& nums) {
// Initialize leaves
for (int i = 0; i < n; ++i) {
tree[n + i] = nums[i];
}
// Build the tree by summing children
for (int i = n - 1; i > 0; --i) {
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
}
};
By using a Segment Tree, we achieve efficient update and query times, making it suitable for handling large arrays and a high frequency of operations.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?