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.

Example

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(startend), return the sum from index start to index end in the given array.
  • For modify(indexvalue), modify the number in the given index to value
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.

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的更多相关文章

  1. Lintcode: Interval Sum II

    Given an integer array in the construct method, implement two methods query(start, end) and modify(i ...

  2. 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 ...

  3. [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 ...

  4. LeetCode:Combination Sum I II

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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. ...

  9. 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 ...

随机推荐

  1. 重温httpsession①

    Session—HTTPSession 服务器创建的,Javaweb提供的 与HTTP协议无关是服务器端对象,保存在服务器端.用来会话跟踪. Cookie与服务器创建,与HTTP协议相关,保存在客户端 ...

  2. python 安装多个包/pip用法

    列出已安装的包 pip freeze or pip list 导出requirements.txt pip freeze > <目录>/requirements.txt 安装包 在线 ...

  3. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  4. [转帖]ESXi 网卡绑定 增加吞吐量的方法

    VMware ESX 5.0 网卡负载均衡配置3种方法 http://blog.chinaunix.net/uid-186064-id-3984942.html (1) 基于端口的负载均衡 (Rout ...

  5. php 中的 “!=”和“!==”

    !==是指绝对不等于,比如,$a = 2, $b=”2″   那么,$a!==$b成立,可是$a!=$b不成立:

  6. Android 图表

    今天在群里有人提问Android的图表,在网上搜索了一下,在贴吧中看到的回答,说是Trinea整理的开源项目,不知道是不是博客上的Trinea. 将内容记录如下,以备需要时查看. GraphView ...

  7. 使用ETL构建数据仓库的思考

    使用ETL构建数据仓库的思考 背景:公司的数据仓库建设项目启动在即,所谓万事开头难,如何在我们数仓建设规划的前期做好业务数据准备和系统建设规划是我们需要思考的问题,这里根据之前的自己参与过的公司ODS ...

  8. Python之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  9. 【刷题】BZOJ 1823 [JSOI2010]满汉全席

    Description 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能够做出满汉全席,而 ...

  10. 今天GG

    刚开考: 这里锅了,那里锅了,还被D了QAQ. 然后\(YL\)说,\(T2\)不是傻逼题吗. 于是萝卜秒掉了\(T1\). 于是\(gsy\)秒掉了\(T3\). \(lalaxu,FlashHu\ ...