Skip to content

Instantly share code, notes, and snippets.

@lavantien
Created March 2, 2022 08:38
Show Gist options
  • Save lavantien/352d6401ec316e028480cf9d8338ac65 to your computer and use it in GitHub Desktop.
Save lavantien/352d6401ec316e028480cf9d8338ac65 to your computer and use it in GitHub Desktop.
Golang - Longest Increasing Sub-sequence
package main
import "fmt"
func main() {
arr := []int{3, 2, 6, 4, 5, 1}
lis := make([][]int, len(arr))
for i := 0; i < len(arr); i++ {
lis[i] = []int{}
}
lis[0] = append(lis[0], arr[0])
maxLength := 0
maxPos := 0
for i := 1; i < len(arr); i++ {
for j := 0; j < i; j++ {
if arr[j] < arr[i] && len(lis[i]) < len(lis[j])+1 {
lis[i] = lis[j]
}
}
lis[i] = append(lis[i], arr[i])
if len(lis[i]) > maxLength {
maxLength = len(lis[i])
maxPos = i
}
}
fmt.Println(lis[maxPos])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment