Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-09-24 16:55:16
https://leetcode.com/problems/champagne-tower/description
799. Champagne Tower
有一个香槟塔,第0层有一个杯子,第1层有两个杯子(编号为0和1),以此类推,共有
100层杯子。
每个杯子可以装1个单位的香槟,超出的部分会均匀的往下一层流,若我们往
香槟塔的顶部倒入poured单位的香槟,求出第query_row层的编号为query_glass的
杯子里面有多少香槟。
https://s3-lc-upload.s3.amazonaws.com/uploads/2018/03/09/tower.png
Example:
Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.00000
Explanation: We poured 1 cup of champange to the top glass of the tower
(which is indexed as (0, 0)). There will be no excess liquid so all the
glasses under the top glass will remain empty.
思路:
1.这个问题是一个递回问题,假如 f(i,j) 是第 i 层编号为 j 的杯子流下来的香槟单位
,那么 f(0,0) = poured,且 f(i,j) = (f(i-1, j)-1)/2 + (f(i-1, j-1)-1)/2。
(如果f(i,j)-1 < 0 取0)
2.有递回关系式之后就可以直接套递回求解了,但是递回过程中有需多重复计算,所以
我们可以对函数作 memoization 记录已经算过的值。
3.因为题目是要求杯子里的容量,所以我们取 MIN(f(i,j), 1) 即可。
Java Code:
作者: wwndbk (黑人问号)   2023-09-24 16:58:00
大师
楼主: Rushia (みけねこ的鼻屎)   2023-09-24 17:00:00
我看别人都用模拟的 我又想iwin了

Links booklink

Contact Us: admin [ a t ] ucptt.com