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 ...
随机推荐
- @Autowire和@Resource注解的区别
1.@Autowire是Spring开发的,而@Resource是jdk开发的 2.@Autowire是按照type来注解的,而@Resource是按照名称来的,如果名称找不到,那么就按照type,, ...
- php redis 的基本操作
前言: 断断续续的接触了redis的使用.但是也就简单的记住了几个set.get方法,用的还是太少了吧.所以来做个笔记,记录下一些常用的命令. 内容: 首先是php连接redis. $redis = ...
- Beta冲刺——day7
Beta冲刺--day7 作业链接 Beta冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602134 王龙 ...
- BUG报告
Bug1 1.看到的现象:如果在注册时使用中文作为密码,并不会报错,但是登陆过程中的密码框却不能输入中文导致无法登陆. 2.期待的现象:登陆也能支持中文或在注册功能中添加输入约束. 3.二者的差异 在 ...
- 利用Attribute和IErrorHandler处理WCF全局异常
在处理WCF异常的时候,有大概几种方式: 第一种是在配置文件中,将includeExceptionDetailInFaults设置为true <behavior name="servi ...
- Java线程池 与Lambda
七.线程池.Lambda 1.1基本概念: 线程池:其实就是一个容纳多个线程的容器,其中的线程可以反复使用,省去了频繁创建线程对象的操作,无需反复创建线程而消耗过多的资源. 1.2线程池的好处: ...
- MT【76】直线系
解答 :答案是3,4.
- 【bzoj2194】快速傅立叶之二 FFT
题意:给定序列a,b,求序列c,\(c(k)=\sum_{i=k}^{n-1}a(i)b(i-k)\) Solution: \[ c(k)=\sum_{i=k}^{n-1}a(i)b(i-k)\\ c ...
- 学习Spring Boot:(十九)Shiro 中使用缓存
前言 在 shiro 中每次去拦截请求进行权限认证的时候,都会去数据库查询该用户的所有权限信息, 这个时候就是有一个问题了,因为用户的权限信息在短时间内是不可变的,每次查询出来的数据其实都是重复数据, ...
- 【BZOJ1449】[JSOI2009]球队收益(网络流,费用流)
[BZOJ1449][JSOI2009]球队收益(网络流,费用流) 题面 BZOJ 洛谷 题解 首先对于一支队伍而言,总共进行多少场比赛显然是已知的,假设是\(n_i\)场,那么它的贡献是:\(C_i ...