美文网首页动态规划
leetcode 486- dp+递归

leetcode 486- dp+递归

作者: Ariana不会哭 | 来源:发表于2019-01-09 03:00 被阅读14次

    这个题是在搞得我头疼,所以就看了网上的答案:


    WeChat Image_20190108135634.jpg

    上面是我写的递归过程,红笔是player2 显然其实两个玩家的原则是一样的 就是将自己的数变大; 自己-后期别人的数 趋近于尽量大

    • code:
    //my
    int helper(vector<int>& nums, int l, int r) {
        if (l == r)
            return nums[l];
        else {
            return max(nums[l] - helper(nums, l + 1, r),
                nums[r] - helper(nums, l, r - 1));
        }
    }
    bool PredictTheWinner(vector<int>& nums) {
        return helper(nums, 0, nums.size() - 1) >= 0;
    }
    

    相关文章

      网友评论

        本文标题:leetcode 486- dp+递归

        本文链接:https://www.haomeiwen.com/subject/heoirqtx.html