Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6-H0ngJu #164

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
| 3μ°¨μ‹œ | 2024.03.10 | νž™ | [N번째 큰 수](https://www.acmicpc.net/problem/2075) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/156 |
| 4μ°¨μ‹œ | 2024.03.13 | νž™ | [λ¬Έμ œμ§‘](https://www.acmicpc.net/problem/1766) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/158 |
| 5μ°¨μ‹œ | 2024.03.16 | κ΅¬ν˜„ | [μš”μ„Έν‘ΈμŠ€ 문제](https://www.acmicpc.net/problem/1158) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/161 |
| 6μ°¨μ‹œ | 2024.03.19 | μŠ€νƒ | [였큰수](https://www.acmicpc.net/problem/17298) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/164 |

---
25 changes: 25 additions & 0 deletions H0ngJu/였큰수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
from collections import *

def input(): return sys.stdin.readline().strip()
N = int(input())

arr = list(map(int, input().split()))
arr.reverse()
Comment on lines +7 to +8
Copy link
Collaborator

@pknujsp pknujsp Mar 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr을 λ’€μ§‘κ±°λ‚˜ pop()ν•΄μ„œ μ§€μš°λŠ” μ‹μœΌλ‘œ arr을 크게 κ±΄λ“œλ¦¬μ§€ μ•Šμ•„λ„ λΌμš”

stackμ—μ„œ μ›μ†Œλ₯Ό κΊΌλ‚΄μ„œ answer에 였큰수λ₯Ό λ„£μ–΄μ£ΌλŠ” μƒν™©μ—μ„œλŠ”,
μŠ€νƒμ— 있던 μ›μ†Œμ˜ μΈλ±μŠ€κ°€ ν˜„μž¬ μˆœνšŒμ€‘μΈ arr의 μ›μ†Œ μΈλ±μŠ€λ³΄λ‹€ 항상 μ•žμ— μžˆμ–΄μš”

κ·Έλž˜μ„œ arr을 κ±΄λ“œλ¦¬μ§€ μ•Šκ³ , μˆœμ„œλŒ€λ‘œ μˆœνšŒν•˜λ©΄μ„œ 인덱슀만 μŠ€νƒμ— λ„£μ–΄μ£Όλ©΄μ„œ μ§„ν–‰ν•˜λ©΄ λΌμš”

  1. arr을 μˆœμ„œλŒ€λ‘œ μˆœνšŒν•©λ‹ˆλ‹€
  2. ν˜„μž¬ μ›μ†Œκ°€ μŠ€νƒμ— μžˆλŠ” μ›μ†Œλ³΄λ‹€ 크면, κ·Έ μ›μ†Œμ˜ 였큰수둜 μ„€μ •ν•΄μ€λ‹ˆλ‹€
  3. μŠ€νƒμ΄ λΉ„κ±°λ‚˜ ν˜„μž¬ μ›μ†Œλ³΄λ‹€ 큰 μ›μ†Œλ₯Ό λ§Œλ‚  λ•ŒκΉŒμ§€ 이걸 κ³„μ†ν•©λ‹ˆλ‹€
  4. ν˜„μž¬ μ›μ†Œμ˜ 인덱슀λ₯Ό μŠ€νƒμ— λ„£μŠ΅λ‹ˆλ‹€

μ΄λ ‡κ²Œ ν•˜λ©΄, arr을 λ”°λ‘œ κ±΄λ“œλ¦¬μ§€ μ•Šκ³ , ν•œ 번만 봐도 λͺ¨λ“  였큰수λ₯Ό 찾을 수 μžˆμ–΄μš”
λ©”λͺ¨λ¦¬λ„ λ”°λ‘œ νŠœν”Œμ„ μƒμ„±ν•˜λ©΄μ„œ μ§„ν–‰ν•˜μ§€ μ•ŠμœΌλ‹ˆ 많이 μ€„μ–΄λ“€μ–΄μš”

N = int(input())
A = list(map(int, input().split()))

stack = deque()
result = [-1] * N

for i in range(N):
    while stack and A[stack[-1]] < A[i]:
        result[stack.pop()] = A[i]
        
    stack.append(i)

print(*result)

answer = [-1 for _ in range(N)]

id = 0

stack = []
stack.append((id,arr.pop()))

while arr:
id += 1
arr_data = arr.pop()
while stack and stack[-1][1] < arr_data:
index, data = stack.pop()
answer[index] = arr_data
stack.append((id, arr_data))

for i in answer:
print(i, end=" ")