LeetCode – Longest Increasing Subsequence (Java)

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example, given [10, 9, 2, 5, 3, 7, 101, 18], the longest increasing subsequence is [2, 3, 7, 101]. Therefore the length is 4.

Java Solution 1 - Naive

Let max[i] represent the length of the longest increasing subsequence so far.
If any element before i is smaller than nums[i], then max[i] = max(max[i], max[j]+1).

Here is an example:

longest-increasing-subsequence-leetcode-java

public int lengthOfLIS(int[] nums) {
    if(nums==null || nums.length==0)
        return 0;
 
    int[] max = new int[nums.length];
 
    for(int i=0; i<nums.length; i++){
        max[i]=1;
        for(int j=0; j<i;j++){
            if(nums[i]>nums[j]){
                max[i]=Math.max(max[i], max[j]+1);
            }
        }
    }
 
    int result = 0;
    for(int i=0; i<max.length; i++){
        if(max[i]>result)
            result = max[i];
    }
    return result;
}

Or, simplify the code as:

public int lengthOfLIS(int[] nums) {
    if(nums==null || nums.length==0)
        return 0;
 
    int[] max = new int[nums.length];
    Arrays.fill(max, 1);
 
    int result = 1;
    for(int i=0; i<nums.length; i++){
        for(int j=0; j<i; j++){
            if(nums[i]>nums[j]){
                max[i]= Math.max(max[i], max[j]+1);
 
            }
        }
        result = Math.max(max[i], result);
    }
 
   return result;
}

Java Solution 2 - Binary Search

We can put the increasing sequence in a list.

for each num in nums
     if(list.size()==0)
          add num to list
     else if(num > last element in list)
          add num to list
     else 
          replace the element in the list which is the smallest but bigger than num

longest-increasing-subsequence-leetcode-java

public int lengthOfLIS(int[] nums) {
    if(nums==null || nums.length==0)
        return 0;
 
    ArrayList<Integer> list = new ArrayList<Integer>(); 
 
    for(int num: nums){
        if(list.size()==0 || num>list.get(list.size()-1)){
            list.add(num);
        }else{
            int i=0; 
            int j=list.size()-1;
 
            while(i<j){
                int mid = (i+j)/2;
                if(list.get(mid) < num){
                    i=mid+1;
                }else{
                    j=mid;
                }
            }
 
            list.set(j, num);
        }
    }
 
    return list.size();
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Darewreck

    It’s asking for the longest sequence count, Not the actual longest sequence. The longest sequence is 4. If they where asking to print out the longest sequence, you would be correct. In that case, you would also handle the case when there two sequence with the same length.

  • TUSHAR SHARAD MHASKAR

    The binary search program fails for input 1 4 9 10 2 3
    Output produced is 1 2 3 10 which id definitely not longest increasing.