码迷,mamicode.com
首页 > 其他好文 > 详细

Interval Sum I && II

时间:2016-07-14 07:12:55      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

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

 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 ArrayList<Long> intervalSum(int[] A, 
16                                        ArrayList<Interval> queries) {
17                                            
18         ArrayList<Long> list = new ArrayList<Long>();                                   
19         if (A == null || A.length == 0) return list;
20         if (queries == null || queries.size() == 0) return list;
21         
22         long[] sum = new long[A.length];
23         
24         for (int i = 0; i < sum.length; i++) {
25             if (i == 0) {
26                 sum[i] = A[i];
27             } else {
28                 sum[i] += sum[i - 1] + A[i];
29             }
30         }
31         
32         for (int i = 0; i < queries.size(); i++) {
33             Interval interval = queries.get(i);
34             if (interval.start == 0) {
35                 list.add(sum[interval.end]);
36             } else {
37                 list.add(sum[interval.end] - sum[interval.start - 1]);
38             }
39         }
40         return list;
41     }
42 }

 

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.

 1 public class Solution {
 2     /* you may need to use some attributes here */
 3 
 4     SegmentTreeNode root;
 5 
 6     /**
 7      * @param A:
 8      *            An integer array
 9      */
10     public Solution(int[] A) {
11         if (A == null || A.length == 0)
12             return;
13         root = build(A, 0, A.length - 1);
14     }
15     
16     private SegmentTreeNode build(int[] A, int start, int end) {
17         if (A == null || start < 0 || end >= A.length)
18             return null;
19         SegmentTreeNode root = new SegmentTreeNode(start, end, 0);
20         if (start == end) {
21             root.sum = A[start];
22         } else {
23             int mid = (end - start) / 2 + start;
24             root.left = build(A, start, mid);
25             root.right = build(A, mid + 1, end);
26             root.sum = root.left.sum + root.right.sum;
27         }
28         return root;
29     }
30 
31     public long query(int start, int end) {
32         if (root == null || start > end)
33             return 0;
34         return helper(root, Math.max(0, start), Math.min(end, root.end));
35     }
36 
37     public long helper(SegmentTreeNode root, int start, int end) {
38         if (root.start == start && root.end == end) {
39             return root.sum;
40         }
41 
42         int mid = (root.start + root.end) / 2;
43         if (start >= mid + 1) {
44             return helper(root.right, start, end);
45         } else if (end <= mid) {
46             return helper(root.left, start, end);
47         } else {
48             return helper(root.left, start, mid) + helper(root.right, mid + 1, end);
49         }
50     }
51 
52     public void modify(int index, int value) {
53         if (root == null)
54             return;
55         if (index < root.start && index > root.end)
56             return;
57         modifyHelper(root, index, value);
58     }
59 
60     public void modifyHelper(SegmentTreeNode root, int index, int value) {
61         if (root.start == root.end && root.start == index) {
62             root.sum = value;
63             return;
64         }
65 
66         int mid = (root.start + root.end) / 2;
67         if (index >= mid + 1) {
68             modifyHelper(root.right, index, value);
69         } else {
70             modifyHelper(root.left, index, value);
71         }
72         root.sum = root.left.sum + root.right.sum;
73 
74     }
75 }
76 
77 class SegmentTreeNode {
78     public int start, end, sum;
79     public SegmentTreeNode left, right;
80 
81     public SegmentTreeNode(int start, int end, int sum) {
82         this.start = start;
83         this.end = end;
84         this.sum = sum;
85         this.left = this.right = null;
86     }
87 }

 

 

Interval Sum I && II

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5668864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!