Maximum Number of Weeks for Which You Can Work — LeetCode 1953 Python Solution
- Problem
- #1953
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- Input
- milestones = [1,2,3]
- Output
- 6
- Explanation
- One possible scenario is:
Python solution
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 sComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the number of projects |
| Space | O(1) auxiliary |
Pattern: Greedy
Take the locally best option every time — when you can prove that never costs you later. LeetCode 1953. Maximum Number of Weeks for Which You Can Work is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Greedy.
The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1953. Maximum Number of Weeks for Which You Can Work?
- LeetCode 1953. Maximum Number of Weeks for Which You Can Work is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1953. Maximum Number of Weeks for Which You Can Work?
- The Python solution on this page runs in O(n), where n is the number of projects.
- What is the space complexity of LeetCode 1953. Maximum Number of Weeks for Which You Can Work?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1953. Maximum Number of Weeks for Which You Can Work cover?
- LeetCode 1953. Maximum Number of Weeks for Which You Can Work is tagged Greedy and Array on LeetCode.