Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start, end), return the sum from index start to index end in the given array.
For modify(index, value), modify the number in the given index to value
Have you met this question in a real interview? Yes
Example
Given array A = [1,2,7,8,5]. query(0, 2), return 10.
modify(0, 4), change A[0] from 1 to 4.
query(0, 1), return 6.
modify(2, 1), change A[2] from 7 to 1.
query(2, 4), return 14.
Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first. Challenge
O(logN) time for query and modify.
Segment Tree:
public class Solution {
/* you may need to use some attributes here */
class SegmentTreeNode {
long sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
}
}
SegmentTreeNode root;
/**
* @param A: An integer array
*/
public Solution(int[] A) {
// write your code here
if (A == null || A.length==0) return;
root = build(A, 0, A.length-1);
}
public SegmentTreeNode build(int[] A, int start, int end) {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) cur.sum = A[start];
else {
int mid = (start + end)/2;
cur.left = build(A, start, mid);
cur.right = build(A, mid+1, end);
cur.sum = cur.left.sum + cur.right.sum;
}
return cur;
}
/**
* @param start, end: Indices
* @return: The sum from start to end
*/
public long query(int start, int end) {
// write your code here
return queryTree(root, start, end);
}
public long queryTree(SegmentTreeNode cur, int start, int end) {
if (cur.start==start && cur.end==end) return cur.sum;
int mid = (cur.start + cur.end)/2;
if (end <= mid) return queryTree(cur.left, start, end);
else if (start > mid) return queryTree(cur.right, start, end);
else return queryTree(cur.left, start, mid) + queryTree(cur.right, mid+1, end);
}
/**
* @param index, value: modify A[index] to value.
*/
public void modify(int index, int value) {
// write your code here
modifyTree(root, index, value);
}
public void modifyTree(SegmentTreeNode cur, int index, int val) {
if (cur.start == cur.end) {
cur.sum = val;
return;
}
int mid = (cur.start + cur.end)/2;
if (index <= mid) modifyTree(cur.left, index, val);
else modifyTree(cur.right, index, val);
cur.sum = cur.left.sum + cur.right.sum;
}
}
Lintcode: Interval Sum II的更多相关文章
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- Lintcode: Interval Sum
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- LintCode: Combination Sum II
C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...
- LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...
- Interval Sum I && II
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- [LintCode] Subarray Sum & Subarray Sum II
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- javaWeb中struts开发——helloworld
1.新建一个web项目 2.选中project,右键,选择MyElcipse,选择add struts capab...添加struts支持,然后自己命名包 3.Struts在建立jsp时,标签要到 ...
- DDL DML DCL SQL
https://dev.mysql.com/doc/refman/5.7/en/glossary.html#glos_ddl SQL The Structured Query Language tha ...
- P1018 乘积最大
开始定义状态f[i][j][k]为[i,j)区间插入k个括号,使用记忆化搜索,但是成功爆栈,得到4个mle #include <bits/stdc++.h> using namespace ...
- VS附加到进程调试的方法及应用场景
应用场景:.Net做网站时,代码量很大的时候,每次调试一个网页都编译整个网站是不显示的,而且有时候整个网站是存在错误的,通不过编译.这时你又要调试某部分网页,就可以通过附加到进程调试.方法如下: (1 ...
- GPG操作——签名验证
问题描述: 可能大家都遇到过软件在下载过程中由于网络原因导致下载的软件体积与实际软件体积不符.最常见的办法是对待下载文件附加一个摘要文件.这种做法比较常见,也比较容易实现.但是,还是会有一个问题:如果 ...
- oracle变量的定义和使用【转】
在程序中定义变量.常量和参数时,则必须要为它们指定PL/SQL数据类型.在编写PL/SQL程序时,可以使用标量(Scalar)类型.复合(Composite)类型.参照(Reference)类型和LO ...
- ViewModel在MVC3中的应用:实现多字段表格的部分更新
假设我们有这样一张用户表: public class F_users { [Key] [Display(Name="用户名:")] [Required(ErrorMessage=& ...
- Shell Script-读取配置文件
需求 有时候在shell script里面需要一些执行,如果放在程序里面不便于统一管理,并且每一次修改路径都要去script里面改好麻烦,所以统一把路径放在配置文件中方便管理. 问题 如何读取相对应的 ...
- Aptana快捷键(方便查询)
窗口类:Ctrl+ Shift +L 调出快捷键提示Ctrl+ W 关闭当前标签窗口Ctrl+ Shift + W 关闭当前标签窗口Ctrl+ F6 (或者是Aptana的Ctrl+Tab )下一个编 ...
- asp.net mvc视图中嵌套分部视图
asp.net mvc中Layout相当于webForm中母版页,分部视图相当于webForm中的用户控件. 下面例子是一个视图如何嵌套分部视图: A是分部视图,B是一般视图(A,B中的代码省略) 我 ...