package dynamic
func LongestIncreasingSubsequenceGreedy(nums []int) int {
longestIncreasingSubsequnce := make([]int, 0)
for _, num := range nums {
leftmostIndex := lowerBound(longestIncreasingSubsequnce, num)
if leftmostIndex == len(longestIncreasingSubsequnce) {
longestIncreasingSubsequnce = append(longestIncreasingSubsequnce, num)
} else {
longestIncreasingSubsequnce[leftmostIndex] = num
}
}
return len(longestIncreasingSubsequnce)
}
func lowerBound(arr []int, val int) int {
searchWindowLeft, searchWindowRight := 0, len(arr)-1
for searchWindowLeft <= searchWindowRight {
middle := (searchWindowLeft + searchWindowRight) / 2
if arr[middle] < val {
searchWindowLeft = middle + 1
} else {
searchWindowRight = middle - 1
}
}
return searchWindowRight + 1
}