题解 | #不能连续吃草的牛#
不能连续吃草的牛
https://www.nowcoder.com/practice/64d9400c321042acb754a9455852a8d7
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型
*/
private int energy = 0;
public int eatGrass (int[] nums) {
// write code here
return Math.max(eatNext(0,nums, 0), eatNext(1,nums, 0));
}
// 动态规划不太熟,第一反应直接用了递归
private int eatNext(int i, int[]nums ,int energy){
if(i<nums.length){
energy += nums[i];
return Math.max(eatNext(i+2,nums, energy), eatNext(i+3,nums, energy));
}else
return energy;
}
}
SHEIN希音公司福利 280人发布