알고리즘
[Python] 프로그래머스 고득점 Kit (정렬) : K번째 수
ji_iin
2021. 10. 4. 19:11
문제
[ 문제 풀이 ]
문제에서 제시한 대로 array를
- 여러 commands 배열의 0번째 값 부터 1번째 값 까지 잘라낸 후
- 자른 배열 정렬 후
- 2번째 값 인덱스를 return 하면 되는 간단한 문제다.
다른 짧은 풀이도 봤는데, 한줄로도 풀 수 있는 문제였다.
나는 몇 줄에 걸쳐했지만,, 문법 공부하자...
코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 정렬 : K번째 수 | |
def solution(array, commands): | |
result = [] | |
for com in commands: | |
i, j, k = com | |
result.append(list(sorted(array[i-1:j]))[k-1]) | |
return result | |
solution([1, 5, 2, 6, 3, 7, 4],[[2, 5, 3], [4, 4, 1], [1, 7, 3]]) | |
# 1번째 풀이 | |
# def solution(array, commands): | |
# c = commandsƒ | |
# l = [] | |
# for i in range(len(commands)): | |
# start = c[i][0]-1 | |
# end = c[i][1] | |
# th = c[i][2]-1 | |
# arr = sorted(array[start:end]) | |
# l.append(arr[th]) | |
# return l | |
# solution([1, 5, 2, 6, 3, 7, 4],[[2, 5, 3], [4, 4, 1], [1, 7, 3]]) |