Leetcode 375 Solution
This article provides solution to leetcode question 375 (guess-number-higher-or-lower-ii)
Access this page by simply typing in "lcs 375" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/guess-number-higher-or-lower-ii
Solution
class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int>> dp;
for (int i = 0; i <= n; i++)
{
vector<int> b(n + 1);
dp.push_back(b);
}
for (int i = 0; i < n; i++)
{
for (int j = 1; i + j <= n; j++)
{
int l = j;
int r = i + j;
if (l == r)
dp[l][r] = 0;
else
{
dp[l][r] = min(l + dp[l + 1][r], r + dp[l][r - 1]);
for (int k = l + 1; k <= r - 1; k++)
{
dp[l][r] = min(dp[l][r], k + max(dp[l][k - 1], dp[k + 1][r]));
}
}
}
}
return dp[1][n];
}
};