Remove Digits To Have Minimum Number - Coding in a Smart Way
Written by truongkhanh on Discussion Actually, this problem asks us to decide which digit to keep to have minimum number. Therefore, we will check from left to right and determine the minimum digit we can keep at each time. In the above example, we need to keep 2 digits. The first digit we want to keep is 2 because it is minimum. Then, for 6, 7, and 4, we decide to keep 4. Thus we have 24. However, there are cases where we can not just select the minimum digit. For example, N = 32514, K = 2. We need to keep 3 digits. The most-left digit can not be 1 because we do no have enough digit to keep. So our finding is that each time, we select a minimum digit such that there still have enough digits to keep. In the example where N = 32514 and K = 2, we need to keep 3 digits. First, we find the minimum of [3, 2, 5] because we need to have at least 2 digits on the right. It is 2. Then, we will find the minimum of [5, 1] because we need to find after the digit 2 and stop at 1 to keep 1 digit.Read full article from Remove Digits To Have Minimum Number - Coding in a Smart Way
No comments:
Post a Comment