From 685e540a9f7b1fcbd1a976ea103082c8c2416264 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Thu, 23 Apr 2026 17:03:21 +0900 Subject: [PATCH 1/2] add files --- 0122-best-time-to-buy-and-sell-stock-II/memo.md | 0 .../step1.cpp | 17 +++++++++++++++++ .../step2.cpp | 12 ++++++++++++ .../step3.cpp | 17 +++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 0122-best-time-to-buy-and-sell-stock-II/memo.md create mode 100644 0122-best-time-to-buy-and-sell-stock-II/step1.cpp create mode 100644 0122-best-time-to-buy-and-sell-stock-II/step2.cpp create mode 100644 0122-best-time-to-buy-and-sell-stock-II/step3.cpp diff --git a/0122-best-time-to-buy-and-sell-stock-II/memo.md b/0122-best-time-to-buy-and-sell-stock-II/memo.md new file mode 100644 index 0000000..e69de29 diff --git a/0122-best-time-to-buy-and-sell-stock-II/step1.cpp b/0122-best-time-to-buy-and-sell-stock-II/step1.cpp new file mode 100644 index 0000000..41f8e90 --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-II/step1.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int hold = -prices[0]; + int not_hold = 0; + + for (int i = 1; i < prices.size(); i++) { + int new_hold = max(hold, not_hold - prices[i]); + int new_not_hold = max(not_hold, hold + prices[i]); + + hold = new_hold; + not_hold = new_not_hold; + } + + return not_hold; + } +}; diff --git a/0122-best-time-to-buy-and-sell-stock-II/step2.cpp b/0122-best-time-to-buy-and-sell-stock-II/step2.cpp new file mode 100644 index 0000000..83d0d17 --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-II/step2.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int profit = 0; + for (int i = 1; i < prices.size(); i++) { + if (prices[i] > prices[i - 1]) { + profit += prices[i] - prices[i - 1]; + } + } + return profit; + } +}; diff --git a/0122-best-time-to-buy-and-sell-stock-II/step3.cpp b/0122-best-time-to-buy-and-sell-stock-II/step3.cpp new file mode 100644 index 0000000..41f8e90 --- /dev/null +++ b/0122-best-time-to-buy-and-sell-stock-II/step3.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxProfit(vector& prices) { + int hold = -prices[0]; + int not_hold = 0; + + for (int i = 1; i < prices.size(); i++) { + int new_hold = max(hold, not_hold - prices[i]); + int new_not_hold = max(not_hold, hold + prices[i]); + + hold = new_hold; + not_hold = new_not_hold; + } + + return not_hold; + } +}; From 53433ceb91103d8800897d528b32c3b54c1ef502 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Tue, 28 Apr 2026 11:05:31 +0900 Subject: [PATCH 2/2] update memo.md --- 0122-best-time-to-buy-and-sell-stock-II/memo.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/0122-best-time-to-buy-and-sell-stock-II/memo.md b/0122-best-time-to-buy-and-sell-stock-II/memo.md index e69de29..0596300 100644 --- a/0122-best-time-to-buy-and-sell-stock-II/memo.md +++ b/0122-best-time-to-buy-and-sell-stock-II/memo.md @@ -0,0 +1,12 @@ +### step1 + +わからなかったのでChatGPTに解いてもらった。株を持っている状態と持っていない状態の最大値を更新していくDP。 + +### step2 + +greedyでも解けるということでChatGPTに解いてもらった。最初考えていたが実装できなかったアイデアに近かった。 +https://github.com/naoto-iwase/leetcode/pull/43/changes こちらのコードの実装1も読んでみたが、step2のコードとやってることはほぼ同じだった。 + +### step3 + +step1を3回通すまで書き直し。