We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Maximum Number of Weeks for Which You Can Work

Number: 2084

Difficulty: Medium

Paid? No

Companies: SAP, Wells Fargo


Problem Description

Given an array milestones where each element represents the number of milestones for a project, you work exactly one milestone per week while never working on the same project in consecutive weeks. You need to maximize the number of weeks you can work by possibly leaving some milestones unfinished if necessary.


Key Insights

  • The total work possible is essentially the sum of all milestones.
  • The constraint of not working on the same project consecutively forces you to interleave work from different projects.
  • If one project has too many milestones compared to the rest (i.e. if max > sum of the others + 1), then you will be limited by the opportunities to interleave with other projects.
  • Specifically, if max_milestones > remaining_sum + 1, the answer is 2 * remaining_sum + 1, otherwise, you can complete all milestones.

Space and Time Complexity

Time Complexity: O(n) – scanning through the milestones array to compute the sum and maximum. Space Complexity: O(1) – only constant extra space is used.


Solution

The solution involves calculating two key values: the total number of milestones (total_work) and the maximum milestones from a single project (max_work). Then, subtract the maximum from the total to get the sum of milestones from the other projects (other_work).

If max_work is greater than other_work + 1, it implies that after scheduling all other milestones, there will still be surplus milestones in the max project that cannot be interleaved without breaking the consecutive rule. Hence, the answer in this case is 2 * other_work + 1. Otherwise, you can work for all weeks where total weeks equal the sum of milestones.

Data structures used include:

  • Basic arithmetic operations.
  • A linear scan across the milestones list for summing and finding the maximum.

This approach is greedy as it leverages the fact that interleaving work optimally is subject to the distribution of milestones, which directly informs whether the maximum can be interleaved fully with the remainder.


Code Solutions

# Python solution for Maximum Number of Weeks for Which You Can Work

def numberOfWeeks(milestones):
    # Calculate the total sum of milestones
    total_milestones = sum(milestones)
    # Find the maximum milestones among all projects
    max_milestones = max(milestones)
    # Calculate the sum of milestones from other projects
    other_milestones = total_milestones - max_milestones
    # If the largest project has more milestones than can be alternated with the others
    if max_milestones > other_milestones + 1:
        # Maximum weeks is twice the work on other projects plus one extra for an initial week from the largest project
        return 2 * other_milestones + 1
    else:
        # Otherwise, you can finish all milestones
        return total_milestones

# Example usage:
print(numberOfWeeks([1, 2, 3]))  # Output: 6
print(numberOfWeeks([5, 2, 1]))  # Output: 7
← Back to All Questions