728x90
📚문제
📝풀이
# 1668번 트로피 진열(Bronze II)
import sys
from collections import deque
from collections import OrderedDict
arr = []
cnt_l = 0
cnt_r = 0
for _ in range(int(input())):
arr.append(int(input()))
left = deque(arr[:arr.index(max(arr))+1])
right = deque((arr[arr.index(max(arr)):]))
# 왼쪽 시점
max_left = 0
while True:
if left == deque([]):
break
if left[0] > max_left:
cnt_l += 1
max_left = left[0]
left.popleft()
else:
left.popleft()
# 오른쪽 시점
max_right = 0
while True:
if right == deque([]):
break
if right[-1] > max_right :
cnt_r += 1
max_right = right[-1]
right.pop()
else:
right.pop()
print(cnt_l)
print(cnt_r)
다른 풀이를 보니 훨씬 더
간단하게 풀 수 있었다..ㅎ
그래도 내가 생각한 방향성으로 풀어보고 싶었고
몇 번 시행착오 겪은 다음에
풀어내서 뿌듯하다
✒️다른 풀이
arr = []
for _ in range(int(input())):
arr.append(int(input()))
# 왼쪽 방향
max_lh = cnt_l = 0
for i in arr:
if max_lh < i:
max_lh = i
cnt_l += 1
# 오른쪽 방향
max_rh = cnt_r = 0
for j in range(len(arr)-1,-1,-1):
if max_rh < arr[j]:
max_rh = arr[j]
cnt_r += 1
print(cnt_l)
print(cnt_r)
왼쪽 오른쪽 방향에서 오는것은 똑같은데
나는 왼쪽 방향 / 오른쪽 방향 리스트를 따로 구분해주었다
이 풀이가 코드가 조금 더 간단한 것 같다
728x90
'Python(알고리즘,문제풀이) > BOJ(Bronze II)' 카테고리의 다른 글
백준 / 1718번 / 암호 / Python / 구현,문자열 (0) | 2024.01.11 |
---|---|
백준 / 1673번 / 치킨 쿠폰 / 수학,구현 (0) | 2024.01.09 |
백준 / 1592번 / 영식이와 친구들 / Python / 구현,시뮬레이션 (0) | 2024.01.08 |
백준 / 1440번 / 타임머신 / Python / 브루트포스 알고리즘 (0) | 2024.01.07 |
백준 / 1408번 / 24 / Python / 수학,구현,사칙연산 (1) | 2024.01.06 |