알고리즘 문제풀이/프로그래머스

[프로그래머스 2023 KAKAO BLIND RECRUITMENT] 이모티콘 할인행사

선서니 2023. 6. 19. 16:28

 

[문제 바로가기]👇

 

프로그래머스

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

programmers.co.kr

 

💡풀이💡

1. 이모티콘 별 할인율을 완전탐색

  • 이모티콘은 10% 20% 30% 40%을 가질 수 있고 순서가 있고 따라 중복이 가능 :: 중복 순열!
  • 중복 순열로 가능한 모든 경우의 수를 완전 탐색 

2. 이모티콘 별 구매 비용 계산

  • 가능한 모든 경우의 수 중 하나의 경우가 완성이 되었을 때, 이모티콘 별 구매 비용을 계산
  • 구매하는 이모티콘의 비용의 합이 사용자 별 금액 기준보다 크면 서비스 가입

 

전체 코드

import java.io.*;
import java.util.*;

class Solution {  
    int N;
    int[] sale = {10, 20, 30, 40};
    int[] res;
    int[] answer = {0, 0};
    
    public int[] solution(int[][] users, int[] emoticons) {        
        N = emoticons.length;
        res = new int[N]; // 중복 순열 결과 저장
        perm(0, users, emoticons); // 중복 순열 
        
        return answer;
    }
    
    public void perm(int cnt, int[][] users, int[] emoticons) {
        if(cnt == N) {
            // System.out.println(Arrays.toString(res));
            
            // 이모티콘별 구매 비용 계산
            calEmoticons(users, emoticons);
            return;
        }
        
        // 10% 20% 30% 40% 이모티콘 별 할인율을 중복 순열로 완전탐색
        for(int i=0; i<sale.length; i++) {
            res[cnt] = sale[i];
            perm(cnt+1, users, emoticons);
        }
    }
    
    public void calEmoticons(int[][] users, int[] emoticons) {
        int join = 0; // 서비스 가입자 수
        int total = 0; // 이모티콘 판매액
        
        for(int i=0; i<users.length; i++) {
            int pay = 0;
            
            for(int j=0; j<emoticons.length; j++) {
                if(users[i][0] <= res[j]) { // 구매 하는 이모티콘 비용 계산
                    pay += emoticons[j] * (100 - res[j]) / 100;
                }
            }
            
            // 구매 비용이 기준 초과 :: 구매 모두 취소, 서비스 가입
            if(pay >= users[i][1]) {
                pay = 0;
                join++;
            }
            total += pay;
        }
        
        // 결과 저장
        if(answer[0] < join) {
            answer[0] = join;
            answer[1] = total;
        }
        else if(answer[0] == join) {
            answer[1] = Math.max(answer[1], total);
        }
    }
}