DP,Longest Increasing Subsequence.
Given an array of N integers, we need to find the length of the longest increasing subsequence such that difference between two consecutive elements in the sequence is exactly equal to 1.
In this question, we can find the length of longest increasing subsequence by maintaining an array dp[maxi]. Let dp[k] indicate the length of the longest increasing subsequence ending with integer k. Thus by iterating through the array, we can calculate the length of increasing subsequence upto the index i ending with a* as dp[a*]=dp[a*-1]+1. And finding the maximum value of the dp array gives us the length of the longest increasing subsequence with the given constraint.
Based on the implementation, If you use an hash table like an array, the time complexity will be O(n). If you use map, the time complexity will be O(n logn). Both solutions will pass.
Author’s solution can be found here