300. Longest Increasing subsequence Leetcode Solution
Longest Increasing subsequence Leetcode Problem :
Given an integer array nums, return the length of the longest strictly increasing
Longest Increasing subsequence Leetcode Solution :
Constraints :
- 1 <= nums.length <= 2500
- -104 <= nums[i] <= 104
Example 1:
- Input: nums = [10,9,2,5,3,7,101,18]
- Output: 4
- Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Approach :
The main idea of this approach will be to find the longest common subsequence(LCS) between the given array and the sorted form of the array. However by careful observations (and a number of wrong submissions), it appears that duplicate elements play and important role too. We need to include duplicate elements once.
But, if we try to remove duplicasy in the given array, nums[], the order of the elements will be disturbed and we might get different results because the subsequence changes.
Therefore, we remove duplicasy in the sorted array. Finally, find the LCS between the given array and the sorted array(after duplicate elements are included only once).
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
class Solution { public int lengthOfLIS(int[] nums) { //longest increasing subsequence = longest common subsequence (given array and sorted array-the sorted array will have all the duplicates written only once) int n = nums.length; int[]sort = new int[n]; for (int i=0; i < n; i++) { sort[i] = nums[i]; } Arrays.sort(sort); ArrayList< Integer > list = new ArrayList< Integer>(); for (int i=0; i< n; i++) { if (!list.contains(sort[i])) { list.add(sort[i]); } } int size = list.size(); int[]sorty = new int[size]; for (int i=0; i< size; i++) { sorty[i] = list.get(i); } return lcs (nums, sorty, n, size); } public int lcs (int[] nums1, int[] nums2, int n, int m) { int[][]matrix = new int[n+1][m+1]; for (int i=0; i< n+1; i++) { for (int j=0; j< m+1; j++) { if (i == 0 || j == 0) { matrix[i][j] = 0; } else if (nums1[i-1] == nums2[j-1]) { matrix[i][j] = 1 + matrix[i-1][j-1]; } else if (nums1[i-1] != nums2[j-1]) { matrix[i][j] = Math.max (matrix[i-1][j] , matrix[i][j-1]); } } } return matrix[n][m]; } }
public int lengthOfLIS(int[] nums) { int[] tails = new int[nums.length]; int size = 0; for (int x : nums) { int i = 0, j = size; while (i != j) { int m = (i + j) / 2; if (tails[m] < x) i = m + 1; else j = m; } tails[i] = x; if (i == size) ++size; } return size; } // Runtime: 2 ms
def lengthOfLIS(self, nums): tails = [0] * len(nums) size = 0 for x in nums: i, j = 0, size while i != j: m = (i + j) / 2 if tails[m] < x: i = m + 1 else: j = m tails[i] = x size = max(i + 1, size) return size
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment