307. Range Sum Query - Mutable
题目:
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.
Example:
- Given nums = [1, 3, 5]
- sumRange(0, 2) -> 9
- update(1, 2)
- sumRange(0, 2) -> 8
Note:
- The array is only modifiable by the update function.
- You may assume the number of calls to update and sumRange function is distributed evenly.
链接: http://leetcode.com/problems/range-sum-query-mutable/
题解:
这应该算是Range Query的经典题目之一了。也是通过这道题我第一次接触到了Segment Tree,也对Fenwick Tree有了一点了解。下面是用Segment Tree来做的。 Segment Tree线段树每一个节点都是一段线段,有start和end,然后还可以有其他的值,比如区间和sum,区间最大值max,区间最小值min。我们可以用自底向上构建二叉树的方式构建Segment Tree,这个过程也有点类似于Bottom-up的merge sort,思想也是Divide and Conquer。完毕之后就可以在O(logn)的时间update,或者得到range Sum。其实更好的方法是使用Fenwick Tree, Fenwick Tree(Binary Indexed Tree)在处理Range Query真的是一绝,构造简练,原理也精妙,还可以扩展到多维,一定要好好学一学。
Time Complexity - O(n) build, O(logn) update, O(logn) rangeSum, Space Complexity - O(n)
- public class NumArray {
- private class SegmentTreeNode {
- public int start;
- public int end;
- public int sum;
- public SegmentTreeNode left, right;
- public SegmentTreeNode(int start, int end) {
- this.start = start;
- this.end = end;
- this.sum = 0;
- }
- }
- private SegmentTreeNode root;
- public NumArray(int[] nums) {
- this.root = buildTree(nums, 0, nums.length - 1);
- }
- public void update(int i, int val) {
- update(root, i, val);
- }
- private void update(SegmentTreeNode node, int position, int val) {
- if(node.start == position && node.end == position) {
- node.sum = val;
- return;
- }
- int mid = node.start + (node.end - node.start) / 2;
- if(position <= mid) {
- update(node.left, position, val);
- } else {
- update(node.right, position, val);
- }
- node.sum = node.left.sum + node.right.sum;
- }
- public int sumRange(int i, int j) {
- return sumRange(root, i, j);
- }
- private int sumRange(SegmentTreeNode node, int lo, int hi) {
- if(node.start == lo && node.end == hi) {
- return node.sum;
- }
- int mid = node.start + (node.end - node.start) / 2;
- if(hi <= mid) {
- return sumRange(node.left, lo, hi);
- } else if (lo > mid) {
- return sumRange(node.right, lo, hi);
- } else {
- return sumRange(node.left, lo, mid) + sumRange(node.right, mid + 1, hi);
- }
- }
- private SegmentTreeNode buildTree(int[] nums, int lo, int hi) {
- if(lo > hi) {
- return null;
- } else {
- SegmentTreeNode node = new SegmentTreeNode(lo, hi);
- if(lo == hi) {
- node.sum = nums[lo];
- } else {
- int mid = lo + (hi - lo) / 2;
- node.left = buildTree(nums, lo, mid);
- node.right = buildTree(nums, mid + 1, hi);
- node.sum = node.left.sum + node.right.sum;
- }
- return node;
- }
- }
- }
- // Your NumArray object will be instantiated and called as such:
- // NumArray numArray = new NumArray(nums);
- // numArray.sumRange(0, 1);
- // numArray.update(1, 10);
- // numArray.sumRange(1, 2);
Fenwick Tree: (Binary Indexed Tree) (树状数组)
很有意思的构建,以数组nums = {1, 2, 3, 4, 5, 6, 7, 8}为例,这个数组长度为8。 跟dynamic programming的预处理很像,我们先建立一个长度为nums.length + 1 = 9的数组BIT。接下来遍历数组nums,对BIT数组进行update(i + 1, nums[i])。这里BIT数组每个值BIT[i]代表nums数组里在i之前的部分元素和。原理是像自然数可以被表示为2n的和一样,把nums数组里到0到i的sum表示成2n的和,从而导致update和rangeSum都可以用O(logn)的时间求出来。这里构建的时候可以有几种写法,主要就是利用当前i的least significante 1来确定到底BIT[i]要保存多少原数组的值。这里借用algorithmist的原话"Every index in the cumulative sum array, say i, is responsible for the cumulative sum from the index i to (i - (1<<r) + 1)。" 构建过程中可以用 (i & -i)来找到least significate 1,之后来进行i = i + (i & -i)来尝试从小到大计算下一个BIT数组中被影响的元素。 而rangeSum的时候则使用i = i - (i & -i)来从大到小查找从0到i - 1的sum。
构建过程 - update, 给定数组nums = {1,2, 3, 4, 5, 6, 7, 8}
BIT[0] = 0
BIT[1] = nums[0] = 1 = 1
BIT[2] = nums[0] + nums[1] = 1 + 2 = 3
BIT[3] = nums[2] = 3 = 3
BIT[4] = nums[0] + nums[1] + nums[2] + nums[3] = 1+ 2 + 3 + 4 = 10
BIT[5] = nums[4] = 5 = 5
BIT[6] = nums[4] + nums[5] = 5 + 6 = 11
BIT[7] = nums[6] = 7 = 7
BIT[8] = nums[0] + nums[1] + nums[2] + nums[3] + nums[4] + nums[5] + nums[6] + nums[7] = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
求Sum过程, 通过 sum = BIT[i + 1]; i = i - (i & -i); 从大到小迭代来计算。
sum(0) = BIT[1]
sum(1) = BIT[2]
sum(2) = BIT[3] + BIT[2]
sum(3) = BIT[4]
sum(4) = BIT[5] + BIT[4]
sum(5) = BIT[6] + BIT[4]
sum(6) = BIT[7] + BIT[6] + BIT[4]
sum(7) = BIT[8]
得到sum(i)以后就可以相减来计算range sum了。
Time Complexity - O(nlogn) build, O(logn) update, O(logn) rangeSum, Space Complexity - O(n)
- public class NumArray {
- private int BIT[]; // Binary Indexed Tree = Fenwick Tree
- private int[] nums;
- public NumArray(int[] nums) {
- BIT = new int[nums.length + 1];
- for(int i = 0; i < nums.length; i++) {
- init(i + 1, nums[i]);
- }
- this.nums = nums;
- }
- private void init(int i, int val) {
- while(i < BIT.length) {
- BIT[i] += val;
- i = i + (i & -i);
- }
- }
- public void update(int i, int val) {
- int delta = val - nums[i];
- nums[i] = val;
- init(i + 1, delta);
- }
- public int sumRange(int i, int j) {
- return getSum(j + 1) - getSum(i);
- }
- private int getSum(int i) {
- int sum = 0;
- while(i > 0) {
- sum += BIT[i];
- i = i - (i & -i);
- }
- return sum;
- }
- }
题外话: 今天去NYU图书馆自习,正值考期,人山人海的。我带了电脑却忘记带插座,无奈只能用Java 在线的编译器https://www.compilejava.net/, 不过这个真的还挺好用,除了不能debug,其他都可以,nice。晚上吃了Hakata Tonton,4个人大概人均$50+,并没有想象的那么好吃,以后还是要攒机会去日本玩。 刷题群里大家对Heapify有了热烈的讨论,我自己认为Heapify主要有两种,Bottom-up (swim)和Top-down(sink)。也要复习一下Priority Queue的implementation。要多看Sedgewick的课件和sample code才行。 在GitHub发现有个人叫indy256,实现了好多好多高级数据结构,有2d-fenwick tree,以及O(n)的Suffix Tree。大牛和普通人的距离真的好遥远,我还是继续努力。
二刷:
暂时只用了segment tree。 Fenwick Tree以后再理解。
Java:
Segment Tree:
- public class NumArray {
- private SegmentTreeNode root;
- private int[] nums;
- public NumArray(int[] nums) {
- this.nums = nums;
- this.root = buildTree(0, nums.length - 1);
- }
- void update(int i, int val) {
- update(root, i, val);
- }
- private void update(SegmentTreeNode node, int pos, int val) {
- if (node == null) return;
- if (node.start == pos && node.end == pos) {
- node.val = val;
- nums[pos] = val;
- return;
- }
- int mid = node.start + (node.end - node.start) / 2;
- if (pos <= mid) {
- update(node.left, pos, val);
- } else {
- update(node.right, pos, val);
- }
- node.val = node.left.val + node.right.val;
- }
- public int sumRange(int i, int j) {
- return sumRange(root, i, j);
- }
- private int sumRange(SegmentTreeNode node, int lo, int hi) {
- if (lo > hi) return 0;
- if (node.start == lo && node.end == hi) return node.val;
- int mid = node.start + (node.end - node.start) / 2;
- if (hi <= mid) {
- return sumRange(node.left, lo, hi);
- } else if (lo > mid) {
- return sumRange(node.right, lo, hi);
- } else {
- return sumRange(node.left, lo, mid) + sumRange(node.right, mid + 1, hi);
- }
- }
- private SegmentTreeNode buildTree(int lo, int hi) {
- if (lo > hi) return null;
- SegmentTreeNode node = new SegmentTreeNode(lo, hi);
- if (lo == hi) {
- node.val = nums[lo];
- } else {
- int mid = lo + (hi - lo) / 2;
- node.left = buildTree(lo, mid);
- node.right = buildTree(mid + 1, hi);
- node.val = node.left.val + node.right.val;
- }
- return node;
- }
- private class SegmentTreeNode {
- int start;
- int end;
- int val;
- SegmentTreeNode left, right;
- public SegmentTreeNode(int start, int end) {
- this.start = start;
- this.end = end;
- this.val = 0;
- }
- }
- }
- // Your NumArray object will be instantiated and called as such:
- // NumArray numArray = new NumArray(nums);
- // numArray.sumRange(0, 1);
- // numArray.update(1, 10);
- // numArray.sumRange(1, 2);
Reference:
https://www.compilejava.net/
http://algs4.cs.princeton.edu/93intersection/IntervalST.java.html
https://leetcode.com/discuss/70202/17-ms-java-solution-with-segment-tree
http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/
http://algs4.cs.princeton.edu/99misc/SegmentTree.java.html/
https://leetcode.com/discuss/70272/solution-using-buckets-updating-for-query-the-worst-case-fast
https://leetcode.com/discuss/74222/java-using-binary-indexed-tree-with-clear-explanation
https://leetcode.com/discuss/70278/simple-recursive-java-solution
http://algs4.cs.princeton.edu/99misc/FenwickTree.java.html
https://web.stanford.edu/class/cs97si/03-data-structures.pdf
https://en.wikipedia.org/wiki/Fenwick_tree
http://algs4.cs.princeton.edu/99misc/FenwickTree.java.html
http://cs.nyu.edu/courses/spring14/CSCI-UA.0480-004/Lecture4.pdf
https://www.topcoder.com/community/data-science/data-science-tutorials/
http://www.algorithmist.com/index.php/Fenwick_tree
https://leetcode.com/discuss/70273/java-7ms-binary-index-tree-solution
https://leetcode.com/discuss/72658/java-solution-with-binary-indexed-tree-beats-81-95%25
https://leetcode.com/discuss/74222/java-using-binary-indexed-tree-with-clear-explanation
https://leetcode.com/discuss/70311/11-ms-java-binary-tree
https://leetcode.com/discuss/70191/share-my-c-solution-1700ms-using-tree-array
https://leetcode.com/discuss/70293/java-binary-indexed-tree
https://sites.google.com/site/indy256/algo_cpp/fenwick_tree
https://sites.google.com/site/indy256/algo/fenwick_tree_2d
http://www.cnblogs.com/grandyang/p/4985506.html
http://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=A4ADC19B8D7E3A202944A808F5840D21?doi=10.1.1.14.8917&rep=rep1&type=pdf
http://arxiv.org/pdf/1311.6093v5.pdf
https://leetcode.com/discuss/72658/java-solution-with-binary-indexed-tree-beats-81-95%25
307. Range Sum Query - Mutable的更多相关文章
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] 307. Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- leetcode@ [307] Range Sum Query - Mutable / 线段树模板
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] 307. Range Sum Query - Mutable 解题思路
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- LeetCode - 307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 307. Range Sum Query - Mutable查询求和的范围(可变)
[抄题]: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...
- leetcode 307. Range Sum Query - Mutable(树状数组)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 【leetcode】307. Range Sum Query - Mutable
题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
随机推荐
- 将项目初始化到git服务器
使用的是GitLab来管理Git服务器; 步骤: 一. 先在服务器上创建一个新的项目(GitLab右上角的New project)
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写
使用loadrunner进行压力测试主要分两步,第一步是编写脚本(比较重点),第二步执行测试(配置都是在界面上点点就行了,当然我只的是比较简单的,能满足日常需要的),第三步分析结果(这一步比较高深,但 ...
- 轻松解决在一个虚拟主机上运行多个 ASP.NET 网站应用
不知道有没有朋友像我一样会遇到这样一个问题: 在网上购买 .NET 空间,由于虚拟主机的限制,你并不能把某个目录设为一个独立的应用,或者一些价格比较高的空间,虽然可以设置,但数量也是有限的.这个问题导 ...
- 四则运算三+psp0级表格
一.题目 在四则运算二的基础上,选择一个方向进行拓展,我选择的是增加了答题模块 二.设计思路 1.在上次的基础上,增加了答题模块,每出现一道四则运算题目,便提醒输入结果,如果结果错误,就会提示错误 2 ...
- 延迟加载 ERROR org.hibernate.LazyInitializationException:42 - could not initialize proxy - ...
no Session问题,即延迟加载 延迟加载的问题是指当我们调用完action中的某个方法,在jsp页面要显示我们想要的信息的时候,发现在dao中打开的session已经关闭了. 如下图,第一个箭头 ...
- C#预编译指令
近日工作涉及到于外部系统交互,对方提供接口:但是在双方系统未联调时,引用外部DLL,相关类实例化,提示异常错误(错误消息正常):后面操作无法进行,那如何写调试代码,即在调试时不运行某段代码,而在正式发 ...
- C语言遇到的错误和解决方案~~~持续更新,记录成长的过程
1.error C2296: '&' : illegal, left operand has type 'char [3]' scanf("%d" &x); 少了一 ...
- 浅谈自我对git的初步认识
本学期我们新增了一门课程,那就是软件工程,不知道为什么,感觉有种莫名的高大上.哈哈!难道是这个名称太抽象了吗?这个问题我感觉到后来肯定就明白了. 第一次博客,感觉好紧张哦,嘿嘿!老师让我们谈谈对git ...
- UVALive - 6529 找规律+dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...
- javascript版Ajax请求
什么是Ajax请求,Ajax也就是“Asynchronous JavaScript and XML”(异步JavaScript和XML),无刷新数据读取.能减少流量的消耗,也提高了浏览的流畅性,给用户 ...