- sol1: stack을 활용한 풀이
흔히 있는 스택문제의 유형(처음 보면 어렵지만 많이 보면 익숙해짐)
- for문을 돌며 현재 입력 값(height)과 stack의 top()의 값을 비교
- 현재 입력 값(height)이 stack의 top()보다 크거나 같다면 stk.pop()
- 스택의 크기만큼 answer에 더해줌(stk에 남은 빌딩은 현재 높이 빌딩을 볼 수 있는 빌딩)
- answer 출력
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int count;
stack<int> s;
long long answer = 0;
cin >> count;
for(int i = 0; i < count; ++i){
int height;
cin >> height;
while(!s.empty() && s.top() <= height) s.pop();
answer += s.size();
s.push(height);
}
cout << answer;
}