https://school.programmers.co.kr/learn/courses/30/lessons/150368
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
이모티콘 할인율 조합을 통해 사용자의 구매 패턴을 분석하고,
이모티콘 플러스 구독자 수와 총 수익을 최대화하는 로직을 짜 문제를 해결햇다.
각 이모티콘에 대해 10%, 20%, 30%, 40% 할인율 중 하나를 선택할 수 있으며,
이를 조합으로 구성해 모든 가능한 경우를 탐색한다.
combination 메서드는 이모티콘의 할인율 조합을 생성하는 역할을 한다.
재귀적으로 각 이모티콘의 할인율을 설정하며,
할인율 배열이 완성되면 check 메서드를 호출한다.
check 메서드는 현재 조합으로 사용자의 구매 패턴을 시뮬레이션한다.
각 사용자는 자신이 설정한 할인율 이상인 이모티콘만 구매하며,
구매 총액이 사용자의 기준 금액을 초과하면 이모티콘 플러스 구독자로 전환된다.
그렇지 않은 경우 구매 총액이 총 수익에 합산된다.
각 조합에 대한 결과는 플러스 구독자 수와 총 수익으로 나뉘며,
현재까지 계산된 최적의 결과와 비교해 더 나은 값이면 이를 answer 배열에 저장한다.
플러스 구독자 수를 우선으로 비교하고,
동일한 경우 총 수익을 기준으로 판단한다.
모든 조합을 탐색하는 완전탐색으로 구현하였다.
class Solution {
int[] answer = {0,0};
public void combination(int[] arr, int count, int[][] users, int[] emoticons){
if(count == emoticons.length){
check(arr,users,emoticons);
return ;
}
for(int i = 10; i <= 40; i+=10){
arr[count] = i;
combination(arr,count+1,users,emoticons);
}
}
public void check(int[] arr, int[][] users, int[] emoticons){
int plus = 0;
int money = 0;
for(int i = 0 ; i < users.length ; i++){
int mem = 0;
for(int j = 0 ; j < emoticons.length; j++){
if(users[i][0]<=arr[j]){
mem += (emoticons[j]/100) * (100-arr[j]);
}
}
if(users[i][1] <= mem){
plus++;
}else{
money += mem;
}
}
if(answer[0] < plus || (answer[0] == plus && answer[1] < money)){
answer[0] = plus;
answer[1] = money;
}
}
public int[] solution(int[][] users, int[] emoticons) {
int[] arr = new int[emoticons.length];
// 조합짜기
combination(arr,0,users,emoticons);
// 짠조합 체크 및 결과저장
return answer;
}
}
'Algorithm & Data Structures > BOJ' 카테고리의 다른 글
b6549. 히스토그램에서 가장 큰 직사각형 (0) | 2025.01.06 |
---|---|
b12100. 2048(Easy) (0) | 2025.01.03 |
b9466. 텀 프로젝트 (0) | 2024.12.31 |
b9295. LCS2 (1) | 2024.12.27 |
b7579. 앱 (0) | 2024.12.26 |