How to get the least number after deleting k digits from the input number

前端 未结 11 1204
灰色年华
灰色年华 2020-12-11 05:34

For example, if the input number is 24635, the least number is 23 after deleting any 3 digits.

It\'s not the same as taking the two smalles

11条回答
  •  自闭症患者
    2020-12-11 05:49

    I think this will do the trick:

    Let's use 532874902352. Try it with anything you want if you feel like it.

    With 532874902352, we will remove 4 digits, or any you want.

    Move 4+1 digits in. 53287|4902352

    Find the smallest number. 2

    Delete all digits before the chosen one. We have now 28749023532; we deleted 2 digits. 2 more.

    Delete 2 digits after the first one. We have 249023532. There we go.

    However, if the digit that would be deleted is the second to last one, delete the last one if it is greater than the second to last one.

    Examples:

    24635, remove 3.

    Move in 3+1.

    2463|5

    Delete all before 2, the smallest.

    24635

    Delete to fulfill the amount of digits needed, 3. But delete 5 instead of 3.

    23

    43331, remove 3.

    Move in 3+1.

    4333|1

    Delete all before 3, the smallest.

    31

    Delete to fulfill the amount of digits needed, 3. We have no more digits to delete.

    It's up to you to implement this method.

提交回复
热议问题