From 7305b1f7dd96223b999670c4988cee286987f156 Mon Sep 17 00:00:00 2001 From: TK Date: Tue, 12 Dec 2023 07:57:14 -0300 Subject: [PATCH] * --- .../optimal-freelancing.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 coding_interviews/algoexpert/optimal-freelancing/optimal-freelancing.js diff --git a/coding_interviews/algoexpert/optimal-freelancing/optimal-freelancing.js b/coding_interviews/algoexpert/optimal-freelancing/optimal-freelancing.js new file mode 100644 index 0000000..4b25f96 --- /dev/null +++ b/coding_interviews/algoexpert/optimal-freelancing/optimal-freelancing.js @@ -0,0 +1,28 @@ +function optimalFreelancing(jobs) { + let profit = 0; + let day = 7; + let completedJobs = new WeakSet(); + + while (day > 0) { + let bestJob; + + for (let job of jobs) { + if ( + job.deadline >= day && + job.payment > (bestJob?.payment || 0) && + !completedJobs.has(job) + ) { + bestJob = job; + } + } + + day--; + + if (bestJob) { + completedJobs.add(bestJob); + profit += bestJob.payment; + } + } + + return profit; +}