-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205.h
More file actions
81 lines (73 loc) · 2.22 KB
/
Copy path205.h
File metadata and controls
81 lines (73 loc) · 2.22 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
/**
* Definition of Interval:
* class Interval {
* int start, end;
* Interval(int start, int end) {
* this->start = start;
* this->end = end;
* }
* }
*/
struct SegmentNode{
public:
int start, end, min;
SegmentNode *left, *right;
SegmentNode(int start, int end, int min) {
this->start = start;
this->end = end;
this->min = min;
this->left = this->right = nullptr;
}
};
class Solution {
public:
/*
* @param A: An integer array
* @param queries: An query list
* @return: The result list
*/
vector<int> intervalMinNumber(vector<int> &A, vector<Interval> &queries) {
// write your code here
SegmentNode *root = build(0, A.size() - 1, A);
// for(int i = 0; i < A.size(); i++)
// insert(root, i, A[i]);
vector<int> r;
for(int i = 0; i < queries.size(); i++){
r.push_back(query(root, queries[i].start, queries[i].end));
}
return r;
}
private:
SegmentNode *build(int start, int end, const vector<int> &A){
if(start == end){
return new SegmentNode(start, end, A[start]);
}
SegmentNode *root = new SegmentNode(start, end, INT_MAX);
int mid = start + (end - start) / 2;
root->left = build(start, mid, A);
root->right= build(mid+1, end, A);
root->min = min(root->left->min, root->right->min);
return root;
}
int query(SegmentNode *root, int start, int end){
if(root == nullptr) return INT_MAX;
if(start < root->start) start = root->start;
if(end > root->end) end = root->end;
if(start == root->start && end == root->end){
return root->min;
}
int mid = root->start + (root->end - root->start) / 2;
int lmin = INT_MAX, rmin = INT_MAX;
if(start >= mid + 1){
rmin = query(root->right, start, end);
}
else if(end <= mid){
lmin = query(root->left, start, end);
}
else{
lmin = query(root->left, start, mid);
rmin = query(root->right,mid+1, end);
}
return min(lmin, rmin);
}
};