public class Solution {
    public int GetMoneyAmount(int n)
        {
            int[,] table = new int[n + 1, n + 1];
            return DP(table, 1, n);
        }

        int DP(int[,] t, int s, int e)
        {
            if (s >= e) return 0;
            if (t[s, e] != 0) return t[s, e];
            int res = int.MaxValue;
            for (int x = s; x <= e; x++)
            {
                int tmp = x + Math.Max(DP(t, s, x - 1), DP(t, x + 1, e));
                res = Math.Min(res, tmp);
            }
            t[s, e] = res;
            return res;
        }
}

 

public class Solution {
    public int GetMoneyAmount(int n)
        {
            int[,] table = new int[n + 1, n + 1];
            for (int j = 2; j <= n; j++)
            {
                for (int i = j - 1; i > 0; i--)
                {
                    int globalMin = int.MaxValue;
                    for (int k = i + 1; k < j; k++)
                    {
                        int localMax = k + Math.Max(table[i, k - 1], table[k + 1, j]);
                        globalMin = Math.Min(globalMin, localMax);
                    }
                    table[i, j] = i + 1 == j ? i : globalMin;
                }
            }
            return table[1, n];
        }
}

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

posted on 2017-05-24 09:51  Sempron2800+  阅读(126)  评论(0编辑  收藏  举报