Last active
March 2, 2021 12:08
-
-
Save Stuff90/89ab82527f966f56a52bb2124d8725f9 to your computer and use it in GitHub Desktop.
Mark and Toys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BudgeOptimizer { | |
private _prices: number[]; | |
constructor(prices: number[], private _budget: number) { | |
this._prices = prices.sort((a, b) => a - b); | |
} | |
get count() { | |
let inc = 0; | |
let total = 0; | |
while (total <= this._budget) { | |
total += this._prices.shift() | |
if (total <= this._budget) { | |
inc++; | |
} | |
} | |
return inc; | |
} | |
} | |
function maximumToys(prices, k) { | |
return new BudgeOptimizer(prices, k).count; | |
} | |
let input = '1 12 5 111 200 1000 10'; | |
const prices = (input.split(' ').map(e => +e)); | |
console.log(maximumToys(prices, 50)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment