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; +}