iT邦幫忙

2023 iThome 鐵人賽

DAY 24
0
自我挑戰組

leetcode題目分享系列 第 24

[Day 24] 799. Champagne Tower

  • 分享至 

  • xImage
  •  

使用dp紀錄到水到的杯子

ref:https://leetcode.com/problems/champagne-tower/solutions/1818207/c-detailed-explanation-w-recursion-to-memoziation-understand-concept/?envType=daily-question&envId=2023-09-24

class Solution {
public:
    double t[101][101];
    double solve(int poured, int row, int glass)
    {
        if(row < 0 || glass > row || glass < 0){
            return 0.00;
        }
        
        if(row == 0 && glass == 0)
        {
            return poured;
        }
        
        if(t[row][glass] > -1)
        {
            return t[row][glass];
        }
        double left = (solve(poured, row - 1, glass - 1) - 1) / 2;
        if(left < 0)
        {
            left = 0;
        }
        
        double right = (solve(poured, row - 1, glass) - 1) / 2;
        if(right < 0)
        {
            right = 0;
        }
        
        double total = left + right;
        
        return t[row][glass] = total;
        
        
    }
    double champagneTower(int poured, int query_row, int query_glass) {
        memset(t, -1, sizeof(t));
        
        return min(1.00, solve(poured, query_row, query_glass));
    }
};

上一篇
[Day 23] 1048. Longest String Chain
下一篇
[Day 25] 389. Find the Difference
系列文
leetcode題目分享30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言