반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 재귀
- level3
- 파이썬
- MYSQL
- programmers
- DP
- BFS
- 프로그래머스
- 가상메모리 관리
- 코딩테스트
- 구현
- N과M
- 힙
- 브루트포스
- level0
- level1
- 가상메모리
- 에라스토테네스의 체
- dfs
- level2
- 운영체제
- 수학
- python
- 다익스트라
- 그리디
- 딕셔너리
- 백준
- 다이나믹 프로그래밍
- BOJ
- 스택
Archives
- Today
- Total
동캄의 코딩도장
백준 6198 [옥상 정원 꾸미기] 파이썬 본문
반응형
https://www.acmicpc.net/problem/6198
#백준 6198 옥상 정원 꾸미기
import sys
N=int(sys.stdin.readline().rstrip()) # N 입력 처리
buildings=[] #빌딩의 값을 저장할 스택 생성
cnt=0
ans=0
for i in range(N):
curr_building=int(sys.stdin.readline()) #현재 비교할 빌딩 입력 처리
if buildings: # 스택에 값이 있다면
if buildings[-1]>curr_building: #스택 마지막 값(빌딩 높이) > 현재 비교할 값(빌딩 높이)
buildings.append(curr_building) #스택에 추가
cnt+=1 #스택 크기 1증가
else:
while buildings: #스택이 없다면 반복문 탈출
if buildings[-1]>curr_building: #스택 마지막 값(빌딩 높이) > 현재 비교할 값(빌딩 높이)
buildings.append(curr_building) #스택에 추가
cnt+=1 #스택 크기 1증가
break
else: #스택 마지막 값(빌딩 높이) <= 현재 비교할 값(빌딩 높이)이면
buildings.pop() #스택에서 pop
cnt-=1 #스택 크기 1감소
if not buildings: #만약 스택에 값이 없다면
buildings.append(curr_building) #스택에 추가
cnt+=1 #스택 크기 1증가
else: #스택에 값이 없다면
buildings.append(curr_building) #스택에 추가
cnt+=1
ans+=(cnt-1)
print(ans)
계속 값을 가져오면서 스택을 관리한다.
스택의 마지막 값 > 비교할 값 이면 스택에 저장하고,
그렇지 않다면 스택의 마지막 값 > 비교할 값이 될때까지 스택을 비운다.
반응형
'코테 > BOJ' 카테고리의 다른 글
백준 7562 [나이트의 이동] 파이썬 (0) | 2025.02.20 |
---|---|
백준 17298 [오큰수] 파이썬 (0) | 2025.02.19 |
백준 2504 [괄호의 값] 파이썬 (0) | 2025.02.18 |
백준 10799 [쇠막대기] 파이썬 (0) | 2025.02.13 |
백준 9012 [괄호] 파이썬 (0) | 2025.02.13 |