guess-number-higher-or-lower-ii runtime error
shipleyxie opened this issue · 1 comments
shipleyxie commented
guess-number-higher-or-lower-ii get error while running in leetcode enviroment
Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6040000000d0 overflowed to 0x6040000000cc (stl_vector.h)
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];
}
};
Your code work fine in my local machie but can't pass in Leetcode envrioment.
Do you have any idea how to solve this?
kamyu104 commented
- The problem is
pay[i][k - 1]
may incur access violation sincek - 1 < 0
is possible. - Fixed, thank you very much!!