2511. Partition String Into Substrings With Values at Most K
My accepted Python solution to LeetCode problem 2511, Partition String Into Substrings With Values at Most K, running in 51ms.
- Difficulty: Medium
- Python
- Runtime 51ms
- Memory 18.1MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 51ms, memory 18.1MB, accepted 2025-12-31.
class Solution:
def minimumPartition(self, s: str, k: int) -> int:
count = 0
current = 0
for digit in s:
d = int(digit)
if d > k:
return -1
new_val = current * 10 + d
if new_val <= k:
current = new_val
else:
count += 1
current = d
return count + 1