https://school.programmers.co.kr/learn/courses/30/lessons/150368
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제설명
- {10, 20, 30, 40}의 할인율을 임의로 선택하여서 아래 목표 최대값을 구하는 문제이다.
- 이모티콘 플러스 서비스 가입자를 최대한 늘리는 것.
- 이모티콘 판매액을 최대한 늘리는 것.
1번 목표가 우선이며, 2번 목표가 그 다음입니다.
문제풀이
1. 이모티콘의 할인율 정하기 -> permutation 함수를 사용해 output에 할인율을 담아준다.
2. 할인율이 정해지면, 구매자별 조건 대조 -> check 함수
3. 값은 HashMap<가입자수, 구매액> 으로 담아준다.
import java.util.*;
class Solution {
//할인율
static int[] discount = {10, 20, 30, 40};
static int cnt, sum;
static HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
public int[] solution(int[][] users, int[] emoticons) {
int[] answer = new int[2];
int n = emoticons.length;
int[] output = new int[n];
boolean[] visited = new boolean[n];
permutation(users, emoticons, 0, output, visited, n);
for(int i : map.keySet()) {
cnt = Math.max(i, cnt);
}
sum = map.get(cnt);
answer[0] = cnt;
answer[1] = sum;
return answer;
}
public void permutation(int[][] users, int[] emoticons, int depth, int[] output, boolean[] visited, int n){
if(depth == n){
check(users, emoticons, output);
return;
}
for(int i = 0; i < 4; i++){
if(!visited[depth]){
visited[depth] = true;
output[depth] = discount[i];
permutation(users, emoticons, depth+1, output, visited, n);
}
visited[depth] = false;
}
}
public void check(int[][] users, int[] emoticons, int[] output){
int count = 0;
int hap = 0;
for(int i = 0; i < users.length; i++){
int dis = users[i][0];
int price = users[i][1];
int total = 0;
for(int j = 0; j < output.length; j++){
if(output[j] >= dis){
int tmp = emoticons[j] * (100-output[j])/100;
total += tmp;
}
}
//가입자 늘림
if(total >= price) {
count++;
//판매액
} else {
hap += total;
}
}
if(map.containsKey(count)) {
int tmp = map.get(count);
if(hap > tmp) {
map.replace(count, hap);
}
} else {
map.put(count, hap);
}
}
}
'코딩테스트 > programmers' 카테고리의 다른 글
[level2] 롤케이크 자르기 JAVA (0) | 2023.01.05 |
---|---|
[level] 점찍기 JAVA (0) | 2023.01.04 |
[level2]마법의 엘리베이터 JAVA (0) | 2023.01.04 |
[level3] 야근지수 JAVA (0) | 2022.11.24 |
[level3] 정수 삼각형 JAVA (0) | 2022.11.21 |