Maximum Number of Weeks for Which You Can Work Problem
Description
LeetCode Problem 1953.
There are n projects numbered from 0 to n - 1. You are given an integer array milestones where each milestones[i] denotes the number of milestones the i^th project has.
You can work on the projects following these two rules:
- Every week, you will finish exactly one milestone of one project. You must work every week.
- You cannot work on two milestones from the same project for two consecutive weeks.
Once all the milestones of all the projects are finished, or if the only milestones that you can work on will cause you to violate the above rules, you will stop working. Note that you may not be able to finish every project's milestones due to these constraints.
Return the maximum number of weeks you would be able to work on the projects without violating the rules mentioned above.
Example 1:
Input: milestones = [1,2,3]
Output: 6
Explanation: One possible scenario is:
- During the 1^st week, you will work on a milestone of project 0.
- During the 2^nd week, you will work on a milestone of project 2.
- During the 3^rd week, you will work on a milestone of project 1.
- During the 4^th week, you will work on a milestone of project 2.
- During the 5^th week, you will work on a milestone of project 1.
- During the 6^th week, you will work on a milestone of project 2.
The total number of weeks is 6.
Example 2:
Input: milestones = [5,2,1]
Output: 7
Explanation: One possible scenario is:
- During the 1^st week, you will work on a milestone of project 0.
- During the 2^nd week, you will work on a milestone of project 1.
- During the 3^rd week, you will work on a milestone of project 0.
- During the 4^th week, you will work on a milestone of project 1.
- During the 5^th week, you will work on a milestone of project 0.
- During the 6^th week, you will work on a milestone of project 2.
- During the 7^th week, you will work on a milestone of project 0.
The total number of weeks is 7.
Note that you cannot work on the last milestone of project 0 on 8^th week because it would violate the rules.
Thus, one milestone in project 0 will remain unfinished.
Constraints:
n == milestones.length1 <= n <= 10^51 <= milestones[i] <= 10^9
Solution
Approach: Greedy
We consider under what circumstances we cannot complete all stage tasks. If there is a project i whose number of stage tasks is greater than the sum of the number of stage tasks of all other projects plus 1, then we cannot complete all stage tasks. Otherwise, we can definitely complete all stage tasks by interlacing between different projects.
We denote the sum of the number of stage tasks of all projects as s, and the maximum number of stage tasks as mx, then the sum of the number of stage tasks of all other projects is rest = s - mx.
If mx > rest + 1, then we cannot complete all stage tasks, and at most we can complete rest × 2 + 1 stage tasks. Otherwise, we can complete all stage tasks, the number is s.
Complexity: O(n) time and O(1) space, where n is the number of projects.
class Solution:
def numberOfWeeks(self, milestones: List[int]) -> int:
mx, s = max(milestones), sum(milestones)
rest = s - mx
return rest * 2 + 1 if mx > rest + 1 else s