Maximum Number of Weeks for Which You Can Work — LeetCode 1953 Python Solution

MediumGreedyArray
Problem
#1953
Pattern
Greedy
Reading time
2 min

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

Python
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

Complexity

MeasureComplexity
TimeO(n), where n is the number of projects
SpaceO(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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview