vocus logo

方格子 vocus

LeetCode -- 808. Soup Servings

更新 發佈閱讀 8 分鐘

Soup Servings - LeetCode

Description

You have two soups, A and B, each starting with n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns:

你有兩碗湯,A 和 B,每碗湯的起始量為 n 毫升。在每一輪中,系統都會隨機選擇以下四種湯品的其中一種,每種湯品的機率為 0.25 ,與之前的所有輪次無關:

  • pour 100 mL from type A and 0 mL from type B
    從 A 型倒入 100 毫升,從 B 型倒入 0 毫升
  • pour 75 mL from type A and 25 mL from type B
    從 A 型倒入 75 毫升,從 B 型倒入 25 毫升
  • pour 50 mL from type A and 50 mL from type B
    從 A 型倒入 50 毫升,從 B 型倒入 50 毫升
  • pour 25 mL from type A and 75 mL from type B
    從 A 型倒入 25 毫升,從 B 型倒入 75 毫升

Note: 

  • There is no operation that pours 0 mL from A and 100 mL from B.
    不存在從 A 倒出 0 mL,從 B 倒出 100 mL 的操作。
  • The amounts from A and B are poured simultaneously during the turn.
    在輪流期間,A 和 B 中的量會同時倒入。
  • If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup.
    如果操作人員要求您倒出比剩餘湯更多的湯,請將剩餘的湯全部倒出。

The process stops immediately after any turn in which one of the soups is used up.

任何一輪用完一碗湯後,該過程都會立即停止。

Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn. Answers within 10-5 of the actual answer will be accepted.

回傳 A 先於 B 用完的機率,加上兩杯湯在同一輪次用完的機率的一半。答案與實際答案的誤差在 10-5 以內即可接受。

Solution

If we use basic recursion—setting aside Python's maximum recursion depth—even a test case with n = 800 will cause the code to exceed the time limit, , therefore we need the following concepts.

如果我們使用最基礎的遞迴,先不論 python 的遞迴最大深度,光 n = 800 的測資就會讓程式碼 Time Limit Exceeded,因此我們需要以下概念。

Core concept

  • cache:This section uses Python's built-in caching tool, lru_cache. For usage and a detailed introduction, you can refer to this article, which explains why caching is needed and how to use lru_cache effectively.
    快取:這裡使用 python 內建的快取 lru_cache。使用方式和介紹可以參考這篇,裡面詳細介紹了為何需要快取以及 lru_cache 的使用方法。
  • Answers within 10-5 of the actual answer will be accepted. This means that in some case we can just return 1 when n is very big
    答案與實際答案的誤差在 10-5 以內即可接受。這代表當 n 很大時,我們可以直接回傳 1。
  • Last but not the least, the number of milliliters poured out is a multiple of 25, so we can record 25 as 1, 50 as 2, and so on.
    最後,同時也很重要的概念,倒出的毫升數都是 25 的倍數,因此,我們可以將 25 記為 1,50 記為 2,以此類推。

Code

from functools import lru_cache
import math
class Solution:
def soupServings(self, n: int) -> float:
if n >= 4800:
return 1.0
n = math.ceil(n/25)
return self.recursion(n,n)

@lru_cache(None)
def recursion(self, A, B):
if A <= 0 and B <= 0:
return 0.5
elif A<=0:
return 1
elif B<=0:
return 0
return 0.25*(
(self.recursion(A-4,B))+
(self.recursion(A-3,B-1))+
(self.recursion(A-2,B-2))+
(self.recursion(A-1,B-3))
)

If you like this article, give it a like. OwO b

如果這篇對你有幫助請按個讚 ouob。

