728x90

https://school.programmers.co.kr/learn/courses/30/lessons/150368?language=javascript

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

function solution(users, emoticons) {
    let answer = [0, 0];

    function discountCombinations(discounts, numProducts) {
    
        function generateCombo(currentCombo, depth) {
            if (depth === numProducts) {
                let plus = 0, sell = 0; //플러스 가입자수, 총 판매금액 
                
                //reduce를 사용해 이모티콘 총 구매금액을 계산 
                for (let user of users) {
                    let total = emoticons.reduce((sum, price, c) => 
                        sum + (currentCombo[c] >= user[0] ? price * (1 - currentCombo                         [c] * 0.01) : 0), 0
                    );
                    //총 구매한 금액이 목표금액보다 비싼경우 구매를 취소하고 플러스 가입 
                    if (total >= user[1]) plus++;
                    else sell += total;
                }
                //그 경우의 수 마다 max값을 비교해서 최적의 결과를 answer에 넣어줌 
                if (plus > answer[0] || (plus === answer[0] && sell > answer[1])) {
                    answer = [plus, sell];
                }
                return;
            }
            //재귀
            for (let discount of discounts) {
                generateCombo([...currentCombo, discount], depth + 1);
            }
        }
        generateCombo([], 0);
        
    }

    discountCombinations([10, 20, 30, 40], emoticons.length);
    return answer;
}

 

재귀함수를 사용해서 각 이모티콘별로 할인되는 모든 경우의 수를 구하고, 그 경우의 수 중 max 값을 찾는 

무식한 방법을 사용했다 ㅎㅎ ... 

이모티콘의 갯수가 많지 않고 할인은 10 20 30 40 단 네가지의 경우의 수만 존재하기때문에 가능했던 풀이 

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/148653#qna

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

탐욕Greedy 알고리즘을 사용해 풀어보자 

 

자신의 자리가 5 이상이라면 올림으로 처리하는것이 더 낫다 

e.g) 95인 경우에는 10*9 + 1*5 총 14회 보다 , 100*1 - 1*5 해서 총 6회로 나오는것이 더 적게 든다 

 

function solution(storey) {
    let ary = String(storey).split("").map(Number); 
    let carry = 0; //올림수
    let total = 0; 

    for (let i = ary.length - 1; i >= 0; i--) { 
        let current = ary[i] + carry;

        if (current > 5) {
            carry = 1;
            total += 10 - current;
        } else if (current === 5 && i > 0 && ary[i - 1] >= 5) {
            carry = 1;
            total += 5;
        } else {
           
            carry = 0;
            total += current;
        }
    }

  
    if (carry > 0) {
        total += carry;
    }

    return total;
}

 

555의 경우에는 올림을 해서 계산을 할 때와 올림을 적용하지 않았을때 둘 다 15회로 값이 동일하다 

728x90

https://school.programmers.co.kr/learn/courses/30/lessons/70129

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

function solution(s) {
    let cnt=0;
    let answer = s;
    let zero = 0;
    
    while(answer!=='1'){
        let findZero = answer.split('0').length - 1
        let editNum = answer.replaceAll("0",'');
        answer = editNum.length.toString(2);
     
        cnt++;
        zero+=findZero;
    }
    return [cnt,zero];
}

 

javascript의 여러 메서드들을 연습해볼 수 있는 문제군 ~.~

728x90

+ Recent posts