From f0e6da5e390d4c0fc5c12299bf16a9d5eae7683e Mon Sep 17 00:00:00 2001 From: Ryohei Sato <130881456+Satorien@users.noreply.github.com> Date: Tue, 5 Aug 2025 11:45:45 +0900 Subject: [PATCH 1/2] Create 122. Best Time to Buy and Sell Stock II.md --- 122. Best Time to Buy and Sell Stock II.md | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 122. Best Time to Buy and Sell Stock II.md diff --git a/122. Best Time to Buy and Sell Stock II.md b/122. Best Time to Buy and Sell Stock II.md new file mode 100644 index 0000000..94f5509 --- /dev/null +++ b/122. Best Time to Buy and Sell Stock II.md @@ -0,0 +1,95 @@ +## Step 1. Initial Solution + +- ある点までの利益とその後の利益を足す? + - 何回も足す必要があるしO(n)を何回かやることになる +- よく考えると上がるところを全部計上すれば良いだけ + - これで実装して以下の通り + - prices[0]で初期化するべきかは悩んだがその日のうちに売ることもできるという文脈を踏まえて含めた + - ただこのままだと前日に買っていてかつそれが今日より安かったら売る、という状況になっていて現実の動作には即していない + +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + prev_price = prices[0] + profit = 0 + for price in prices: + if price > prev_price: + profit += price - prev_price + prev_price = price + return profit +``` + +### Complexity Analysis + +- 時間計算量:O(n) +- 空間計算量:O(1) + +## Step 2. Alternatives + +- もっとシンプルに書くならこれでも良い + + ```python + class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + for i in range(1, len(prices)): + if prices[i] > prices[i-1]: + profit += prices[i] - prices[i-1] + return profit + ``` + + - とても当たり前だがなぜか上の書き方で書いてしまった + - iで動かそうとしたら書けていたかも +- +αできかれそうなこと + + > この利益を実現するための最小売買回数を求められますか? + > + - https://github.com/Yoshiki-Iwasa/Arai60/pull/53/files#r1730194725 + - 上がり始めたから下がるまでが1回でこれをカウントしていく + + ```python + class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + buy = prices[0] + hold = False + transactions = 0 + for i in range(1, len(prices)): + if prices[i] < prices[i-1]: + if hold: + profit += prices[i - 1] - buy + hold = False + transactions += 1 + buy = prices[i] + else: + hold = True + if hold: + profit += prices[-1] - buy + transactions += 1 + return profit, transactions + ``` + +- 変数名 + - 山と谷というイメージ + - topとbottomという表現はちょうど良い + - https://github.com/olsen-blue/Arai60/pull/38/files + - 例外があるという話 + - 確かにわざわざ山と谷と呼ぶことで例外を想定しにくくなっている気がする + - 上にあるようなtop/bottomは問題なさそう + - https://github.com/goto-untrapped/Arai60/pull/59/files#r1777077060 + +## Step 3. Final Solution + +- シンプルな書き方に帰着 + - 空リストの場合はエラーを返さない点に注意 + +```python +class Solution: + def maxProfit(self, prices: List[int]) -> int: + profit = 0 + for i in range(1, len(prices)): + price_diff = prices[i] - prices[i-1] + if price_diff > 0: + profit += price_diff + return profit +``` From 362cc43539a62f43b4abc33b8ede02a28fc4ea4a Mon Sep 17 00:00:00 2001 From: Ryohei Sato <130881456+Satorien@users.noreply.github.com> Date: Tue, 5 Aug 2025 11:46:26 +0900 Subject: [PATCH 2/2] Rename 122. Best Time to Buy and Sell Stock II.md to Python3/122. Best Time to Buy and Sell Stock II.md --- .../122. Best Time to Buy and Sell Stock II.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 122. Best Time to Buy and Sell Stock II.md => Python3/122. Best Time to Buy and Sell Stock II.md (100%) diff --git a/122. Best Time to Buy and Sell Stock II.md b/Python3/122. Best Time to Buy and Sell Stock II.md similarity index 100% rename from 122. Best Time to Buy and Sell Stock II.md rename to Python3/122. Best Time to Buy and Sell Stock II.md