Hello everyone, I was trying to solve this problem, 1765N - Number Reduction. I wrote a code and its logic is same as the editorial but rather than finding from digit '0' to '9' which is occurs first in the next 'k+1' elements in the number from left to right, I just searched the minimum among the 'k+1'.
My code works on almost all testcases except when the input is increasing, like n=1337 and k=2. Answer should be 13 but mine gives garbage value.
Here is my code,
I saw authors code solutions using stack to keep the smallest elements. Can someone provide me some corrections in my code, or will i need to implement stack solution?
Edit
Solved the edge case by removing elements from end when k!=0 after traversing through the number. But got TLE as the increasing sequence will always check for minimum from the next 'k+1' elements of each index, resulting in worst case O(n^2) solution. Maintaining stack is the most optimal way to keep the smallest numbers.
Auto comment: topic has been updated by prakharmishra (previous revision, new revision, compare).