LongestIncreasingSubsequence returns the longest increasing subsequence where all elements of the subsequence are sorted in increasing order
(elements []int)
| 13 | // LongestIncreasingSubsequence returns the longest increasing subsequence |
| 14 | // where all elements of the subsequence are sorted in increasing order |
| 15 | func 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 | } |