The key is computing remaining time per task and then sorting by that ascending, then greedily picking the cheapest tasks until you run out of budget.
First, compute the additional time needed for each task by subtracting already-spent time from required time. Then, sort these additional times in ascending order and greedily allocate the budget to the tasks with the smallest additional time until the budget is exhausted, counting the tasks completed. This greedy strategy maximizes the number of tasks because selecting tasks with minimal extra time leaves more budget for others.
Pro tip: Explicitly state the greedy choice and prove its optimality by an exchange argument: any optimal solution can be transformed to include the task with the smallest additional time without reducing the total count. This demonstrates algorithmic rigor and impresses interviewers.
Clarify that each task requires a certain total time, some of which is already spent, and you can add extra time from a fixed budget to complete tasks. The goal is to maximize the number of completed tasks.
For each task, calculate the extra time required as max(0, required_time - already_spent_time). If already_spent_time >= required_time, the task is already complete and requires no extra time.
Sort the list of additional times in non-decreasing order. This allows you to prioritize tasks that require the least extra time.
Iterate through the sorted list, and for each task, if the additional time is less than or equal to the remaining budget, subtract it from the budget and increment the count. Stop when the budget is insufficient for the next task.
The count of tasks completed is the maximum number achievable with the given budget.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.