728x90
반응형
목차
문제
https://school.programmers.co.kr/learn/courses/30/lessons/150368
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
문제 구현 방향
이모티콘의 개수는 최대 7개 할인율 가지수는 4개이므로 7^4승 밖에 되지 않아 브루트포스로 해결할 수 있다.
이때 배열에 원소를 추가하고 끝에 도달했을 때 pop해서 원상 복구를 하지 않으면 length를 초과해 maxium call stack이 뜨게 되니 주의 하자
코드 구현
var saleT =[10, 20, 30, 40];
var answer = [];
function solution(users, emoticons) {
answer.push(0);
answer.push(0);
let sales =[];
dfs(users, emoticons, sales);
return answer;
}
function dfs(users, emoticons, sales){
if(sales.length === emoticons.length){
let register =0;
let sum =0;
for(let u of users){
let [percent, price] = u;
let total =0;
for(let i=0; i< emoticons.length; i++){
if(sales[i] >= percent){
total += emoticons[i] - Math.floor(emoticons[i] /100 * sales[i]);
}
}
if(total >=price){
register++;
total =0;
}
sum += total;
}
if(answer[0] < register) answer = [register, sum];
else if( answer[0] === register) answer = [register, Math.max(sum, answer[1])];
return;
}
for(let i = 0; i< 4; i++){
sales.push(saleT[i]);
dfs(users, emoticons, sales);
sales.pop();
}
}반응형
'PS > 프로그래머스' 카테고리의 다른 글
| [프로그래머스][dp] 숫자 변환하기 (0) | 2025.09.11 |
|---|---|
| [프로그래머스][bfs] 미로 탈출 명령어 (0) | 2025.09.09 |
| [프로그래머스][bfs] 리코쳇 로봇 bfs 구현 (1) | 2025.09.01 |
| [프로그래머스][그리디] 요격 시스템 (1) | 2025.09.01 |
| [프로그래머스][그리디] 광물캐기 (5) | 2025.07.15 |