코딩테스트 연습 - K번째수
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
풀이
주어진 요구사항 그대로 구현하면 되는 쉬운 문제이다.
map을 사용해서 다시 풀어보기!
코드
import Foundation
func solution(_ array:[Int], _ commands:[[Int]]) -> [Int] {
var ans: Array<Int> = []
for i in commands {
var temp: Array<Int> = []
for j in i[0]...i[1] {
temp.append(array[j-1])
}
temp.sort()
ans.append(temp[i[2]-1])
}
return ans
}
'Study > 알고리즘' 카테고리의 다른 글
[프로그래머스] 12915. 문자열 내 마음대로 정렬하기 (swift) (0) | 2020.07.23 |
---|---|
[프로그래머스] 12912. 두 정수 사이의 합 (swift) (0) | 2020.07.21 |
[프로그래머스] 12903. 가운데 글자 가져오기 (swift) (0) | 2020.07.21 |
MySQL 정리 (0) | 2020.06.04 |
[프로그래머스] 12979. 기지국 설치 (C++) (0) | 2020.05.29 |