时间复杂度 O(n^2),n代表行数
空间复杂度 O(n^2)
class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
vector<vector<double>> received(101, vector<double>(101, 0));
received[0][0] = poured;
for (int i=0; i<received.size()-1; i++) {
for (int j=0; j<=i; j++) {
double overflow = (received[i][j] - 1)/2;
if (overflow > 0){
received[i+1][j] += overflow;
received[i+1][j+1] += overflow;
}
}
}
return min(1.0, received[query_row][query_glass]);
}
};
No comments:
Post a Comment