Longest Increasing Subsequence Solutions in Java
Number 300
Difficulty Medium
Acceptance 42.6%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Memory Search/// Time Complexity: O(n^2)/// Space Complexity: O(n)public class Solution1 {// memo[i] is the length of the longest increasing sequence end in nums[i]private int[] memo;public int lengthOfLIS(int[] nums) {if(nums.length == 0)return 0;memo = new int[nums.length];Arrays.fill(memo, -1);int res = 1;for(int i = 0 ; i < nums.length ; i ++)res = Math.max(res, getMaxLength(nums, i));return res;}private int getMaxLength(int[] nums, int index){if(memo[index] != -1)return memo[index];int res = 1;for(int i = 0 ; i <= index-1 ; i ++)if(nums[index] > nums[i])res = Math.max(res, 1 + getMaxLength(nums, i));return memo[index] = res;}public static void main(String[] args) {int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};System.out.println((new Solution1()).lengthOfLIS(nums1));// 4// ---int nums2[] = {4, 10, 4, 3, 8, 9};System.out.println((new Solution1()).lengthOfLIS(nums2));// 3// ---int nums3[] = {2, 2};System.out.println((new Solution1()).lengthOfLIS(nums3));// 1// ---int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};System.out.println((new Solution1()).lengthOfLIS(nums4));// 6}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Dynamic Programming/// Time Complexity: O(n^2)/// Space Complexity: O(n)public class Solution2 {public int lengthOfLIS(int[] nums) {if(nums.length == 0)return 0;// memo[i] is the length of the longest increasing sequence end in nums[i]int memo[] = new int[nums.length];Arrays.fill(memo, 1);for(int i = 1 ; i < nums.length ; i ++)for(int j = 0 ; j < i ; j ++)if(nums[i] > nums[j])memo[i] = Math.max(memo[i], 1 + memo[j]);int res = memo[0];for(int i = 1 ; i < nums.length ; i ++)res = Math.max(res, memo[i]);return res;}public static void main(String[] args) {int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};System.out.println((new Solution2()).lengthOfLIS(nums1));// 4// ---int nums2[] = {4, 10, 4, 3, 8, 9};System.out.println((new Solution2()).lengthOfLIS(nums2));// 3// ---int nums3[] = {2, 2};System.out.println((new Solution2()).lengthOfLIS(nums3));// 1// ---int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};System.out.println((new Solution2()).lengthOfLIS(nums4));// 6}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/longest-increasing-subsequence/description//// Author : liuyubobobo/// Time : 2017-11-19import java.util.Arrays;/// Dynamic Programming/// Best Solution for LIS Problem////// Time Complexity: O(nlogn)/// Space Complexity: O(n)public class Solution3 {public int lengthOfLIS(int[] nums) {if(nums.length == 0)return 0;// dp[i] is the last num for length i increasing sequenceint dp[] = new int[nums.length + 1];Arrays.fill(dp, Integer.MIN_VALUE);int len = 1;dp[1] = nums[0];for(int i = 1 ; i < nums.length ; i ++)if(nums[i] > dp[len]){len ++;dp[len] = nums[i];}else{int index = lowerBound(dp, 0, len, nums[i]);if(dp[index] != nums[i])dp[index] = Math.min(dp[index], nums[i]);}return len;}private int lowerBound(int[] arr, int l, int r, int target){int left = l, right = r + 1;while(left != right){int mid = left + (right - left) / 2;if(arr[mid] >= target)right = mid;else // arr[mid] < targetleft = mid + 1;}return left;}public static void main(String[] args) {int nums1[] = {10, 9, 2, 5, 3, 7, 101, 18};System.out.println((new Solution3()).lengthOfLIS(nums1));// 4// ---int nums2[] = {4, 10, 4, 3, 8, 9};System.out.println((new Solution3()).lengthOfLIS(nums2));// 3// ---int nums3[] = {2, 2};System.out.println((new Solution3()).lengthOfLIS(nums3));// 1// ---int nums4[] = {1, 3, 6, 7, 9, 4, 10, 5, 6};System.out.println((new Solution3()).lengthOfLIS(nums4));// 6}}