LeetCode solutions

2657. Minimum Time to Complete All Tasks

My accepted Python solution to LeetCode problem 2657, Minimum Time to Complete All Tasks, running in 589ms.

  • Difficulty: Hard
  • Python
  • Runtime 589ms
  • Memory 18.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 589ms, memory 18.4MB, accepted 2025-12-31.

python
class Solution:
    def findMinimumTime(self, tasks: List[List[int]]) -> int:
        # Sort tasks by end time
        tasks.sort(key=lambda x: x[1])
        
        # Track which time slots are used (up to 2000)
        max_time = max(task[1] for task in tasks)
        used = [False] * (max_time + 1)
        
        for start, end, duration in tasks:
            # Count already used slots in this range
            already_used = sum(1 for t in range(start, end + 1) if used[t])
            
            # Need to use more slots
            remaining = duration - already_used
            
            # Greedily pick latest available slots (to maximize overlap with future tasks)
            for t in range(end, start - 1, -1):
                if remaining <= 0:
                    break
                if not used[t]:
                    used[t] = True
                    remaining -= 1
        
        return sum(used)

Source