Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[프로그래머스] Lv.1 K번째 수 [Python, 파이썬] 본문
- 문제 : https://school.programmers.co.kr/learn/courses/30/lessons/42748
- array의 요소들을 commands의 i ~ j의 범위에 맞게 정렬 후 k 번째 수를 찾는 문제
- 예) array = [1, 5, 2, 6, 3, 7, 4] commands[0] = [2, 5, 3]
- i = 2, j = 5, k = 3
- 2~5번째까지 추출 = [5, 2, 6, 3]
- 정렬 = [2, 3, 5, 6]
- k 번째 수 = 5
def solution(array, commands):
answer = []
arr = []
for i in range(len(commands)):
arr = array[commands[i][0]-1:commands[i][1]]
arr.sort()
answer.append(arr[commands[i][2]-1])
return answer
print(solution([1, 5, 2, 6, 3, 7, 4],
[[2, 5, 3], [4, 4, 1], [1, 7, 3]]))
'프로그래머스' 카테고리의 다른 글
[프로그래머스] Lv.1 같은 숫자는 싫어 [Python, 파이썬] (0) | 2023.02.16 |
---|---|
[프로그래머스] Lv.2 가장 큰 수 [Python, 파이썬] (0) | 2023.02.16 |
[프로그래머스] Lv.0 소인수분해 [Python, 파이썬] (0) | 2022.12.29 |
[프로그래머스] Lv.0 공 던지기 [Python, 파이썬] (0) | 2022.12.27 |
[프로그래머스] Lv.0 숨어있는 숫자의 덧셈 (2) [Python, 파이썬] (0) | 2022.12.27 |
Comments