Leetcode: Range Sum Query - Mutable && Summary: Segment Tree
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.
Introduction of Segment Tree: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/
Time Complexity:
Time Complexity for tree construction is O(n). There are total 2n-1 nodes, and value of every node is calculated only once in tree construction.
Time complexity to query is O(Logn). To query a sum, we process at most four nodes at every level and number of levels is O(Logn).
The time complexity of update is also O(Logn). To update a leaf value, we process one node at every level and number of levels is O(Logn).
public class NumArray {
SegmentTreeNode root;
public NumArray(int[] nums) {
this.root = buildTree(nums, 0, nums.length-1);
}
void update(int i, int val) {
update(root, i, val);
}
public int sumRange(int i, int j) {
return sumRange(root, i, j);
}
public SegmentTreeNode buildTree(int[] nums, int start, int end) {
if (start > end) return null;
else {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) {
cur.sum = nums[start];
}
else {
int mid = start + (end - start)/2;
cur.left = buildTree(nums, start, mid);
cur.right = buildTree(nums, mid+1, end);
cur.sum = cur.left.sum + cur.right.sum;
}
return cur;
}
}
public void update(SegmentTreeNode root, int i, int val) {
if (root.start == root.end) { //leaf node
root.sum = val;
return;
}
else {
int mid = root.start + (root.end - root.start)/2;
if (i <= mid) update(root.left, i, val);
else update(root.right, i, val);
root.sum = root.left.sum + root.right.sum;
}
}
public int sumRange(SegmentTreeNode root, int i, int j) {
if (i==root.start && j==root.end) return root.sum;
else {
int mid = root.start + (root.end - root.start)/2;
if (j <= mid) return sumRange(root.left, i, j);
else if (i >= mid+1) return sumRange(root.right, i, j);
else
return sumRange(root.left, i, mid) + sumRange(root.right, mid+1, j);
}
}
public class SegmentTreeNode{
int sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
public SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
}
}
}
// 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);
Leetcode: Range Sum Query - Mutable && Summary: Segment Tree的更多相关文章
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query - Mutable 题解
题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...
- [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- php-fpm
install php-fpm # Ubuntu sudo apt-get install python-software-properties; sudo add-apt-repository pp ...
- corresponding SQLSTATE values general error
http://dev.mysql.com/doc/refman/5.7/en/error-messages-server.html B.3 Server Error Codes and Message ...
- Python For Data Analysis -- Pandas
首先pandas的作者就是这本书的作者 对于Numpy,我们处理的对象是矩阵 pandas是基于numpy进行封装的,pandas的处理对象是二维表(tabular, spreadsheet-like ...
- windows系统中ubuntu虚拟机安装及web项目到服务上(二)
ajp方式整合apache2和tomcat 7 1:在apache2.conf配置文件中启用模块mod_proxy_ajp,在里面添加 LoadModule proxy_module modules/ ...
- String作为方法参数传递 与 引用传递
String作为方法参数传递 String 和 StringBuffer的区别见这里: http://wenku.baidu.com/view/bb670f2abd64783e09122bcd.htm ...
- spm完成dmp在windows系统上导入详细过程
--查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...
- 通过驱动向打印机发送一段(ESC)控制指令
这个功能看起来挺奇葩的, 写这个是因为有客户在使用驱动连接票据打印机, 但是又要开钱箱, 驱动里只能每张单据都开钱箱, 而这个打印机又不是只打印结帐单 所以就需要用软件控制打印机开钱箱 票据打印机一般 ...
- JS 去字符串空格 总结
str为要去除空格的字符串: 去除所有空格: str = str.replace(/\s+/g,""); 去除两头空格: str = str.replace(/^\s+|\s+$/ ...
- Java高级之线程同步
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于实现多线程的意义,"从业四年看并发"一文已经讲述,而本篇主要讲一下常用的设计 ...
- JS删除数组条目中重复的条目
[腾讯2015春招web前端开发练习卷] 请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. Array.prototyp ...