🔍 알고리즘/프로그래머스 Python

[Python] 프로그래머스 42748. K번째 수 (Lv.1)

탄치 2022. 8. 7. 22:35

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

파이썬의 내장 메소드를 이용해 List를 자르고, 정렬해서 원하는 값을 출력했습니다.

 

def solution(array, commands):
    answer = []

    for command in commands:
        start = command[0]
        end = command[1]
        target = command[2]

        arr = array[start-1:end]
        arr.sort()
        answer.append(arr[target-1])

    return answer

print(solution([1, 5, 2, 6, 3, 7, 4], [[2, 5, 3], [4, 4, 1], [1, 7, 3]]))
728x90