Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathInterval Sum.java
More file actions
Latest commit
executable file
·104 lines (85 loc) · 3.06 KB
/
Copy pathInterval Sum.java
File metadata and controls
executable file
·104 lines (85 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
M
1527997211
tags: SegmentTree, BinarySearch, Lint
给一串数字int[], 然后一个queryInterval[], 每个interval是 [start, end], 找query区间里的sum.
#### SegmentTree + BinarySearch
- 其实是segmenttree每个node上面加个sum
- 记得SegmentTreemethods: Build, Query
- Note: 存在SegmentTreeNode里面的是sum. 其他题目可能是min,max,count ... orsomethingelse.
```
/*
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]
Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first.
Challenge
O(logN) time for each query
*/
/*
Thoughts:
Feels like constructing segment tree, and attach 'interval sum' to each node, after conquer its left and right child's sum.
*/
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
publicclassSolution {
publicclassSegmentSumTreeNode {
publicintstart, end;
publiclongsum;
publicSegmentSumTreeNodeleft,right;
publicSegmentSumTreeNode(intstart, intend, longsum) {
this.start = start;
this.end = end;
this.sum = sum;
this.left = null;
this.right = null;
}
}
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
publicList<Long> intervalSum(int[] A, List<Interval> queries) {
List<Long> rst = newArrayList<>();
if (A == null || A.length == 0 || queries == null || queries.size() == 0) {
returnrst;
}
SegmentSumTreeNoderoot = build(A, 0, A.length - 1);
for (Intervalrange : queries) {
rst.add(query(root, range.start, range.end));
}
returnrst;
}
privateSegmentSumTreeNodebuild(int[] A, intstart, intend) {
if (start == end) returnnewSegmentSumTreeNode(start, end, A[start]);
intmid = (start + end) / 2;
SegmentSumTreeNodeleftChild = build(A, start, mid);
SegmentSumTreeNoderightChid = build(A, mid + 1, end);
SegmentSumTreeNodenode = newSegmentSumTreeNode(start, end, leftChild.sum + rightChid.sum);
node.left = leftChild;
node.right = rightChid;
returnnode;
}
privatelongquery(SegmentSumTreeNoderoot, intstart, intend) {
if (root.start == start && root.end == end) returnroot.sum;
intmid = (root.start + root.end) / 2;
if (end <= mid) {
returnquery(root.left, start, end);
} elseif (start > mid) {
returnquery(root.right, start, end);
}
//start <= mid < end
returnquery(root.left, start, root.left.end) + query(root.right, root.right.start, end);
}
}
```