Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 122/step1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
--:--

Time complexity: O(n)
Space complexity: O(1)

計算ミスをしてしまい、1円でも特になるなら常に売買してよい、という条件を見落としてしまう。

@h-masder h-masder Jun 19, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leetcode上でアクセプトされることを意識しすぎないことが大事だと思います。なんとなくですが、以下のことを意識するとよいと思いました。

・コードを書く前に実装したいことや入出力を確認すること(決まっていないものは自分で決めていいです)
・どんなコードを書けばいいか簡単に説明を入れること
・実行時間はどれくらいかを見積もること
・(テストケースを自分で書いてみること)

@colorbox colorbox Jun 22, 2026

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、コメントありがとうございます、参考にします。

底値で買って最高値で売るを繰り返すというコードを書こうとしたが、いまいちコードに落とし込めなかった。
などのミスによって初見で解けなかったので解説などを見た

全探索→DP→貪欲法という順序を進んでとければ良かったが、DPの構造を見出せなかったのでstep2以降で練習していく
自分の良くない思考の癖みたいなのをLLMとのやり取りの中で見つけられたので次回以降は改善していきたい
ボトムアップで考えず、ひらめきによるショートカットを試みようとする悪癖があり、それが出るとコードが書けなくてハマってしまう。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パソコンで書く前に
コードを含めて紙に書いてみるのもいいと思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、コメントありがとうございます。
脳内メモリから溢れないようにするのに手書きは良さそうですね。

全探索から考えていけるように練習していく


*/
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max_benefit = 0;
for (int i = 1; i < prices.size(); ++i) {
if (prices[i] > prices[i - 1]) {
max_benefit += prices[i] - prices[i - 1];
}
}
return max_benefit;
}
};
20 changes: 20 additions & 0 deletions 122/step2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*

株を持っている状態と持っていない状態に分けて、各状態で利益の多い値で更新していく方式。
最初DPの構造が見えていなかったので、LLMと壁打ちしてその辺りの構造を理解した。

*/

class Solution {
public:
int maxProfit(vector<int>& prices) {
int max_profit_with_stock = numeric_limits<int>::min();
int max_profit_without_stock = 0;
for (int i = 0; i < prices.size(); ++i) {
int tmp_max_profit_with_stock = max(max_profit_with_stock, max_profit_without_stock - prices[i]);
max_profit_without_stock = max(max_profit_without_stock, max_profit_with_stock + prices[i]);
max_profit_with_stock = tmp_max_profit_with_stock;
}
return max(max_profit_with_stock, max_profit_without_stock);
}
};
13 changes: 13 additions & 0 deletions 122/step3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
int maxProfit(vector<int>& prices) {
int max_profit = 0;
for (int i = 1; i < prices.size(); ++i) {
int profit = prices[i] - prices[i - 1];
if (profit > 0) {
max_profit += profit;
}
}
return max_profit;
}
};