Google VO Interview Question: Maximum Number from a Subsequence of K Digits

22 Views
No Comments

Given a sequence S of N digits, find a subsequence of K digits such that the number formed by these K digits, in order, is the largest.

The task is to select K digits from a sequence of N digits, preserving order, so that the resulting number is as large as possible. The standard solution is greedy: maintain a monotonic decreasing stack and remove smaller previous digits whenever a larger digit appears, while staying within the allowed number of deletions, N-K. This ensures the highest-order digits are as large as possible, which is the key to maximizing the final number.

END
 0