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. 《Linux内核分析》第三周:Linux系统启动过程

    杨舒雯 原创作品转载请注明出处 Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验--使用gdb跟踪调试内 ...

  2. 课堂讨论 alpha版最后总结

    议时间:组队开发最后总结会议   星期一   下午4:30-5:30 会议地点:学院楼自习室 到会人员:唐野野 胡潘华 王永伟 魏孟 会议概要: 1.展示最后开发成果: 2.交流开发过程心得体会: 会 ...

  3. Drools解决积分问题

    http://blog.csdn.net/quzishen/article/details/6163012 http://www.cnblogs.com/ityouknow/p/7297524.htm ...

  4. Linux学习之CentOS(二)----远程登录管理工具SecureCRT的使用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  5. FMDB基本操作

    1.以前使用数据库,因为一般就建立一张表,所以都是自己写代码创建,没用过fmdb,这次因为项目中涉及聊天模块,需要多张表格和数据库保存聊天记录 按照以前方法不好操作,就研究了下fmdb,发现确实挺方便 ...

  6. 【设计模式】—— 创建者模式Builder

    前言:[模式总览]——————————by xingoo 模式意图 一个对象的创建十分复杂,为了区分构建过程和使用过程,因此分开.使用一个Director类进行对象的创建,Builder规定了这个创建 ...

  7. #pragma once 与 #ifndef 的使用

    为了防止头文件被重复包含,主要有两种方式: 方式一:使用 #ifndef #ifndef OPTIONAL_TEST_H #define OPTIONAL_TEST_H //............. ...

  8. LCM Cardinality UVA - 10892(算术基本定理)

    这题就是 LightOJ - 1236 解析去看这个把https://www.cnblogs.com/WTSRUVF/p/9185140.html 贴代码了: #include <iostrea ...

  9. Xml文档添加节点和属性

    XmlDocument doc = new XmlDocument(); XmlElement xmlElement = doc.CreateElement("节点名称"); xm ...

  10. 洛谷 P2146 [NOI2015]软件包管理器 解题报告

    P2146 [NOI2015]软件包管理器 题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软 ...