코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
풀이
리스트의 slice를 이용하여 commands에 주어진 i에서 j까지 array를 잘라 새로운 리스트를 만들고 리스트의 sort()를 사용하여 새로운 리스트에서 오름차순으로 정렬된 k번째 수를 구하여 해결했습니다.
코드
def solution(array, commands):
answer = []
for command in commands:
slice_arr = array[command[0] - 1:command[1]]
slice_arr.sort()
answer.append(slice_arr[command[2] - 1])
return answer
pyo7410/Algorithm
1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.
github.com
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[Python] 키패드 누르기 (0) | 2021.02.27 |
---|---|
[Python] 2016년 (0) | 2021.02.21 |
[Python] 체육복 (0) | 2021.02.13 |
[Python] 모의고사 (0) | 2021.02.12 |
[Python] 완주하지 못한 선수 (0) | 2021.02.07 |