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

Lintcode: Interval Sum II

时间:2016-02-02 06:28:06      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

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 the given index to value
Have you met this question in a real interview? Yes
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.
Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first.

Challenge
O(logN) time for query and modify.

Segment Tree:

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

 

Lintcode: Interval Sum II

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5176677.html

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