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.

Have you met this question in a real interview? Yes
Example
For array [1,2,7,8,5], and queries [(0,4),(1,2),(2,4)], return [23,9,20] Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first. Challenge
O(logN) time for each query

这道题最简便的方法当然是prefix Sum

/**
* 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) {
// write your code here
long[] prefixSum = new long[A.length+1];
for (int i=0; i<A.length; i++) {
prefixSum[i+1] = prefixSum[i] + A[i];
}
ArrayList<Long> res = new ArrayList<Long>();
for (Interval interval : queries) {
int start = interval.start;
int end = interval.end;
long result = prefixSum[end+1] - prefixSum[start];
res.add(result);
}
return res;
}
}

用Segment Tree

 /**
* 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 class SegmentTreeNode {
long sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
public SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
}
} SegmentTreeNode root; public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) {
// write your code here
ArrayList<Long> res = new ArrayList<Long>();
root = build(A, 0, A.length-1);
for (Interval interval : queries) {
int start = interval.start;
int end = interval.end;
res.add(query(root, start, end));
}
return res;
} 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;
} public Long query(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 query(cur.left, start, end);
else if (start > mid) return query(cur.right, start, end);
else return query(cur.left, start, mid) + query(cur.right, mid+1, end);
}
}

Lintcode: Interval Sum的更多相关文章

  1. Lintcode: Interval Sum II

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

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

  3. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  4. LintCode "4 Sum"

    4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...

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

  6. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  7. LintCode Subarray Sum

    For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...

  8. [LintCode] Two Sum 两数之和

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  9. [LintCode] Submatrix Sum 子矩阵之和

    Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return ...

随机推荐

  1. spotlight监控工具使用

    利用spotlight工具可以监控如下系统:        1.Spotlight on Unix 监控Linux服务器 1)安装 Spotlight on Unix 2)配置spotlight登陆用 ...

  2. Oracle数据库--SQL函数

    Oracle SQL函数  1.ASCII返回与指定的字符对应的十进制数;SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii( ...

  3. 样条曲线的Fortran程序

    subroutine basis_function_b_val ( tdata, tval, yval ) ! !******************************************* ...

  4. 一个很好的Delphi博客

    一个很好的Delphi博客,主人叫万一 http://www.cnblogs.com/del/archive/2011/09/21/2183007.html

  5. this super 解释

    关于 this super 什么时候有,他们指向谁? 书上说: this 指向自己,super指向父亲的对象,个人觉得是错误的. 我认为 this 是一个指向自己对象的句柄,而super只是一个类句柄 ...

  6. Bootstrap 进度条媒体对象和条组

    列表组组件 列表组组件用于显示一组列表的组件. //基本实例 <ul class="list-group"> <li class="list-group ...

  7. Xcode Shortcuts

    Description:⌘: Command     ⌥: Option     ⌃: Control    ←↑↓→: Left, Up, Down, Right                  ...

  8. nsenter into docker. selinux(semanage,restorecon)

    Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动了五个容器后,每个容器默认是没有配置SS ...

  9. Redis学习笔记(9)-管道/分布式

    package cn.com; import java.util.Arrays; import java.util.List; import redis.clients.jedis.Jedis; im ...

  10. 用Js的eval解析JSON中的注意点

    在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...