PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#FT0000182. Số nhỏ nhất sau tối đa K lần đổi chỗ kề (Minimum Possible Integer After at Most K Adjacent Swaps on Digits)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Số nhỏ nhất sau tối đa K lần đổi chỗ kề (Minimum Possible Integer After at Most K Adjacent Swaps on Digits)
Minimum Possible Integer After at Most K Adjacent Swaps on Digits
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given the decimal digit string num of a very large nonnegative integer without leading zeroes, and an integer . One operation swaps two adjacent digits. Perform at most swaps, possibly fewer, to obtain the lexicographically smallest digit string. The result may start with zeroes and must preserve all original digits. Deletion and nonadjacent swaps are not allowed as single operations.
Input
The first line contains the digit string num; the second contains integer . This is the PHOJ standard-input adaptation of the LeetCode function interface.
Output
Print the smallest attainable digit string, retaining any leading zeroes.
Subtasks
- Subtask 1 (20 points): ; ; the input is a decimal digit string with no leading zero; output may start with zeroes.
- Subtask 2 (30 points): ; ; the input is a decimal digit string with no leading zero; output may start with zeroes.
- Subtask 3 (50 points): ; ; the input is a decimal digit string with no leading zero; output may start with zeroes.
Examples
Example 1
Input
4321
4
Output
1342
Explanation
- Choose digit
1at current index 3 (0-based); use 3 adjacent swaps, budget 4 → 1; result prefix1. - Choose digit
3at current index 1 (0-based); use 1 adjacent swaps, budget 1 → 0; result prefix13. - Choose digit
4at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix134. - Choose digit
2at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix1342.
Example 2
Input
100
1
Output
010
Explanation
- Choose digit
0at current index 1 (0-based); use 1 adjacent swaps, budget 1 → 0; result prefix0. - Choose digit
1at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix01. - Choose digit
0at current index 0 (0-based); use 0 adjacent swaps, budget 0 → 0; result prefix010.