留言
avatar-img
周濡墨的沙龍
19會員
121內容數
有別於未付費的文章,專題區的文章偏向愛情短篇小說,較有戲劇性,篇幅也會較長;未付費文章會偏向極短篇小說,並且以類似散文的手法寫作
周濡墨的沙龍的其他內容
2025/08/06
將水果放入籃子 - LeetCode --- Fruit Into Baskets - LeetCode Description You are visiting a farm that has a single row of fruit trees arranged from......
2025/08/06
將水果放入籃子 - LeetCode --- Fruit Into Baskets - LeetCode Description You are visiting a farm that has a single row of fruit trees arranged from......
2025/08/01
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly.....
Thumbnail
2025/08/01
Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly.....
Thumbnail
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
2025/07/30
information link:Two Sum - LeetCode Description:Given an array of integers nums and an integer target, return indices of the two numbers such......
看更多
你可能也想看
Thumbnail
債券投資,不只是高資產族群的遊戲 在傳統的投資觀念中,海外債券(Overseas Bonds)常被貼上「高資產族群專屬」的標籤。過去動輒 1 萬甚至 10 萬美元的最低申購門檻,讓許多想尋求穩定配息的小資族望而卻步。 然而,在股市波動劇烈的環境下,尋求穩定的美元現金流與被動收入成為許多投資人
Thumbnail
債券投資,不只是高資產族群的遊戲 在傳統的投資觀念中,海外債券(Overseas Bonds)常被貼上「高資產族群專屬」的標籤。過去動輒 1 萬甚至 10 萬美元的最低申購門檻,讓許多想尋求穩定配息的小資族望而卻步。 然而,在股市波動劇烈的環境下,尋求穩定的美元現金流與被動收入成為許多投資人
Thumbnail
透過川普的近期債券交易揭露,探討債券作為資產配置中「穩定磐石」的重要性。文章分析降息對債券的潛在影響,以及股神巴菲特的操作策略。並介紹玉山證券「小額債」平臺,如何讓小資族也能低門檻參與海外債券市場,實現「低門檻、低波動、固定收益」的務實投資方式。
Thumbnail
透過川普的近期債券交易揭露,探討債券作為資產配置中「穩定磐石」的重要性。文章分析降息對債券的潛在影響,以及股神巴菲特的操作策略。並介紹玉山證券「小額債」平臺,如何讓小資族也能低門檻參與海外債券市場,實現「低門檻、低波動、固定收益」的務實投資方式。
Thumbnail
解析「債券」如何成為資產配置中的穩定錨,提供低風險高回報的投資選項。 藉由玉山證券的低門檻債券服務,投資者可輕鬆入手,平衡風險並穩定財務。
Thumbnail
解析「債券」如何成為資產配置中的穩定錨,提供低風險高回報的投資選項。 藉由玉山證券的低門檻債券服務,投資者可輕鬆入手,平衡風險並穩定財務。
Thumbnail
相較於波動較大的股票,債券能提供固定現金流,而玉山證券推出的小額債,更以1000 美元的低門檻,讓學生與新手也能參與全球優質企業債投資。玉山E-Trader平台即時報價、條件式篩選與清楚的交易流程等特色,大幅降低投資難度,對於希望分散風險、建立穩定現金流的人來說,玉山小額債是一個值得嘗試的理財起點。
Thumbnail
相較於波動較大的股票,債券能提供固定現金流,而玉山證券推出的小額債,更以1000 美元的低門檻,讓學生與新手也能參與全球優質企業債投資。玉山E-Trader平台即時報價、條件式篩選與清楚的交易流程等特色,大幅降低投資難度,對於希望分散風險、建立穩定現金流的人來說,玉山小額債是一個值得嘗試的理財起點。
Thumbnail
沒有好不好,只有怎麼配!輕鬆配出你的每日均衡營養!
Thumbnail
沒有好不好,只有怎麼配!輕鬆配出你的每日均衡營養!
Thumbnail
前 一個飯糰 飢止腹滿 今 一桌滿漢 飢渴難填 嘴是同一張 索求不一樣
Thumbnail
前 一個飯糰 飢止腹滿 今 一桌滿漢 飢渴難填 嘴是同一張 索求不一樣
Thumbnail
這是一個美味的白菜滷食譜,使用多種食材和調味料結合而成。這道菜不僅美味,而且營養豐富,適閤家庭聚餐或日常食用。
Thumbnail
這是一個美味的白菜滷食譜,使用多種食材和調味料結合而成。這道菜不僅美味,而且營養豐富,適閤家庭聚餐或日常食用。
Thumbnail
上次失敗後,重新研究,這次成功了,分享一下食譜 材料: 米:1.5半 水:1.5杯 肝腸:兩條 臘腸:兩條 蔬菜高湯包粉:一小包 蔥:一支 乾蒜片:一湯匙 橄欖油:少許 米酒:一小匙 醬料: 蠔油:一大匙 清醬油:三大匙 香油:一小匙 米酒:一小匙 前置作業: 1.
Thumbnail
上次失敗後,重新研究,這次成功了,分享一下食譜 材料: 米:1.5半 水:1.5杯 肝腸:兩條 臘腸:兩條 蔬菜高湯包粉:一小包 蔥:一支 乾蒜片:一湯匙 橄欖油:少許 米酒:一小匙 醬料: 蠔油:一大匙 清醬油:三大匙 香油:一小匙 米酒:一小匙 前置作業: 1.
Thumbnail
每天晚餐吃什麼?每天晚餐煮什麼?
Thumbnail
每天晚餐吃什麼?每天晚餐煮什麼?
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News