题解 | #不能连续吃草的牛#
不能连续吃草的牛
https://www.nowcoder.com/practice/64d9400c321042acb754a9455852a8d7
import java.util.*; public class Solution { public int eatGrass (int[] nums) { int n = nums.length; if (n == 0) { return 0; } if (n == 1) { return nums[0]; } int x = nums[0]; int y = Math.max(nums[0], nums[1]); int z=0; for (int i = 2; i < n; i++) {//滑动窗口的动态规划。 z = Math.max(y, x+ nums[i]); x=y; y=z; } return y; } }