1953. Maximum Number of Weeks for Which You Can Work Medium

@problem@discussion
#Array#Greedy



1/**
2 * [1953] Maximum Number of Weeks for Which You Can Work
3 *
4 * 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.
5 * You can work on the projects following these two rules:
6 * 
7 * 	Every week, you will finish exactly one milestone of one project. You must work every week.
8 * 	You cannot work on two milestones from the same project for two consecutive weeks.
9 * 
10 * 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.
11 * Return the maximum number of weeks you would be able to work on the projects without violating the rules mentioned above.
12 *  
13 * Example 1:
14 * 
15 * Input: milestones = [1,2,3]
16 * Output: 6
17 * Explanation: One possible scenario is:
18 * ​​​​- During the 1^st week, you will work on a milestone of project 0.
19 * - During the 2^nd week, you will work on a milestone of project 2.
20 * - During the 3^rd week, you will work on a milestone of project 1.
21 * - During the 4^th week, you will work on a milestone of project 2.
22 * - During the 5^th week, you will work on a milestone of project 1.
23 * - During the 6^th week, you will work on a milestone of project 2.
24 * The total number of weeks is 6.
25 * 
26 * Example 2:
27 * 
28 * Input: milestones = [5,2,1]
29 * Output: 7
30 * Explanation: One possible scenario is:
31 * - During the 1^st week, you will work on a milestone of project 0.
32 * - During the 2^nd week, you will work on a milestone of project 1.
33 * - During the 3^rd week, you will work on a milestone of project 0.
34 * - During the 4^th week, you will work on a milestone of project 1.
35 * - During the 5^th week, you will work on a milestone of project 0.
36 * - During the 6^th week, you will work on a milestone of project 2.
37 * - During the 7^th week, you will work on a milestone of project 0.
38 * The total number of weeks is 7.
39 * Note that you cannot work on the last milestone of project 0 on 8^th week because it would violate the rules.
40 * Thus, one milestone in project 0 will remain unfinished.
41 * 
42 *  
43 * Constraints:
44 * 
45 * 	n == milestones.length
46 * 	1 <= n <= 10^5
47 * 	1 <= milestones[i] <= 10^9
48 * 
49 */
50pub struct Solution {}
51
52// problem: https://leetcode.com/problems/maximum-number-of-weeks-for-which-you-can-work/
53// discuss: https://leetcode.com/problems/maximum-number-of-weeks-for-which-you-can-work/discuss/?currentPage=1&orderBy=most_votes&query=
54
55// submission codes start here
56
57impl Solution {
58    pub fn number_of_weeks(milestones: Vec<i32>) -> i64 {
59        
60    }
61}
62
63// submission codes end
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_1953() {
71    }
72}
73


Back
© 2025 bowen.ge All Rights Reserved.