[프로그래머스] K번째수 (Python)

SeongWon Oh·2021년 7월 17일
0
post-thumbnail

🔗 문제 링크

https://programmers.co.kr/learn/courses/30/lessons/42748


👨🏻‍💻 내가 작성한 코드

def solution(array, commands):
    answer = []
    for command in commands:
        temp = array[command[0]-1: command[1]]
        temp.sort()
        answer.append(temp[command[2]-1])
    
    return answer

📝 결론

문제를 풀며 sort와 slicing를 다시 한번 떠올릴 수 있었다.'

4~6줄의 경우 아래와 같이 더 줄일 수 있다.

i,j,k = command
        answer.append(list(sorted(array[i-1:j]))[k-1])
profile
블로그 이전했습니다. -> https://seongwon.dev/

0개의 댓글