美文网首页
375. Guess Number Higher or Lowe

375. Guess Number Higher or Lowe

作者: becauseyou_90cd | 来源:发表于2018-07-26 06:03 被阅读0次

    https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/

    解题思路:

    1. 用深搜方法

    代码:
    class Solution {
    public int getMoneyAmount(int n) {

        int[][] table = new int[n+1][n+1];
        return dp(table, 1, n);
    }
    
    public int dp(int[][] t, int begin, int end){
        if(begin >= end) return 0;
        if(t[begin][end] != 0) return t[begin][end];
        int res = Integer.MAX_VALUE;
        for(int i = begin; i <= end; i++){
            int temp = i + Math.max(dp(t, begin, i - 1), dp(t, i + 1, end));
            res = Math.min(res, temp);
        }
        t[begin][end] = res;
        return res;
    }
    

    }

    相关文章

      网友评论

          本文标题:375. Guess Number Higher or Lowe

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