Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Jan 5, 2024
1 parent 7cff5d3 commit c92ea42
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// https://leetcode.com/problems/longest-increasing-subsequence/

package main

import (
"fmt"
)

func lengthOfLIS(nums []int) int {
result := 1
n := len(nums)
f := make([]int, n+1)

for i := range f {
f[i] = 1
}

for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if nums[j] < nums[i] {
f[i] = max(f[i], f[j]+1)
result = max(result, f[i])
}
}
}

return result
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func main() {
nums := []int{10, 9, 2, 5, 3, 7, 101, 18}

fmt.Println(lengthOfLIS(nums))
}

0 comments on commit c92ea42

Please sign in to comment.