did_story
[ํ๋ก๊ทธ๋๋จธ์ค / JAVA] ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ. ๋ณธ๋ฌธ
[ํ๋ก๊ทธ๋๋จธ์ค / JAVA] ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ.
์ด์ ์์ 2025. 10. 12. 15:18https://school.programmers.co.kr/learn/courses/30/lessons/150368
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
1์๊ฐ๋์์ ๊ณ ๋ฏผ ํ ํ๋ฆฌ์ง ์์์ ๋ค๋ฅธ ๋ถ (https://trillium.tistory.com/163)์ ์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ํผ ๊ฒ์ ์์ฑํด๋ณธ๋ค.
์ค๋๋ ๋ชปํจ์ ๊นจ๋ซ๊ณ ํ๋จ๊ณ ์ฑ์ฅํด๋ณด์
๋ฌธ์ ์ค๋ช

๋ฌธ์ ํ์ด
! ์ฒ์ ๋ด๊ฐ ์๊ฐํ๋ ๋ฌธ์ ํ์ด
๋ณด๊ณ ์๊ฐํ ๊ฒ์ ์์ ํ์์ผ๋ก ํ๋ฉด ๋์ง ์์๊น ์๊ฐํ๋ค. ์ด์ ๋
- ์๊ฐ ๋ณต์ก๋ O(n*4^m) (n : ์นด์นด์คํก ์ฌ์ฉ์, m : ์ด๋ชจํฐ์ฝ์ ๊ฐฏ์) ์ด๋ผ์ ์ต๋ O(1638400)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ๋ ๋ค๊ณ ์๊ฐํ๊ณ ์ด๋ 10์ด, 2GB memmory ์์์ ๊ฐ๋ฟํ ๋ ๊ฒ์ด๋ผ ์๊ฐํ๋ค.
- ๋ฐ๋ผ์ ์ฌ๊ท๋ฅผ ์ด์ฉํด์ ํ๋ฉด ๋์ง ์์๊น ์๊ฐํ๋ค.
BUT, 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;
}
}
}
๊ณ ์ฐฐ
์นด์นด์ค ์ฝ๋ฉํ ์คํธ๋ฅผ ์ฐ์ตํ๊ณ , ๊ณต๋ถํ๋ฉด์ ๋ด๊ฐ ์๋ฐ์ ๋ํด์ ์ ํํ๊ฒ ์๊ณ ์์ง ์๊ตฌ๋๋ผ๊ณ ์๊ฐํ๋ค. ๋ ๋ฐฐ์ธ๊ฒ ๋ง๊ณ ๊ธฐ์ตํ๊ณ ์๋๊ฒ ๋ง์๋ฐ, ๊ณต๋ถํ์ง ์์๋๊ฑด๊ฐ ์๊ฐํ๋ค. ๊ฐ์ฒด์งํฅ ์ฝ๋๋ฅผ ๋ง์ด ์ข์ํ๋ค๊ณ ๋งํ ๋ด๊ฐ ์ค์ค๋ก ๋ถ๋๋ฌ์์ ์ป์ ์ ๋๋ก ์ด๋ฒ ๋ฌธ์ ๋ฅผ ํ ๋ ์์ฌ์์ด ๋จ๋๋ค..
'Algorithm๐ > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [ํ๋ก๊ทธ๋๋จธ์ค / JAVA] ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ (0) | 2026.05.14 |
|---|---|
| [ํ๋ก๊ทธ๋๋จธ์ค / JAVA] ํ ํธ์ง (0) | 2025.10.09 |
| [ํ๋ก๊ทธ๋๋จธ์ค / JAVA] ๋๋๊ณผ ๋ง๋ ๊ทธ๋ํ. (0) | 2025.09.30 |
| [ํ๋ก๊ทธ๋๋จธ์ค / Java] ๊ฑฐ๋ฆฌ๋๊ธฐ ํ์ธํ๊ธฐ (0) | 2025.09.29 |
| [ํ๋ก๊ทธ๋๋จธ์ค / ์๋ฐ] ๊ณต์์ฐ์ฑ (0) | 2025.09.12 |