[AlgoExpert] Sunset Views

Sunset Views

  • Time : O(n)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using namespace std;

vector<int> sunsetViews(vector<int> buildings, string direction) {
int ma = -1, n = buildings.size();
bool inc = direction == "WEST";
vector<int> res;
for(int i = inc ? 0 : n - 1; 0 <= i and i < n; i += inc ? 1 : -1) {
if(buildings[i] > ma) {
res.push_back(i);
}
ma = max(ma, buildings[i]);
}
if(!inc) reverse(begin(res), end(res));
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/12/PS/AlgoExpert/sunset-views/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.