Skip to content

Instantly share code, notes, and snippets.

@Stuff90
Last active March 2, 2021 12:08
Show Gist options
  • Save Stuff90/89ab82527f966f56a52bb2124d8725f9 to your computer and use it in GitHub Desktop.
Save Stuff90/89ab82527f966f56a52bb2124d8725f9 to your computer and use it in GitHub Desktop.
Mark and Toys
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