import Foundation
func solution(_ k:Int, _ tangerine:[Int]) -> Int {
var tmp: [Int:Int] = [:]
var sum = 0
for tan in tangerine {
tmp[tan, default: 0] += 1
}
let sortedValue = tmp.sorted(by: { $0.value > $1.value })
var answer = 0
for (index, value) in sortedValue.enumerated() {
sum += value.value
if sum >= k {
answer = index + 1
break
}
}
return answer
}
func solution(_ k: Int, _ tangerine: [Int]) -> Int {
return Dictionary(grouping: tangerine) { $0 }.values
.sorted { $0.count > $1.count }
.reduce((0, 0)) { acc, array in acc.1 >= k ? acc : (acc.0 + 1, acc.1 + array.count) }
.0
}
'Algorithm > programmers' 카테고리의 다른 글
프로그래머스 - 가운데 글자 가져오기 (0) | 2024.11.15 |
---|---|
프로그래머스 - 연속 부분 수열 합의 개수, feat. Set (0) | 2024.10.30 |
프로그래머스 - 영어 끝말잇기 (0) | 2024.10.29 |
프로그래머스 - 멀리 뛰기 feat. DP (0) | 2024.10.27 |
프로그래머스 - 점프와 순간 이동 feat. 최소 연산횟수 (0) | 2024.10.20 |
import Foundation
func solution(_ k:Int, _ tangerine:[Int]) -> Int {
var tmp: [Int:Int] = [:]
var sum = 0
for tan in tangerine {
tmp[tan, default: 0] += 1
}
let sortedValue = tmp.sorted(by: { $0.value > $1.value })
var answer = 0
for (index, value) in sortedValue.enumerated() {
sum += value.value
if sum >= k {
answer = index + 1
break
}
}
return answer
}
func solution(_ k: Int, _ tangerine: [Int]) -> Int {
return Dictionary(grouping: tangerine) { $0 }.values
.sorted { $0.count > $1.count }
.reduce((0, 0)) { acc, array in acc.1 >= k ? acc : (acc.0 + 1, acc.1 + array.count) }
.0
}
'Algorithm > programmers' 카테고리의 다른 글
프로그래머스 - 가운데 글자 가져오기 (0) | 2024.11.15 |
---|---|
프로그래머스 - 연속 부분 수열 합의 개수, feat. Set (0) | 2024.10.30 |
프로그래머스 - 영어 끝말잇기 (0) | 2024.10.29 |
프로그래머스 - 멀리 뛰기 feat. DP (0) | 2024.10.27 |
프로그래머스 - 점프와 순간 이동 feat. 최소 연산횟수 (0) | 2024.10.20 |