求整数数组中最长递增子序列的长度

思想:参考https://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/


func CeilIndex(nums[]int,l,r,key int)int  {
	for r-l>1  {
		m :=l+(r-l)/2
		if nums[m]>=key{
			r = m
		}else {
			l = m
		}
	}
	return r
}
func lengthOfLIS(nums []int) int {
	if len(nums)==0{
		return 0
	}

	var tail []int
	tail = append(tail,nums[0])
	for i:=1;i<len(nums);i++{
		if nums[i]<tail[0]{
			tail[0] = nums[i]
		}else if nums[i]>tail[len(tail)-1]{
			tail = append(tail,nums[i])
		}else {
			tail[CeilIndex(tail,-1,len(tail)-1,nums[i])] = nums[i]
		}
	}
	return len(tail)
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页