ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Longest Increasing Subsequence (Dynamic Programming)

Crystal

Goal -- WPM

Ready
Exercise Algorithm Area
1require "array"
2
3def length_of_lis(nums : Array(Int32)) -> Int32
4# Handle edge cases: empty or single-element array
5if nums.empty?
6return 0
7end
8if nums.size == 1
9return 1
10end
11
12# dp[i] will store the length of the longest increasing subsequence ending at index i
13dp = Array(Int32).new(nums.size, 1)
14
15# Iterate through the array starting from the second element
16(1...nums.size).each do |i|
17# For each element, iterate through all previous elements
18(0...i).each do |j|
19# If the current element is greater than the previous element
20# and extending the subsequence ending at j would be longer than the current dp[i]
21if nums[i] > nums[j] && dp[i] < dp[j] + 1
22dp[i] = dp[j] + 1
23end
24end
25end
26
27# The result is the maximum value in the dp array
28return dp.max
29end
30
31# Helper function to demonstrate usage
32def demonstrate_lis
33puts "--- Longest Increasing Subsequence Examples ---"
34
35test_arrays = [
36[10, 9, 2, 5, 3, 7, 101, 18],
37[0, 1, 0, 3, 2, 3],
38[7, 7, 7, 7, 7, 7, 7],
39[],
40[1],
41[1, 3, 6, 7, 9, 4, 10, 5, 6]
42]
43
44test_arrays.each do |arr|
45result = length_of_lis(arr)
46puts "Array: #{arr} -> LIS Length: #{result}"
47end
48puts "---------------------------------------------"
49end
50
51demonstrate_lis
Algorithm description viewbox

Longest Increasing Subsequence (Dynamic Programming)

Algorithm description:

This algorithm calculates the length of the longest increasing subsequence (LIS) within a given array of integers. It employs dynamic programming, where `dp[i]` represents the length of the LIS ending at index `i`. The algorithm iterates through the array, comparing each element with preceding elements to find potential extensions of increasing subsequences. This is a fundamental problem in computer science with applications in areas like bioinformatics (sequence alignment) and financial analysis (trend identification).

Algorithm explanation:

The `length_of_lis` function uses dynamic programming with a time complexity of O(N^2), where N is the number of elements in the input array. This is due to the nested loops iterating through all pairs of elements. The space complexity is O(N) for storing the `dp` array. The base cases for empty or single-element arrays are handled correctly. The core logic relies on the invariant that `dp[i]` stores the maximum length of an increasing subsequence ending at index `i`. For each `i`, it checks all `j < i`; if `nums[i] > nums[j]`, it means `nums[i]` can extend the LIS ending at `j`, so we update `dp[i]` to be the maximum of its current value and `dp[j] + 1`. The final answer is the maximum value found in the `dp` array, representing the overall longest increasing subsequence.

Pseudocode:

FUNCTION length_of_lis(array nums):
  IF nums is empty THEN
    RETURN 0
  END IF
  IF nums has 1 element THEN
    RETURN 1
  END IF

  CREATE an array dp of same size as nums, initialized with 1s

  FOR i from 1 to length(nums) - 1:
    FOR j from 0 to i - 1:
      IF nums[i] > nums[j] THEN
        dp[i] = max(dp[i], dp[j] + 1)
      END IF
    END FOR
  END FOR

  RETURN the maximum value in dp
END FUNCTION