[LeetCode] Maximum Number of Weeks for Which You Can Work

1953. Maximum Number of Weeks for Which You Can Work

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 ith 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.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long numberOfWeeks(vector<int>& nums) {
long long s(0);
int m(0);
for(int& num : nums) {
s += num;
m = max(m, num);
}
return min(s, ((s - m)<<1) + 1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/11/PS/LeetCode/maximum-number-of-weeks-for-which-you-can-work/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.