-
프로그래머스 - k번째 수프로그래밍/알고리즘 2021. 3. 22. 13:07
def solution(arr, commands): answer = [] while len(commands) != 0: command = commands.pop(0) # 2차원배열 분리 comFirstNum = command.pop(0) -1 # 뽑아진 1차원 배열에서 차례대로 pop comSecNum = command.pop(0) comLastNum = command.pop(0) - 1 # 마지막 단계인 배열에서 뽑아낼 수 selectedArr = arr[comFirstNum:comSecNum] # 뽑아진 수를 사용해 arr에서 골라냄 selectedArr.sort() # 뽑힌 수를 정렬 answer.append(selectedArr[comLastNum]) return answer
다른 사람의 풀이 1
def solution(array, commands): return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands))
다른 사람의 풀이 2
def solution(array, commands): answer = [] for command in commands: i,j,k = command answer.append(list(sorted(array[i-1:j]))[k-1]) # for i, j, k = commands: return answer
728x90반응형LIST'프로그래밍 > 알고리즘' 카테고리의 다른 글
서울에서 김서방 찾기 (0) 2021.03.23 같은 숫자는 싫어 (0) 2021.03.23 가운데 글자 가져오기 (0) 2021.03.23 문자열 다루기 기본 (0) 2021.03.22 2016년 (0) 2021.03.22