![]() |
VOOZH | about |
Given a string S of length n-1, consisting of characters '<' and '>' only. The ith character is the comparison result between the ith element and the (i+1)th element of the sequence. If the ith character of the string is '<', then the ith element of the sequence is less than the (i+1)st element. If the ith character of the string is '>', then the ith element of the sequence is greater than the (i+1)st element. The task is determine two sequences of size n, consisting of integers between 1 and n, such that longest increasing subsequence (LIS) of first sequence is minimum possible and LIS of second sequence is maximum possible.
Example:
Input: n = 3, S = <<
Output:
1 2 3
1 2 3Input: n = 7, S = >><>><
Output:
7 6 4 5 3 1 2
3 2 1 6 5 4 7
Approach:
The idea is to use greedy algorithms. The intuition behind the solution is to create two sequences based on the comparison results in the input string.
For the sequence with the minimum Longest Increasing Subsequence (LIS), the idea is to assign the largest numbers first to the elements that are greater than their next element. This ensures that the LIS is as small as possible because the larger numbers are placed first, making it harder to find an increasing subsequence.
For the sequence with the maximum LIS, the approach is to assign the smallest numbers first to the elements that are less than their next element. This ensures that the LIS is as large as possible because the smaller numbers are placed first, making it easier to find an increasing subsequence.
In both cases, the scans the string from left to right and assigns numbers in decreasing or increasing order based on the comparison results. This approach will ensures that the sequences are constructed in a way that minimizes and maximizes the LIS, respectively. The sequences are then printed out.
Steps:
Below is the implementation of the above approach:
1 2 3 1 2 3
Time Complexity: O(n)
Auxiliary Space: O(n)