-
Notifications
You must be signed in to change notification settings - Fork 0
122. Best Time to Buy and Sell Stock II #36
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
hemispherium
wants to merge
2
commits into
main
Choose a base branch
from
0122-best-time-to-buy-and-sell-stock-II
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,12 @@ | ||
| ### step1 | ||
|
|
||
| わからなかったのでChatGPTに解いてもらった。株を持っている状態と持っていない状態の最大値を更新していくDP。 | ||
|
|
||
| ### step2 | ||
|
|
||
| greedyでも解けるということでChatGPTに解いてもらった。最初考えていたが実装できなかったアイデアに近かった。 | ||
| https://github.com/naoto-iwase/leetcode/pull/43/changes こちらのコードの実装1も読んでみたが、step2のコードとやってることはほぼ同じだった。 | ||
|
|
||
| ### step3 | ||
|
|
||
| step1を3回通すまで書き直し。 |
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,17 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| int hold = -prices[0]; | ||
|
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. 了解です!ご指摘ありがとうございます。 |
||
| 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; | ||
| } | ||
| }; | ||
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,12 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& 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; | ||
| } | ||
| }; |
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,17 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& 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; | ||
| } | ||
| }; |
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.
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の問題が置く制約を満たさないような入力に対しても正常に動く実装になりますね.エラーを返す実装でもいいと思います.