Loading...
Loading...
In a college quiz round, each team gets a long number written on the board. The team must cross out exactly k digits. The digits that are left, kept in the same order, form the team's number, and the smallest number wins.
Print the smallest number the team can get. Leading zeros do not count, so 0200 is printed as 200. If no digits are left, or only zeros are left, print 0.
0.Example 1
Input
1432219 3
Output
1219
Crossing out 4, 3 and one of the two 2s leaves 1219, the smallest choice.
Example 2
Input
10200 1
Output
200
Crossing out the 1 leaves 0200, which is printed as 200.
Try the problem first. Open one hint at a time.
A smaller digit near the front matters more than anything after it.
Read the digits from left to right and keep the digits you plan to keep on a stack.
While you still may cross out digits and the top of the stack is larger than the new digit, pop it. Then push the new digit.
If some crossings are left at the end, remove digits from the top. Finally, skip leading zeros, and print 0 if nothing is left.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor