Advertisement
amitsen01ei

LISLength.java

Nov 13th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | Source Code | 0 0
  1. public class LISLength {
  2.     /**
  3.      * Finds the length of the longest strictly increasing sub-sequence in an array.
  4.      *
  5.      * Time Complexity: O(n^2), where n is the length of the input array.
  6.      *
  7.      * Space Complexity: O(n), where n is the length of the input array.
  8.      *
  9.      *
  10.      * @param nums The input array of integers.
  11.      * @return The length of the longest strictly increasing subsequence.
  12.      */
  13.     private static int findLength(int[] nums) {
  14.         if (nums == null || nums.length == 0) {
  15.             return 0;
  16.         }
  17.  
  18.         var dp = new int[nums.length];
  19.         var maxLen = 1;
  20.         for (var i = 0; i < nums.length; i++) {
  21.             dp[i] = 1;
  22.             for (var j = 0; j < i; j++) {
  23.                 if (nums[i] > nums[j] && dp[i] < dp[j] + 1) {
  24.                     dp[i] = dp[j] + 1;
  25.                 }
  26.             }
  27.             maxLen = Math.max(maxLen, dp[i]);
  28.         }
  29.  
  30.         return maxLen;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement