MediumNo. 1953

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

Pattern
Greedy
Topics
Greedy, Array
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

PythonSolution
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

Time
O(n)where n is the number of projects
Space · auxiliary
O(1)

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

Related problems
No.ProblemDifficulty
1946Largest Number After Mutating SubstringMedium
1962Remove Stones to Minimize the TotalMedium
0011Container With Most WaterMedium
0134Gas StationMedium
0135CandyHard
0179Largest NumberMedium

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.

Free to download. Check it runs on your machine, then pick a plan.