LeetCode solutions

3649. Minimum Time to Break Locks I

My accepted Python solution to LeetCode problem 3649, Minimum Time to Break Locks I, running in 114ms.

  • Difficulty: Medium
  • Python
  • Runtime 114ms
  • Memory 18.2MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 114ms, memory 18.2MB, accepted 2025-12-31.

python
class Solution:
    def findMinimumTime(self, strength: List[int], K: int) -> int:
        from functools import lru_cache
        import math
        
        n = len(strength)
        
        @lru_cache(maxsize=None)
        def dp(mask, x):
            if mask == (1 << n) - 1:
                return 0
            
            min_time = float('inf')
            for i in range(n):
                if not (mask & (1 << i)):
                    # Time to break lock i with factor x
                    time = math.ceil(strength[i] / x)
                    total = time + dp(mask | (1 << i), x + K)
                    min_time = min(min_time, total)
            return min_time
        
        return dp(0, 1)

Source