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. Each query has two integers [start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.
For array [1,2,7,8,5], and queries [(0,4),(1,2),(2,4)], return [23,9,20].
Analysis:
Use an array to save the sum from 0 to i. Then for query [i, j], we shoud return sum[j] - sum[i - 1].
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) { ArrayList<Long> list = new ArrayList<Long>();
if (A == null || A.length == ) return list;
if (queries == null || queries.size() == ) return list; long[] sum = new long[A.length]; for (int i = ; i < sum.length; i++) {
if (i == ) {
sum[i] = A[i];
} else {
sum[i] += sum[i - ] + A[i];
}
} for (int i = ; i < queries.size(); i++) {
Interval interval = queries.get(i);
if (interval.start == ) {
list.add(sum[interval.end]);
} else {
list.add(sum[interval.end] - sum[interval.start - ]);
}
}
return list;
}
}
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
Given array A = [1,2,7,8,5].
query(0, 2), return10.modify(0, 4), change A[0] from 1 to 4.query(0, 1), return6.modify(2, 1), change A[2] from 7 to 1.query(2, 4), return14.
Analysis:
As the value in the array may change, so it is better to build a segement tree. If the value in the tree is changed, we also need to update its parent node.
public class Solution {
/* you may need to use some attributes here */
SegmentTreeNode root;
/**
* @param A:
* An integer array
*/
public Solution(int[] A) {
if (A == null || A.length == )
return;
root = build(A, , A.length - );
}
private SegmentTreeNode build(int[] A, int start, int end) {
if (A == null || start < || end >= A.length)
return null;
SegmentTreeNode root = new SegmentTreeNode(start, end, );
if (start == end) {
root.sum = A[start];
} else {
int mid = (end - start) / + start;
root.left = build(A, start, mid);
root.right = build(A, mid + , end);
root.sum = root.left.sum + root.right.sum;
}
return root;
}
public long query(int start, int end) {
if (root == null || start > end)
return ;
return helper(root, Math.max(, start), Math.min(end, root.end));
}
public long helper(SegmentTreeNode root, int start, int end) {
if (root.start == start && root.end == end) {
return root.sum;
}
int mid = (root.start + root.end) / ;
if (start >= mid + ) {
return helper(root.right, start, end);
} else if (end <= mid) {
return helper(root.left, start, end);
} else {
return helper(root.left, start, mid) + helper(root.right, mid + , end);
}
}
public void modify(int index, int value) {
if (root == null)
return;
if (index < root.start && index > root.end)
return;
modifyHelper(root, index, value);
}
public void modifyHelper(SegmentTreeNode root, int index, int value) {
if (root.start == root.end && root.start == index) {
root.sum = value;
return;
}
int mid = (root.start + root.end) / ;
if (index >= mid + ) {
modifyHelper(root.right, index, value);
} else {
modifyHelper(root.left, index, value);
}
root.sum = root.left.sum + root.right.sum;
}
}
class SegmentTreeNode {
public int start, end, sum;
public SegmentTreeNode left, right;
public SegmentTreeNode(int start, int end, int sum) {
this.start = start;
this.end = end;
this.sum = sum;
this.left = this.right = null;
}
}
Interval Sum I && II的更多相关文章
- Lintcode: Interval Sum II
Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...
- 1. Two Sum I & II & III
1. Given an array of integers, return indices of the two numbers such that they add up to a specific ...
- [Leetcode][JAVA] Path Sum I && II
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- LeetCode:Combination Sum I II
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- Nested List Weight Sum I & II
Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...
- Two Sum I & II
Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...
- 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. ...
- Leetcode 39 40 216 Combination Sum I II III
Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- 20135316王剑桥Linux内核学习笔记第三周
20135316王剑桥 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC 1000029000 三个法宝:存储程序计算机.函数调 ...
- linux内核分析第四周学习笔记
linux内核分析第四周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...
- Java的起源和发展
程序设计语言的发展 第一代语言:机器语言 0011 1100 …… 第二代语言:汇编语言 ADD 12,0x13 第三 ...
- VS2013安装及测试
一.Visual Studio的安装 首先是Visual Studio英文版的安装,安装完成后,为了用的时候方便,我从官网下载Visual Studio 2013的语言包并安装. 二.进行单元测试. ...
- ElasticSearch 2 (20) - 语言处理系列之如何开始
ElasticSearch 2 (20) - 语言处理系列之如何开始 摘要 Elasticsearch 配备了一组语言分析器,为世界上大多数常见的语言提供良好的现成基础支持. 阿拉伯语.亚美尼亚语,巴 ...
- 12th final 发布评价II
1.约跑App——nice!:用户界面很是赏心悦目,给人一种很放松的感觉,与App的主题很配合,同时也在本周内把同学提出的bug都很好地完善了,而且采用了摄像头进行发布,整个发布过程清晰明朗不少.把约 ...
- ESXi 20181229 刚学到的知识点
1. 查看性能 能够获取到服务器的电源消耗 这里很明显的就能看到 2路服务器的情况下 电源在300w 以下, 平均值 270w 左右. 2. 然后在配置里面能够看到 服务器的信息 设置还能看到 序列 ...
- BZOJ3434 WC2014时空穿梭(莫比乌斯反演)
考虑枚举相邻点距离差的比例.显然应使比例值gcd为1以保证不重复统计.确定比例之后,各维坐标的方案数就可以分开考虑.设比例之和为k,则若坐标上限为m,该维坐标取值方案数即为Σm-ki (i=1~⌊m/ ...
- NOI前各种Idea总结以及各种文本乱堆
转载请注明原文地址:https://www.cnblogs.com/LadyLex/p/9227267.html 不过这篇的确没什么*用了转转吧 2018-6-24 关于一类延迟标记(来自UR14 思 ...
- 【刷题】LOJ 6014 「网络流 24 题」最长 k 可重区间集
题目描述 给定实直线 \(L\) 上 \(n\) 个开区间组成的集合 \(I\) ,和一个正整数 \(k\) ,试设计一个算法,从开区间集合 \(I\) 中选取出开区间集合 \(S \subseteq ...