How to find the longest increasing not sorted subsequence (LIS) of a sequence of numbers in VB.NET

1 Answer

0 votes
Imports System

Class Program
    Private Shared Function LongestIncreasingSubsequence(ByVal nums As Integer()) As Integer
        If nums Is Nothing OrElse nums.Length = 0 Then
            Return 0
        End If

        Dim length As Integer() = New Integer(nums.Length - 1) {}
        Array.Fill(length, 1)
        Dim maxLength As Integer = 1

        For i As Integer = 1 To nums.Length - 1
            For j As Integer = 0 To i - 1
                If nums(i) > nums(j) Then
                    length(i) = Math.Max(length(i), length(j) + 1)
                End If
            Next

            maxLength = Math.Max(maxLength, length(i))
        Next

        Return maxLength
    End Function

    Public Shared Sub Main()
        Dim nums As Integer() = {4, 5, 1, 10, 3, 9, 18, 19}

        Console.WriteLine($"Length of LIS: {LongestIncreasingSubsequence(nums)}")
    End Sub
End Class


' run:
'
' ength of LIS: 5
'

 



answered Jun 8, 2019 by avibootz
edited Nov 7, 2025 by avibootz
...