Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions GuessNumberhigherorlower
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public int getMoneyAmount(int n) {
int[][] dp = new int[n + 1][n + 1];

return calculateCost(1, n, dp);
}

private int calculateCost(int start, int end, int[][] dp) {
if (start >= end) return 0;

if (dp[start][end] != 0) return dp[start][end];

int minCost = Integer.MAX_VALUE;

for (int guess = (start + end) / 2; guess <= end; guess++) {
int cost = guess + Math.max(calculateCost(start, guess - 1, dp), calculateCost(guess + 1, end, dp));
minCost = Math.min(minCost, cost);
}

dp[start][end] = minCost;
return minCost;
}
}