코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
해결 과정
sort(), sorted() 아느냐
나의 풀이
def solution(array, commands):
answer = []
for command in commands:
sorted_array = []
sorted_array = array[command[0] - 1:command[1]]
sorted_array.sort()
temp = sorted_array[command[2] - 1]
answer.append(temp)
return answer
다른 풀이
lambda, map 적절하게 사용한 간결한 코드
def solution(array, commands):
return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands))
# 문법
map(f, iterable)
# 함수(f)와 반복 가능한 (iterable) 자료형을 입력으로 받는다.
'Coding Test > 문제 풀이' 카테고리의 다른 글
[Algorithm] 모의고사 (0) | 2021.12.30 |
---|---|
[Algorithm] 가장 큰 수 (0) | 2021.12.30 |
[Algorithm] 전화번호 목록 (0) | 2021.12.29 |
[Algorithm] 완주하지 못한 선수 (0) | 2021.12.29 |
[Algorithm] 단어변환 (0) | 2021.12.23 |