https://school.programmers.co.kr/learn/courses/30/lessons/150368
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
기억해야하는점들입니다.
첫번째, 중복순열을 구합니다.
이 문제는 discountrate = {40, 20, 30, 10} 으로 고정되어 있습니다.
그러므로 중복순열을 구하여 해당 이모티콘 값마다 할인율을 구해주면 됩니다.
두번째, 할인율을 구하는 방법입니다.
처음에 만약에 10000원이 있다고 가정합니다.
이떄 40퍼센트의 할인율을 구하면 10000 - 10000 * 40 / 100 = 10000 - 4000 으로 6000이 되는데
처음에 10000 * 40 / 100 = 6000 으로 계산하여 많은 시간을 잡아먹었습니다.
for(int j=0;j<output.length;j++){
if(user_discountrate <= output[j]){
sum += Emoticons[j] - (Emoticons[j] * output[j] / 100);
// System.out.print("calculatedprice:"+output[j] * Emoticons[j] / 100);
// System.out.print("discountrate: "+output[j]+"emoticonprice:"+Emoticons[j] +" calculatedprice: "+output[j] * Emoticons[j] / 100);
}
}
코드입니다.
class Solution {
int[] discountrate = {40,20,30,10};
int[] output;
int[][] Users;
int[] Emoticons;
int answer_signupcnt=0;
int answer_profitsum = 0;
public int[] solution(int[][] users, int[] emoticons) {
int[] answer = {};
Users = users;
output = new int[emoticons.length];
Emoticons = emoticons;
permutationwithrepetition(0, emoticons.length);
answer = new int[]{answer_signupcnt, answer_profitsum};
return answer;
}
void permutationwithrepetition(int depth, int n){
if(n == depth){
int signupcnt = 0;
int profitsum = 0;
for(int i=0;i<Users.length;i++){
int user_discountrate = Users[i][0];
int user_emoticonplus_price = Users[i][1];
// System.out.println("user discountrate: "+user_discountrate+" emoticonplus_price:"+user_emoticonplus_price);
int sum = 0;
for(int j=0;j<output.length;j++){
if(user_discountrate <= output[j]){
sum += Emoticons[j] - (Emoticons[j] * output[j] / 100);
// System.out.print("calculatedprice:"+output[j] * Emoticons[j] / 100);
// System.out.print("discountrate: "+output[j]+"emoticonprice:"+Emoticons[j] +" calculatedprice: "+output[j] * Emoticons[j] / 100);
}
}
// System.out.println("sum:"+sum);
if( sum >= user_emoticonplus_price){
signupcnt += 1;
}else{
profitsum += sum;
}
}
// System.out.println(" "+signupcnt+" "+profitsum);
if(answer_signupcnt < signupcnt){
answer_signupcnt = signupcnt;
answer_profitsum = profitsum;
}else if(answer_signupcnt == signupcnt){
if(answer_profitsum < profitsum){
answer_profitsum = profitsum;
}
}else{
}
return ;
}
for(int i=0;i<discountrate.length;i++){
output[depth] = discountrate[i];
permutationwithrepetition(depth+1, n);
}
}
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스]괄호 회전하기 - 구현 + 스택 (0) | 2023.01.23 |
---|---|
[프로그래머스] 가장 먼 노드 - 다익스트라 + 그래프 (0) | 2023.01.10 |
[카카오 기출 문제]택배 배달과 수거하기 - 레벨 2, 그리디 + 구현 (0) | 2023.01.07 |
[카카오 기출 문제]블록 이동하기 - 레벨 3, BFS (1) | 2023.01.06 |
[카카오 기출 문제]불량 사용자- 레벨 3, 해쉬 + 이중해쉬 + 순열 + 조합 (0) | 2022.12.15 |