Skip to content

Commit

Permalink
Create guess-number-higher-or-lower-ii.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jul 16, 2016
1 parent d95bb71 commit 2771051
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions C++/guess-number-higher-or-lower-ii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Time: O(n^2)
// Space: O(n^2)

class Solution {
public:
int getMoneyAmount(int n) {
vector<vector<int>> pay(n + 1, vector<int>(n));
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
pay[i][j] = numeric_limits<int>::max();
for (int k = i; k <= j; ++k) {
pay[i][j] = min(pay[i][j], k + 1 + max(pay[i][k - 1], pay[k + 1][j]));
}
}
}
return pay[0][n - 1];
}
};

0 comments on commit 2771051

Please sign in to comment.