题解 | #牛群的可视高度#

牛群的可视高度

https://www.nowcoder.com/practice/942d0585a5654dbb9d5000feaa4e177e

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param cowHeights int整型一维数组 
# @return int整型
#

import math

class Solution:
    def visibleCows(self , cowHeights: List[int]) -> int:
        # write code here
        length = len(cowHeights)

        dp = [1] * length

        max_height = -math.inf

        for i in range(length):
            for j in range(i):
                max_height = max(max_height, cowHeights[j])
                if cowHeights[j] < cowHeights[i] and max_height < cowHeights[i]:
                    dp[i] = max(dp[i], dp[j]+1)
        return max(dp)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务