총 3분이 걸렸습니다.
할인하는 방식을 잘못해서,, 삽질하는데 시간을 많이 날렸습니다.
class Solution {
fun solution(users: Array<IntArray>, emoticons: IntArray): IntArray {
// 각 할인의 따른 경우의 수를 저장할 배열
val pairAns = arrayListOf<Pair<Int,Int>>()
// dfs를 통해 각 할인의 경우의 수를 선택
val discounts = arrayListOf(10, 20, 30, 40)
val ans = Array(emoticons.size){0}
fun dfs(num: Int){
if (num == emoticons.size){
// 조건에 따라 서비스 가입 수, 구매 비용을 구한 후
// Pair 객체 형태로 배열에 저장
var totalPrice = 0
var totalCount = 0
for (i in users.indices){
val discount = users[i].first()
var price = users[i].last()
for (j in ans.indices){
if (ans[j] >= discount){
price -= emoticons[j] - (emoticons[j] / 100 * ans[j])
}
}
if (price <= 0){
totalCount += 1
}
else{
totalPrice += users[i].last() - price
}
}
pairAns.add(Pair(totalCount, totalPrice))
return
}
for (index in discounts.indices){
ans[num] = discounts[index]
dfs(num + 1)
}
}
dfs(0)
// 가입 수, 구매 비용 순으로 정렬
pairAns.sortWith(compareBy<Pair<Int,Int>>{it.first}.thenBy{it.second})
// 마지막 원소가 답
val value = pairAns.last()
return intArrayOf(value.first, value.second)
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[2023-06-17] [프로그래머스 lv1] 신고 결과 받기 (Kotlin) (0) | 2023.06.17 |
---|---|
[2023-06-17] [프로그래머스 lv1] 성격 유형 검사하기 (Kotlin) (0) | 2023.06.17 |
[2023-06-16] [프로그래머스 lv2] 택배 배달과 수거하기 (Kotlin) (0) | 2023.06.16 |
[2023-06-14] [프로그래머스 lv1] 개인정보 수집 유효기간 (Kotlin) (0) | 2023.06.14 |
[2023-06-14] [프로그래머스 lv1] 카드 뭉치 (Kotlin) (0) | 2023.06.14 |
댓글