-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
제 코드도 괜찮을까요??
22.04.20 18:41 작성 조회수 144
0
저는 조건문을 여러개로 나눠 작성했는데.. lt rt는 생각도 못했어요ㅠ
질문1, 혹시 제 코드는 어떤가요??
질문2. 인풋값의 범위가 커질 경우에는 강의에서 가르쳐주신대로 투포인터? 로 푸는게 제 코드보다는 훨씬 낫겠죠??
import sys
from collections import deque
read_file = open("/Users/Downloads/pythonalgorithm_formac/이분탐색&그리디/9. 증가수열 만들기/in5.txt", 'r')
input = read_file.readline
n = int(input())
nums = list(map(int, input().split()))
nums = deque(nums)
LR = ""
temp = []
# 왼, 오 비교해서 더 작은 값 가져오는데, 그 수가 이미 만들어진 증가수열의 맨 끝 값보다 커야 함.
# 그리고 가져오는 원소 위치에 따라 L, R 기록
# 최종 증가수열 원소의 갯수 카운팅
temp.append(0)
while nums:
if len(nums) == 1 and temp[-1] > nums[0]:
if (nums[0] > int(temp[-1])):
LR += 'L'
break
if (nums[0] < nums[-1]):
if (nums[0] > int(temp[-1])):
LR += 'L'
temp.append(nums.popleft())
elif (nums[-1] > int(temp[-1])):
LR += 'R'
temp.append(nums.pop())
else:
break
elif (nums[0] > nums[-1]):
if (nums[-1] > int(temp[-1])):
LR += 'R'
temp.append(nums.pop())
elif (nums[0] > int(temp[-1])):
LR += 'L'
temp.append(nums.popleft())
else:
break
print(len(temp)-1)
print(LR)
read_file.close()
답변을 작성해보세요.
0
답변 1