본문 바로가기

Python(알고리즘,문제풀이)/BOJ(Bronze II)

백준 / 1668번 / 트로피 진열 / Python / 구

728x90

📚문제

출처 : 백준 1668번(https://www.acmicpc.net/problem/1668)


📝풀이

# 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