Cookies help us deliver our services. By using our services, you agree to our use of cookies. Click here to learn more

Interview Question

Analyst Interview(Student Candidate) New York, NY

There are 3 types of coins. The values are

  1, 2, 5, respectively. What's the algorithm to get the minimun number of coins if the sum of them is N?
Tags:
technical, algorithm
Answer

Interview Answer

2 Answers

0

dynamic programming

Interview Candidate on 5 Sep 2012
0

(function foo(n){
    var coins = [1,2,5]
    var num = 0;
    while (coins.length){
        var coin = coins.pop()
        num+= Math.floor(n/coin);
        n=n%coin;
        if(!n) break;
    }
    console.log(num);
})(28)

grim face on 7 Sep 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.