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 ...
随机推荐
- HTML标签之间不是可以随便嵌套的
深究:我们先来认识in-line内联元素和block-line块元素,因为HTML里几乎所有元素都属于内联元素或者块元素中的一种. in-line这个词有很多种解释:内嵌.内联.行内.线级等,但是,它 ...
- c语言作业
- AQS 与 LockSupport
1.结构 Lock的实现类其实都是构建在AbstractQueuedSynchronizer上,每个Lock实现类都持有自己内部类Sync的实例 二.二元信号量 A semaphore initial ...
- zepto源码--width,height--学习笔记
width和height函数,实际上通过css方法也完全可以取到这两个函数的结果.获取width,$elem.css('width');设置width的话,$elem.css('width', 100 ...
- C++ 编译器内存错误 after Normal block。。。
解决 after Normal block(#908) at 0x399EC0. CRT detected that the application wrote to memory after end ...
- iOS Provisioning Profile(Certificate)与Code Signing详解
引言 关于开发证书配置(Certificates & Identifiers & Provisioning Profiles),相信做 iOS 开发的同学没少被折腾.对于一个 iOS ...
- 20145211 《Java程序设计》第1周学习总结——小荷才露尖尖角
教材学习内容总结 Java语言概述 Java是SUN1995年推出的一门高级编程语言,完全面向对象,安全可靠,具有跨平台性(用其编写的语言在任何系统上都能运行,只需安装一个JVM) Java三大平台包 ...
- Android笔记:java 中的枚举
部分数据使用枚举比较方便,java中的enmu不如c#中使用方便 记录备忘 以c#中的代码为例 public enum PlayState { /// <summary> /// 关闭 / ...
- A Guide to Creating a Quality Project Schedule
Successful projects start with a good quality project schedule. Creating a schedule is one of the fi ...
- CSS知识点补充
一.css框模型概述 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外边距,外边距默认是透明的,因此不会遮挡其后的任何元素 1.css内边 ...