-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2345.cpp
More file actions
42 lines (42 loc) · 1.5 KB
/
2345.cpp
File metadata and controls
42 lines (42 loc) · 1.5 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
class Solution {
public:
static bool compare(vector<int>& v1, vector<int>& v2) {
if (v1[1] == v2[1]) return v1[0] < v2[0];
return v1[1] > v2[1];
}
int visibleMountains(vector<vector<int>>& peaks) {
int n = peaks.size();
if (n == 1) return 1;
map<int, int> position2height;
sort(peaks.begin(), peaks.end(), compare);
set<pair<int, int>> st;
for (int i = 0; i < n; ++i) {
if (i == n - 1 && n > 1) {
if ((peaks[i][0] == peaks[i - 1][0]) && (peaks[i][1] == peaks[i - 1][1])) {
st.insert({peaks[i][0], peaks[i][1]});
continue;
}
}
else if (peaks[i][0] == peaks[i + 1][0] && peaks[i][1] == peaks[i + 1][1]) {
st.insert({peaks[i][0], peaks[i][1]});
continue;
}
}
int count = 0;
for (auto& peak : peaks) {
int position = peak[0];
int height = peak[1];
auto it = position2height.upper_bound(position);
if (it != position2height.end()) {
if (it->second - (it->first - position) >= height) continue;
}
if (it != position2height.begin()) {
it--;
if (it->second - (position - it->first) >= height) continue;
}
if (st.find({peak[0], peak[1]}) == st.end()) count++;
position2height[position] = height;
}
return count;
}
};