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

 1 /**
 2  * Definition of Interval:
 3  * public classs Interval {
 4  *     int start, end;
 5  *     Interval(int start, int end) {
 6  *         this.start = start;
 7  *         this.end = end;
 8  *     }
 9  */
10 public class Solution {
11     /**
12      *@param A, queries: Given an integer array and an query list
13      *@return: The result list
14      */
15     public class SegmentTreeNode {
16         long sum;
17         int start;
18         int end;
19         SegmentTreeNode left;
20         SegmentTreeNode right;
21         public SegmentTreeNode(int start, int end) {
22             this.sum = 0;
23             this.start = start;
24             this.end = end;
25             this.left = null;
26             this.right = null;
27         }
28     }
29
30     SegmentTreeNode root;
31
32     public ArrayList<Long> intervalSum(int[] A,
33                                        ArrayList<Interval> queries) {
34         // write your code here
35         ArrayList<Long> res = new ArrayList<Long>();
36         root = build(A, 0, A.length-1);
37         for (Interval interval : queries) {
38             int start = interval.start;
39             int end = interval.end;
40             res.add(query(root, start, end));
41         }
42         return res;
43     }
44
45     public SegmentTreeNode build(int[] A, int start, int end) {
46         SegmentTreeNode cur = new SegmentTreeNode(start, end);
47         if (start == end) {
48             cur.sum = A[start];
49         }
50         else {
51             int mid = (start + end)/2;
52             cur.left = build(A, start, mid);
53             cur.right = build(A, mid+1, end);
54             cur.sum = cur.left.sum + cur.right.sum;
55         }
56         return cur;
57     }
58
59     public Long query(SegmentTreeNode cur, int start, int end) {
60         if (cur.start==start && cur.end==end) return cur.sum;
61         int mid = (cur.start + cur.end)/2;
62         if (end <= mid) return query(cur.left, start, end);
63         else if (start > mid) return query(cur.right, start, end);
64         else return query(cur.left, start, mid) + query(cur.right, mid+1, end);
65     }
66 }
时间: 2024-10-27 02:23:48

Lintcode: Interval Sum的相关文章

Lintcode: 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 t

Lintcode207 Interval Sum II solution 题解

[题目描述] 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

Interval Sum I &amp;&amp; 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 l

Lintcode206 Interval Sum solution 题解

[题目描述] 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 re

[LintCode] Subarray Sum II

Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive) Example Given [1,2,3,4] and interval = [1,3], return 4. Bra

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Ex

[LintCode] Subarray Sum &amp; Subarray Sum II

Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3]. 用hashmap来存从nums[0

[LintCode] Submatrix Sum 子矩阵之和

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Have you met this question in a real interview? Yes Example Given matrix [ [1 ,5 ,7], [3 ,7 ,-8],

[LintCode] Two Sum 两数之和

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that