Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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 1st week, you will work on a milestone of project 0. - During the 2nd week, you will work on a milestone of project 2. - During the 3rd week, you will work on a milestone of project 1. - During the 4th week, you will work on a milestone of project 2. - During the 5th week, you will work on a milestone of project 1. - During the 6th 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 1st week, you will work on a milestone of project 0. - During the 2nd week, you will work on a milestone of project 1. - During the 3rd week, you will work on a milestone of project 0. - During the 4th week, you will work on a milestone of project 1. - During the 5th week, you will work on a milestone of project 0. - During the 6th week, you will work on a milestone of project 2. - During the 7th 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 8th week because it would violate the rules. Thus, one milestone in project 0 will remain unfinished.
Constraints:
n == milestones.length1 <= n <= 1051 <= milestones[i] <= 109Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,2,3]
[5,2,1]
task-scheduler)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1953: Maximum Number of Weeks for Which You Can Work
class Solution {
public long numberOfWeeks(int[] milestones) {
int mx = 0;
long s = 0;
for (int e : milestones) {
s += e;
mx = Math.max(mx, e);
}
long rest = s - mx;
return mx > rest + 1 ? rest * 2 + 1 : s;
}
}
// Accepted solution for LeetCode #1953: Maximum Number of Weeks for Which You Can Work
func numberOfWeeks(milestones []int) int64 {
mx := slices.Max(milestones)
s := 0
for _, x := range milestones {
s += x
}
rest := s - mx
if mx > rest+1 {
return int64(rest*2 + 1)
}
return int64(s)
}
# Accepted solution for LeetCode #1953: Maximum Number of Weeks for Which You Can Work
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
// Accepted solution for LeetCode #1953: 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 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.length
* 1 <= n <= 10^5
* 1 <= milestones[i] <= 10^9
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-number-of-weeks-for-which-you-can-work/
// discuss: https://leetcode.com/problems/maximum-number-of-weeks-for-which-you-can-work/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn number_of_weeks(milestones: Vec<i32>) -> i64 {
let max: i64 = milestones.iter().map(|c| *c as i64).max().unwrap();
let sum: i64 = milestones.iter().map(|c| *c as i64).sum();
let sum_of_rest: i64 = sum - max;
if max - sum_of_rest > 1 {
sum_of_rest * 2 + 1
} else {
sum_of_rest + max
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1953_example_1() {
let milestones = vec![1, 2, 3];
let result = 6;
assert_eq!(Solution::number_of_weeks(milestones), result);
}
#[test]
fn test_1953_example_2() {
let milestones = vec![5, 2, 1];
let result = 7;
assert_eq!(Solution::number_of_weeks(milestones), result);
}
}
// Accepted solution for LeetCode #1953: Maximum Number of Weeks for Which You Can Work
function numberOfWeeks(milestones: number[]): number {
const mx = Math.max(...milestones);
const s = milestones.reduce((a, b) => a + b, 0);
const rest = s - mx;
return mx > rest + 1 ? rest * 2 + 1 : s;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.