https://leetcode.com/problems/guess-number-higher-or-lower-ii/description/
解题思路:
- 用深搜方法
代码:
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;
}
}
网友评论