[BOJ] 6198번 : 옥상 정원 꾸미기

2024. 8. 7. 23:18Algorithm

1. problem : 

https://www.acmicpc.net/problem/6198

 

 

 

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int N;
	cin >> N;
	stack<int> s; 
	long long ans = 0; 
	while (N--) {
		int height;
		cin >> height;

		while (!(s.empty()) && s.top() <= height) {
			s.pop();
			
		}
		ans += s.size();
		s.push(height);
	}
	cout << ans << '\n';
}

ans를 while문안에서 더해주다가 틀렸다. 이때가 성립할 때는 감소하다가 증가하는 포인트가 있어야 성립한다. 따라서, while문 밖으로 ans를 빼주어야 한다. 

'Algorithm' 카테고리의 다른 글

[BOJ] 1267번 : 핸드폰 요금  (0) 2024.08.12
[BOJ] 4949번 : 균형잡힌 세상  (0) 2024.08.08
[BOJ] 5430번 : AC  (0) 2024.08.07
[BOJ] 1021번 : 회전하는 큐  (0) 2024.08.07
[BOJ] 10866번 : 덱  (0) 2024.08.07