본문 바로가기
알고리즘/프로그래머스

[2023-06-16] [프로그래머스 lv2] 이모티콘 할인행사 (Kotlin)

by joh9911 2023. 6. 16.

 

총 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)
    }
}

댓글