Magicsheet logo

Maximum Number of Weeks for Which You Can Work

Medium
62.7%
Updated 6/1/2025

Asked by 3 Companies

Maximum Number of Weeks for Which You Can Work

What is this problem about?

The Maximum Number of Weeks for Which You Can Work coding problem gives you an array milestones, where milestones[i] represents the number of weeks required to complete the i-th project. You can work on any project at any time, but you cannot work on the same project for two consecutive weeks. You want to find the maximum number of weeks you can work across all projects.

Why is this asked in interviews?

Wells Fargo, SAP, and Natwest use this problem to test greedy algorithms, especially those involving sorting and critical element identification. It's a game-theory type problem where maximizing the total work weeks often comes down to strategically using the project with the most remaining work.

Algorithmic pattern used

Greedy Approach with Maximizing Critical Resource: The constraint "cannot work on the same project for two consecutive weeks" is key. This means if you work on project A this week, you must work on a different project next week. If project A has an extremely high number of weeks, it becomes a "bottleneck" or "critical project".

The greedy strategy:

  1. Find the Maximum Milestone: Identify the project with the maximum number of weeks, let's say max_milestone_weeks.
  2. Calculate Sum of Other Milestones: Sum the weeks of all other projects, let's say sum_other_weeks.
  3. Compare: a. If max_milestone_weeks <= sum_other_weeks + 1: You can always interleave the max_milestone_weeks project with weeks from other projects. The +1 allows for the last week of max_milestone_weeks to be worked on without needing an "other" project to separate it from itself. In this scenario, you can work for total_sum_of_all_milestones weeks. b. If max_milestone_weeks > sum_other_weeks + 1: The max_milestone_weeks project is too dominant. You will exhaust all other projects, and then you'll be forced to work on max_milestone_weeks twice in a row, which is forbidden. In this case, you can work sum_other_weeks weeks from other projects, and sum_other_weeks + 1 weeks from the max_milestone_weeks project (interleaving sum_other_weeks of its weeks with sum_other_weeks from other projects, and then one more week from it). So, the total work weeks will be 2 * sum_other_weeks + 1.

Total sum = max_milestone_weeks + sum_other_weeks. So, if max_milestone_weeks > sum_other_weeks + 1, the answer is 2 * sum_other_weeks + 1. Otherwise, the answer is total_sum_of_all_milestones.

Example explanation

milestones = [1, 2, 3].

  1. max_milestone_weeks = 3 (from project with 3 weeks).
  2. sum_other_weeks = 1 + 2 = 3.
  3. Compare: max_milestone_weeks (3) vs sum_other_weeks + 1 (3 + 1 = 4). Since 3 <= 4, it falls into case (a). Total work weeks = total_sum_of_all_milestones = 1 + 2 + 3 = 6. (Example interleaving: P3, P1, P3, P2, P3, P? (P2 is done, P1 is done) - Wait, P3, P1, P3, P2, P3, P2. That's 6 weeks. P3 (1), P1(1), P3(2), P2(1), P3(3), P2(2). All done. )

milestones = [1, 100].

  1. max_milestone_weeks = 100.
  2. sum_other_weeks = 1.
  3. Compare: max_milestone_weeks (100) vs sum_other_weeks + 1 (1 + 1 = 2). Since 100 > 2, it falls into case (b). Total work weeks = 2 * sum_other_weeks + 1 = 2 * 1 + 1 = 3. (Example interleaving: P100, P1, P100. P1 is done. P100 needs 98 more. But can't work P100 again. So, total 3 weeks.)

Common mistakes candidates make

  • Trying complex DP: The problem looks like it might require complex scheduling or DP, but the simple greedy comparison works.
  • Off-by-one in the comparison: The +1 in sum_other_weeks + 1 is critical for the boundary condition where the max project can perfectly interleave.
  • Miscalculating the "dominant" case: Understanding why 2 * sum_other_weeks + 1 is the limit when

Similar Questions