-
Notifications
You must be signed in to change notification settings - Fork 0
122. Best Time to Buy and Sell Stock II #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
colorbox
wants to merge
1
commit into
main
Choose a base branch
from
122
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| --:-- | ||
|
|
||
| Time complexity: O(n) | ||
| Space complexity: O(1) | ||
|
|
||
| 計算ミスをしてしまい、1円でも特になるなら常に売買してよい、という条件を見落としてしまう。 | ||
| 底値で買って最高値で売るを繰り返すというコードを書こうとしたが、いまいちコードに落とし込めなかった。 | ||
| などのミスによって初見で解けなかったので解説などを見た | ||
|
|
||
| 全探索→DP→貪欲法という順序を進んでとければ良かったが、DPの構造を見出せなかったのでstep2以降で練習していく | ||
| 自分の良くない思考の癖みたいなのをLLMとのやり取りの中で見つけられたので次回以降は改善していきたい | ||
| ボトムアップで考えず、ひらめきによるショートカットを試みようとする悪癖があり、それが出るとコードが書けなくてハマってしまう。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. パソコンで書く前に
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leetcode上でアクセプトされることを意識しすぎないことが大事だと思います。なんとなくですが、以下のことを意識するとよいと思いました。
・コードを書く前に実装したいことや入出力を確認すること(決まっていないものは自分で決めていいです)
・どんなコードを書けばいいか簡単に説明を入れること
・実行時間はどれくらいかを見積もること
・(テストケースを自分で書いてみること)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、コメントありがとうございます、参考にします。