문제풀이/기타

[JAVA] Pro 이모티콘 할인행사

승무_ 2023. 7. 7. 15:35

문제

https://school.programmers.co.kr/learn/courses/30/lessons/150368?language=java 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

코드

import java.util.*;

class Solution {
    ArrayList<emo> arr=new ArrayList<>();
    
    class emo{
        int count;
        int price;
        emo(int count, int price){
            this.count=count;
            this.price=price;
        }
    }
    
    public void dfs(int n, String s, int[][] users, int[] emoticons){
        if(n==emoticons.length){
            int[] u=new int[users.length];
            String[] str=s.split(" ");
            
            for(int i=1; i<str.length; i++){
                for(int j=0; j<users.length; j++){
                    // 자신의 기준보다 이모티콘 할인률이 높으면 구매
                    if(Integer.parseInt(str[i])>=users[j][0]){
                        u[j]+=emoticons[i-1]/100*(100-Integer.parseInt(str[i]));
                    }   
                }
            }
            int tempCount=0;
            int tempPrice=0;
            for(int i=0; i<users.length; i++){
                // 구매 비용의 합이 자신의 기준보다 높으면 플러스 서비스 가입
                if(u[i]>=users[i][1]){
                    tempCount+=1;
                }
                else{
                    tempPrice+=u[i];
                }
            }
            arr.add(new emo(tempCount, tempPrice));
            return;
        }
        dfs(n+1,s+" 10", users, emoticons);
        dfs(n+1,s+" 20", users, emoticons);
        dfs(n+1,s+" 30", users, emoticons);
        dfs(n+1,s+" 40", users, emoticons);
    }
    
    
    public int[] solution(int[][] users, int[] emoticons) {
        dfs(0,"", users, emoticons);
        
        // 삼항 연산자를 이용해 count가 다르면 count 내림차순, 같으면 price 내림차순
        Collections.sort(arr, (emo o1,emo o2) -> {
            return o1.count!=o2.count ? o2.count-o1.count : o2.price-o1.price; 
        });
        
        // 첫번째 요소 return
        return new int[] {arr.get(0).count,arr.get(0).price};
    }
}