题解 | #不能连续吃草的牛#

不能连续吃草的牛

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;
    }
}

全部评论

相关推荐

苗条的伊泽瑞尔最喜欢...:同28届被压力了,电科✌就不能去卷算法吗?把Java留给我们双非卷
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务