MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / LongestIncreasingSubsequence

Function LongestIncreasingSubsequence

dynamic/longestincreasingsubsequence.go:15–33  ·  view source on GitHub ↗

LongestIncreasingSubsequence returns the longest increasing subsequence where all elements of the subsequence are sorted in increasing order

(elements []int)

Source from the content-addressed store, hash-verified

13// LongestIncreasingSubsequence returns the longest increasing subsequence
14// where all elements of the subsequence are sorted in increasing order
15func LongestIncreasingSubsequence(elements []int) int {
16 n := len(elements)
17 lis := make([]int, n)
18 for i := range lis {
19 lis[i] = 1
20 }
21 for i := range lis {
22 for j := 0; j < i; j++ {
23 if elements[i] > elements[j] && lis[i] < lis[j]+1 {
24 lis[i] = lis[j] + 1
25 }
26 }
27 }
28 res := 0
29 for _, value := range lis {
30 res = max.Int(res, value)
31 }
32 return res
33}

Callers

nothing calls this directly

Calls 1

IntFunction · 0.92

Tested by

no test coverage detected