did_story

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค / JAVA] ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธํ–‰์‚ฌ. ๋ณธ๋ฌธ

Algorithm๐ŸŒŠ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Programmers)

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค / JAVA] ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธํ–‰์‚ฌ.

์–ด์ œ์‹œ์ž‘ 2025. 10. 12. 15:18

https://school.programmers.co.kr/learn/courses/30/lessons/150368

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก์˜ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

1์‹œ๊ฐ„๋™์•ˆ์˜ ๊ณ ๋ฏผ ํ›„ ํ’€๋ฆฌ์ง€ ์•Š์•„์„œ ๋‹ค๋ฅธ ๋ถ„ (https://trillium.tistory.com/163)์˜ ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ํ‘ผ ๊ฒƒ์„ ์ž‘์„ฑํ•ด๋ณธ๋‹ค.

์˜ค๋Š˜๋„ ๋ชปํ•จ์„ ๊นจ๋‹ซ๊ณ  ํ•œ๋‹จ๊ณ„ ์„ฑ์žฅํ•ด๋ณด์ž

๋ฌธ์ œ ์„ค๋ช…

๋ฌธ์ œ ํ’€์ด

! ์ฒ˜์Œ ๋‚ด๊ฐ€ ์ƒ๊ฐํ–ˆ๋˜ ๋ฌธ์ œ ํ’€์ด

๋ณด๊ณ  ์ƒ๊ฐํ•œ ๊ฒƒ์€ ์™„์ „ํƒ์ƒ‰์œผ๋กœ ํ’€๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค. ์ด์œ ๋Š”

  1. ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n*4^m) (n : ์นด์นด์˜คํ†ก ์‚ฌ์šฉ์ž, m : ์ด๋ชจํ‹ฐ์ฝ˜์˜ ๊ฐฏ์ˆ˜) ์ด๋ผ์„œ ์ตœ๋Œ€ O(1638400)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š” ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ  ์ด๋Š” 10์ดˆ, 2GB memmory ์ƒ์—์„œ ๊ฐ€๋ฟํžˆ ๋ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ–ˆ๋‹ค.
  2. ๋”ฐ๋ผ์„œ ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค.

BUT, 1์‹œ๊ฐ„์˜ ๊ณ ๋ฏผ ์‚ฌ์œ 

  1. rate ์™€ price๋ฅผ ๊ฐ™์ด ๊ด€๋ฆฌ๋ฅผ ํ•  ๊ฒƒ์ธ๋ฐ ์–ด๋–ค์‹์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋Š”๊ฒŒ ์ข‹์„๊นŒ? (ArrayList? ์ƒˆ๋กœ์šด Class?)

๋‚œ ์—ฌ๊ธฐ์„œ ์ž๋ฐ”๋ฅผ ์™„์ „ ๋ชจ๋ฅด๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ๋А๊ผˆ๋‹ค. 

 

Trillium ๋‹˜์˜ ๊ธ€๋กœ ์ธํ•œ ํ•ด๊ฒฐ 

...
static List<Emoticon> emoList = new ArrayList<>();

static class Emoticon {
        int rate; // ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธ์œจ.
        int price; // ํ• ์ธ์œจ์„ ์ ์šฉํ•œ ๊ฐ€๊ฒฉ.
        
        Emoticon(int rate, int price){
            this.rate = rate;
            this.price = price;
        }
    }
}

→ Emoticon ์ด๋ผ๋Š” class๋ฅผ ์ƒ์„ฑ ํ›„, emoList๋ผ๋Š” List์˜ ์ƒ์„ฑ์œผ๋กœ ํ•œ๋ฒˆ์— ๊ด€๋ฆฌํ•˜๋Š” ๋ฒ•. ํ•œ์ˆ˜ ๋ฐฐ์›๋‹ˆ๋‹ค ...

 

Code

import java.util.*;

class Solution {
    static int[] discountRate = {10, 20, 30, 40};
    static int[] answer = new int[2];
    static List<Emoticon> emoList = new ArrayList<>();
    
    public int[] solution(int[][] users, int[] emoticons) {
        recurEmoticon(users, emoticons, 0);
        return answer;
    }
    
    static void recurEmoticon(int[][] users, int[] emoticons, int index){
        if(index == emoticons.length){
            int[] result = proceedUser(users);
            // ์šฐ์„ ์ˆœ์œ„ 1.
            if (answer[0] < result[0]){
                answer = result.clone();
            }
            // ์šฐ์„ ์ˆœ์œ„ 2.
            if (answer[0] == result[0] && answer[1] < result[1]){
                answer = result.clone();
            }
            return;
        }
        
        for (int i = 0; i < 4; i++){
            int rate = discountRate[i];
            int discountPrice = emoticons[index] * (100 - rate) / 100;
            
            // ๋‹ค๋ฅธ rate์˜ emoticon ๊ฐ€๊ฒฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์“ฐ๊ธฐ์œ„ํ•จ. add, remove
            emoList.add(new Emoticon(rate, discountPrice));
            recurEmoticon(users, emoticons, index + 1);
            emoList.remove(emoList.size() - 1);
        }
    }
    
    static int[] proceedUser(int[][] users){
        int plusMember = 0;
        int totalPrice = 0;
        
        for (int i = 0; i < users.length ; i++){
            int userWishRate = users[i][0];
            int userMaxPrice = users[i][1];
            
            int userPurchasePrice = 0;
            for (Emoticon emo : emoList){
                if(userWishRate <= emo.rate){
                    userPurchasePrice += emo.price;
                }
            }
            
            // ํ—ˆ์šฉ๊ฐ€๊ฒฉ ์ด์ƒ ๊ตฌ๋งคํ•˜๋Š”์ง€ ํŒ๋‹จ. 
            if (userPurchasePrice >= userMaxPrice){
                plusMember ++;
            } else {
                totalPrice += userPurchasePrice;
            }
            
        }
        
        return new int[]{plusMember, totalPrice};
    }
    
    static class Emoticon {
        int rate; // ์ด๋ชจํ‹ฐ์ฝ˜ ํ• ์ธ์œจ.
        int price; // ํ• ์ธ์œจ์„ ์ ์šฉํ•œ ๊ฐ€๊ฒฉ.
        
        Emoticon(int rate, int price){
            this.rate = rate;
            this.price = price;
        }
    }
}

๊ณ ์ฐฐ

์นด์นด์˜ค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์—ฐ์Šตํ•˜๊ณ , ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋‚ด๊ฐ€ ์ž๋ฐ”์— ๋Œ€ํ•ด์„œ ์ •ํ™•ํ•˜๊ฒŒ ์•Œ๊ณ  ์žˆ์ง€ ์•Š๊ตฌ๋‚˜๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๋” ๋ฐฐ์šธ๊ฒŒ ๋งŽ๊ณ  ๊ธฐ์–ตํ•˜๊ณ  ์žˆ๋Š”๊ฒŒ ๋งŽ์€๋ฐ, ๊ณต๋ถ€ํ•˜์ง€ ์•Š์•˜๋˜๊ฑด๊ฐ€ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ฐ์ฒด์ง€ํ–ฅ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ข‹์•„ํ•œ๋‹ค๊ณ  ๋งํ•œ ๋‚ด๊ฐ€ ์Šค์Šค๋กœ ๋ถ€๋„๋Ÿฌ์›€์„ ์–ป์„ ์ •๋„๋กœ ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์•„์‰ฌ์›€์ด ๋‚จ๋Š”๋‹ค..