Partition String Into Substrings With Values at Most K

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def minimumPartition(self, s: str, k: int) -> int:
        count = 0
        i = 0
        n = len(s)

        while i < n:
            value = 0

            # Construct the value from the current index until it exceeds k
            while i < n and value * 10 + int(s[i]) <= k:
                value = value * 10 + int(s[i])
                i += 1

            # If value remains 0, then it means we were not able to form any valid substring
            if value == 0:
                return -1

            count += 1

        return count