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. word 使用宏批量设置表格

    Sub ChangeTable() Application.Browser.Target = wdBrowseTable To ActiveDocument.Tables.Count ActiveDo ...

  2. Listener-监听器+ServletContext+ApplicationContext

    参考资料 ServletContext和ApplicationContext有什么区别 ServletContext:是web容器的东西, 一个webapp一个, 比session作用范围要大, 从中 ...

  3. CAS实践笔录

    下载CAS 本文下载的是cas-server-4.0.0-release 下载地址: https://www.apereo.org/cas/download 导入Eclipse测试 Import -& ...

  4. Windows与Linux共享文件夹互相访问

    [原文]  首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...

  5. SQL Server 触发器【转】

    触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...

  6. FTS抓包看蓝牙的SDP整个过程

    1.概述   SDP是蓝牙的Service Discovery Protocol,用来发现远程设备能够提供的Service.它只负责发现对方支持的Service,不负责Service的具体实现.   ...

  7. JavaScript判断文件的大小

    function getFileSize(obj) {//obj 需要传入的参数为Input的对象   var objValue = obj.value; if (objValue == " ...

  8. 【Android开发学习笔记】【第九课】重力感应

    概念 使用重力感应技术的Android游戏已经屡见不鲜,不知道自己以后会不会用到,所以先研究了一下. 在网上学习了一下,貌似没有api,所以得自己去分析手机处在怎样状态下.注意: 下面提供的demo程 ...

  9. c 生成随机不重复的整数序列

    #include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <time.h> ...

  10. C++ 安全字符串拼接

    #include <stdio.h> #include <stdint.h> #include <stdarg.h> #if defined(__GNUC__) # ...