Skip to content

Instantly share code, notes, and snippets.

@narender56
Created November 27, 2022 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narender56/be954b4fce6fda2ee83b490ed99168ea to your computer and use it in GitHub Desktop.
Save narender56/be954b4fce6fda2ee83b490ed99168ea to your computer and use it in GitHub Desktop.
interface Transaction {
id: string;
bank_country_code: string;
amount: number;
}
const latencies: Record<string, number> = {}
function prioritize(transactions: Transaction[], totalTime: number = 1000): Transaction[] {
const result: Transaction[] = [];
const transactionsWithTime = transactions.map(txn => ({
...txn,
amountPerTime: txn.amount / latencies[txn.bank_country_code],
time: latencies[txn.bank_country_code]
})).sort((txn1, txn2) => txn2.amountPerTime - txn1.amountPerTime);
let timeTaken = 0;
for (let i = 0; i < transactionsWithTime.length; i++) {
const transaction = transactionsWithTime[i];
timeTaken += transaction.time;
if(timeTaken > totalTime) {
break;
};
result.push({ id: transaction.id, bank_country_code: transaction.bank_country_code, amount: transaction.amount });
};
return result;
}
const totalTime = 1000;
const transactions: Transaction[] = [];
const subsetTransactions = prioritize(transactions, totalTime); // pass actual transactions here
console.log(`$${subsetTransactions.reduce((acc, curr) => acc + curr.amount, 0)} is the maximum USD transaction amount that can fit into the allotted time slots(${totalTime}ms)`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment