본문 바로가기

알고리즘

Mark and Toys javascript로 풀기

728x90

/*

Mark and Jane are very happy after having their first child.

마크와 제인은 뒤늦게 그들의 첫번째 자식이 생겨 매우 행복하다.

 

Their son loves toys, so Mark wants to buy some. 

그들의 아들은 장난감을 사랑한다, 마크는 그것을 사길 원한다.

 

There are a number of different toys lying in front of him, tagged with their prices. 

숫자를 가진 각각의 장난감이 그의 앞에 놓여있고, 그들의 가격이 붙어있다. 

 

Mark has only a certain amount to spend, and he wants to maximize the number of toys he buys with this money.

마크는 써야 할 액수가 정해져 있고, 그는 그 돈으로 장난감을 살 수 있는 최대값을 원한다 

 

풀이

 

그냥 정렿해서 싹다 더하면 되지않을까?

-> 걍 하니까 되네 별거 없었다.

*/

var prices = [1125111200100010];

var k = 50;

function maximumToys(pricesk) {

    prices.sort((a,b=> {

        return a-b

    })

    var max = 0;

    var cnt = 0;

    for(var i=0;i<prices.length;i++){

        if(max>k)

            break;

        max +=prices[i];

        cnt++;

    }

    return cnt-1;

}

maximumToys(pricesk)

 

728x90