-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution375.cs
31 lines (26 loc) · 892 Bytes
/
Solution375.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace LeetCode.Solutions;
public class Solution375
{
/// <summary>
/// 375. Guess Number Higher or Lower II - Medium
/// <a href="https://leetcode.com/problems/guess-number-higher-or-lower-ii">See the problem</a>
/// </summary>
public int GetMoneyAmount(int n)
{
var dp = new int[n + 1, n + 1];
for (var len = 2; len <= n; len++)
{
for (var start = 1; start <= n - len + 1; start++)
{
var minCost = int.MaxValue;
for (var pivot = start; pivot < start + len - 1; pivot++)
{
var cost = pivot + Math.Max(dp[start, pivot - 1], dp[pivot + 1, start + len - 1]);
minCost = Math.Min(minCost, cost);
}
dp[start, start + len - 1] = minCost;
}
}
return dp[1, n];
}
}