Given an array of n integers arr, return the Longest Increasing Subsequence (LIS) that is Index-wise Lexicographically Smallest.
The Longest Increasing Subsequence (LIS) is the longest subsequence where all elements are in strictly increasing order.
A subsequence A1 is Index-wise Lexicographically Smaller than another subsequence A2 if, at the first position where A1 and A2 differ, the element in A1 appears earlier in the array arr than corresponding element in S2.
Your task is to return the LIS that is Index-wise Lexicographically Smallest from the given array.
Input: arr = [10, 22, 9, 33, 21, 50, 41, 60, 80]
Output: [10, 22, 33, 50, 60, 80]
Explanation: The LIS is [10, 22, 33, 50, 60, 80] and it is the lexicographically smallest.
Input: arr = [1, 3, 2, 4, 6, 5]
Output: [1, 3, 4, 6]
Explanation: Possible LIS sequences are [1, 3, 4, 6] and [1, 2, 4, 6]. Since [1, 3, 4, 6] is Index-wise Lexicographically Smaller, it is the result.
Input: arr = [5, 6, 1, 2, 3, 4, 7]
Striver and the team have worked tirelessly over the past few days to bring this to you. Please bear with us a little more as we finalize everything.
Q: Why does sub store potential LIS elements instead of the actual LIS sequence?
A: The array sub doesn’t store the actual LIS but ensures its length is correct.
Q: Why do we track parent[i]?
A: It helps reconstruct the lexicographically smallest sequence after finding LIS.
Q: What if arr contained duplicate elements?
A: Use non-strict increasing subsequence (<= instead of <).