Skip to content

Commit a4280d3

Browse files
authored
[오늘의 알고리즘] 탑 (#42)
1 parent 4efe2d4 commit a4280d3

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

yongjun-0903/탑.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
처음 생각
3+
import sys
4+
N = int(sys.stdin.readline().strip())
5+
tops = list(map(int, sys.stdin.readline().split()))
6+
tops = list(reversed(tops))
7+
answer = []
8+
# i는 0부터 3까지
9+
for i in range(N - 1):
10+
# j는 1부터 4까지
11+
for j in range(i+1, N):
12+
if tops[i] > max(tops[j:]):
13+
answer.append(N)
14+
elif tops[i] <= tops[j]:
15+
answer.append(tops[j])
16+
answer.append(N)
17+
reversed_tops = [N - x for x in answer]
18+
print(list(reversed(reversed_tops)))
19+
"""
20+
21+
import sys
22+
23+
N = int(sys.stdin.readline().strip())
24+
heights = list(map(int, sys.stdin.readline().split()))
25+
26+
result = [0] * N
27+
28+
stack = []
29+
30+
for i in range(N):
31+
height = heights[i]
32+
33+
while stack and heights[stack[-1]] < height:
34+
stack.pop()
35+
36+
if stack:
37+
result[i] = stack[-1] + 1
38+
39+
stack.append(i)
40+
41+
print(' '.join(map(str, result)))

0 commit comments

Comments
 (0)