PROBLEM LINK:Author: ravikiran0606 DIFFICULTY:EASY PREREQUISITES:DP,Longest Increasing Subsequence. PROBLEM: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. EXPLANATION: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[i] as dp[a[i]]=dp[a[i]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:Author's solution can be found here
This question is marked "community wiki".
asked 13 Mar, 15:58
