Created
January 2, 2025 14:14
-
-
Save PetukhovArt/73a158382ce38eac65f3b229cd72eabe to your computer and use it in GitHub Desktop.
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
// Дан массив банкнот определенных номиналов | |
// [1, 2, 5, 10, 20, 50], | |
// их количество не ограничено. | |
// Нужно выдать ему эти деньги используя минимальное количество купюр. | |
// reduceRight simple | |
const getCash1 = (bills, total) => { | |
let totalRemain = total; | |
return bills.reduceRight((acc, bill) => { | |
const billCount = Math.floor(totalRemain / bill); | |
if (billCount > 0 && totalRemain > 0) { | |
totalRemain = totalRemain - billCount * bill; | |
const billsToAdd = Array.from({length: billCount}, () => bill); | |
acc.push(...billsToAdd); | |
} | |
return acc; | |
}, []); | |
}; | |
// sort + forEach simple | |
const getCash2 = (bills, total) => { | |
const result = []; | |
const sorted = bills.sort((a, b) => b - a); | |
let totalRemain = total; | |
sorted.forEach((bill) => { | |
const billCount = Math.floor(totalRemain / bill); | |
if (billCount > 0 && totalRemain > 0) { | |
totalRemain = totalRemain - billCount * bill; | |
const billsToAdd = Array.from({length: billCount}, () => bill); | |
result.push(...billsToAdd); | |
} | |
}); | |
return result; | |
}; | |
// with additional checks | |
const getCash = (bills, total) => { | |
let totalRemain = total; | |
const fullExchangeBills = [] | |
const result = bills.sort((a, b) => b - a).reduce((acc, bill, idx) => { | |
const billCount = Math.floor(totalRemain / bill) | |
const canExchangeTotal = Number.isInteger(total / bill) | |
if (canExchangeTotal) { | |
fullExchangeBills.push([bill, total / bill]) | |
} | |
if (billCount > 0 && totalRemain > 0) { | |
totalRemain = totalRemain - billCount * bill; | |
const billsToAdd = Array(billCount).fill(bill) | |
acc.push(...billsToAdd); | |
} | |
// если на последней итерации остался остаток, значит нельзя выдать по простому алгоритму | |
// выдать одинаковыми купюрами | |
if (idx === bills.length - 1 && totalRemain > 0) { | |
const fullExchangeBill = fullExchangeBills?.[0]?.[0] | |
if (!fullExchangeBill) { | |
return acc | |
} | |
const fullBillCount = fullExchangeBills[0][1] | |
const fullBills = Array(fullBillCount).fill(Number(fullExchangeBill)) | |
totalRemain = 0 | |
return fullBills | |
} | |
return acc; | |
}, []) | |
if (totalRemain > 0) { | |
return 'cant change' | |
} | |
return result | |
}; | |
console.log(getCash([1, 2, 5, 10, 20, 50], 91)); // [ 50, 20, 20, 1 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 82)); // [ 50, 20, 10, 2 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 73)); // [ 50, 20, 2, 1 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 40)); // [ 20, 20 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 12)); // [ 10, 2 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 3)); // [ 2, 1 ] | |
console.log(getCash([1, 2, 5, 10, 20, 50], 1)); // [ 1 ] | |
// additional | |
console.log(getCash([2, 3, 5, 50], 91)); // can't | |
console.log(getCash([200, 500], 600)); // [200,200,200] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment