题解 | #贪吃牛#
贪吃牛
https://www.nowcoder.com/practice/ae6261c872724fda8913b0377e85f6ab
# 啊终于有我会的动态规划题了 # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return int整型 # class Solution: def eatGrass(self , n: int) -> int: # write code here if n == 1 or n == 2: return n # O(n)方法 # grass = [1, 2] # for i in range(2, n): # grass.append(grass[i-1] + grass[i-2]) # return grass[-1] # O(1)方法 grass1 = 1 grass2 = 2 for i in range(2, n): grass = grass1 + grass2 grass1 = grass2 grass2 = grass return grass2