Smallest Number After Removing Digits
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
A price tag shows the number num. Remove exactly k digits from it without changing the order of the remaining digits,
and return the smallest possible number as a string.
- Strip leading zeros from the result (
"0200"becomes"200"). - If nothing remains, or only zeros remain, return
"0".
Examples
| num | k | result |
|---|---|---|
"1432219" | 3 | "1219" |
"10200" | 1 | "200" |
"10" | 2 | "0" |
Example 1 — Remove 4, 3 and the second 2 to get "1219".
Example 2 — Removing the leading 1 leaves "0200", which is "200".
Example 3 — Every digit is removed, so the answer is "0".
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(num: string, k: int) → string
Constraints
-
num.length numconsists of digits only and has no leading zero unless it is"0".-
knum.length
Expected complexity
- Time
- O(N)
- Space
- O(N)
Examples
| num | k | Expected |
|---|---|---|
"1432219" | 3 | "1219" |
"10200" | 1 | "200" |
"10" | 2 | "0" |
Hint
Hint
A smaller leading digit always wins, so delete any digit that is larger than the digit right after it. Re-scanning from the start
for the first such digit k times is and times out. Push digits onto a stack instead, popping larger digits while
deletions remain; if deletions are left at the end, drop them from the top. Then strip leading zeros (empty → "0"). .
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 13 | verdict only |
| performance | 60 | 4 | verdict only |