From b7d85566e7f253cd5d28e6ed2597254d7233ffc0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 31 Mar 2023 09:37:39 +0300 Subject: [PATCH] Improved task 307 --- .../NumArray.java | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main/java/g0301_0400/s0307_range_sum_query_mutable/NumArray.java b/src/main/java/g0301_0400/s0307_range_sum_query_mutable/NumArray.java index ad4a75b17..f0761e128 100644 --- a/src/main/java/g0301_0400/s0307_range_sum_query_mutable/NumArray.java +++ b/src/main/java/g0301_0400/s0307_range_sum_query_mutable/NumArray.java @@ -1,43 +1,52 @@ package g0301_0400.s0307_range_sum_query_mutable; // #Medium #Array #Design #Segment_Tree #Binary_Indexed_Tree -// #2022_07_07_Time_99_ms_(88.96%)_Space_138.9_MB_(5.17%) +// #2023_03_31_Time_92_ms_(90.27%)_Space_75.3_MB_(16.68%) public class NumArray { + private int[] tree; private int[] nums; - private int sum; public NumArray(int[] nums) { + tree = new int[nums.length + 1]; this.nums = nums; - sum = 0; - for (int num : nums) { - sum += num; + // copy the array into the tree + System.arraycopy(nums, 0, tree, 1, nums.length); + for (int i = 1; i < tree.length; i++) { + int parent = i + (i & -i); + if (parent < tree.length) { + tree[parent] += tree[i]; + } } } public void update(int index, int val) { - sum -= nums[index] - val; + int currValue = nums[index]; nums[index] = val; + index++; + while (index < tree.length) { + tree[index] = tree[index] - currValue + val; + index = index + (index & -index); + } } - public int sumRange(int left, int right) { - int sumRange = 0; - if ((right - left) < nums.length / 2) { - // Array to sum is less than half - for (int i = left; i <= right; i++) { - sumRange += nums[i]; - } - } else { - // Array to sum is more than half - // Better to take total sum and substract the numbers not in range - sumRange = sum; - for (int i = 0; i < left; i++) { - sumRange -= nums[i]; - } - for (int i = right + 1; i < nums.length; i++) { - sumRange -= nums[i]; - } + private int sum(int i) { + int sum = 0; + while (i > 0) { + sum += tree[i]; + i -= (i & -i); } - return sumRange; + return sum; + } + + public int sumRange(int left, int right) { + return sum(right + 1) - sum(left); } } + +/* + * Your NumArray object will be instantiated and called as such: + * NumArray obj = new NumArray(nums); + * obj.update(index,val); + * int param_2 = obj.sumRange(left,right); + */