This approach uses dynamic programming to solve the problem in O(n^2) time complexity. We maintain a dp array where dp[i] represents the length of the longest increasing subsequence that ends with nums[i]. For each element, we iterate over all previous elements to see if they can be included in the subsequence ending at the current element, and update dp[i] accordingly.
Time Complexity: O(n^2) where n is the length of the input array. Space Complexity: O(n) for storing the dp array.
1import java.util.*;
2
3public class LongestIncreasingSubsequence {
4 public int lengthOfLIS(int[] nums) {
5 if (nums == null || nums.length == 0)
6 return 0;
7 int n = nums.length;
8 int[] dp = new int[n];
9 Arrays.fill(dp, 1);
10 int maxLen = 1;
11 for (int i = 1; i < n; i++) {
12 for (int j = 0; j < i; j++) {
13 if (nums[i] > nums[j]) {
14 dp[i] = Math.max(dp[i], dp[j] + 1);
15 }
16 }
17 maxLen = Math.max(maxLen, dp[i]);
18 }
19 return maxLen;
20 }
21 public static void main(String[] args) {
22 LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
23 int[] nums = {10,9,2,5,3,7,101,18};
24 System.out.println(lis.lengthOfLIS(nums));
25 }
26}
The Java solution follows the same logic as C and C++ solutions, using a dp array initialized to 1. It iterates over pairs of elements, updating the array to store the longest subsequence lengths found.
In this approach, we use a combination of dynamic programming and binary search to solve the problem in O(n log n) time. This is often called the 'patience sorting' technique where we maintain a list, 'ends', to store the smallest ending value of any increasing subsequence with length i+1 in 'ends[i]'. We use binary search to find the position where each element in nums can be placed in 'ends'.
Time Complexity: O(n log n). Space Complexity: O(n).
1using System;
2using System.Collections.Generic;
3
4public class Solution {
5 public int LengthOfLIS(int[] nums) {
6 List<int> ends = new List<int>();
7 foreach (var num in nums) {
8 int idx = ends.BinarySearch(num);
9 if (idx < 0) idx = ~idx;
10 if (idx < ends.Count) {
11 ends[idx] = num;
12 } else {
13 ends.Add(num);
14 }
15 }
16 return ends.Count;
17 }
18 public static void Main() {
19 int[] nums = {10,9,2,5,3,7,101,18};
20 Solution sol = new Solution();
21 Console.WriteLine(sol.LengthOfLIS(nums));
22 }
23}
The C# solution uses List's BinarySearch method to find the insertion point of each element efficiently, ensuring we maintain optimal subsequence structures.