Restore The Array — LeetCode 1416 Python Solution

HardStringDynamic Programming
Problem
#1416
Reading time
2 min

The problem

A program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits s and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros in the array.

Example

Input
s = "1000", k = 10000
Output
1
Explanation
The only possible array is [1000]

Complexity

MeasureComplexity
TimeO(n·m) (typical)
SpaceO(n·m) or optimized auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 1416. Restore The Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1416. Restore The Array?
LeetCode 1416. Restore The Array is rated Hard on LeetCode.
What topics does LeetCode 1416. Restore The Array cover?
LeetCode 1416. Restore The Array is tagged String and Dynamic Programming 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.

Get Stealth Interview