-
Notifications
You must be signed in to change notification settings - Fork 0
121. Best Time to Buy and Sell Stock #40
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
ryoooooory
wants to merge
2
commits into
main
Choose a base branch
from
task/121
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,33 @@ | ||
| /* | ||
| * ・概要 | ||
| * 自力解法 | ||
| * 解法は全探索、DPすぐに思い浮かんだ。5分ほどでAC | ||
| * DP解法としては、あるindexの要素に着目したとき、利益はそれまでの最低の価格で買った時との差分であるので、最低値のみを保持して毎回現在との差分を取れば良い。 | ||
| * これは1回の配列走査でおわるのでO(n)となり、変数だけもっておけばいいので空間もO(1) | ||
| * | ||
| * ・計算量 | ||
| * O(n):nは配列の要素数 | ||
| * O(1) | ||
| * | ||
| * ・その他 | ||
| * せっかくなのであとで、全探索versionもかく。 | ||
| * ループは0でなく1からで良さそう。 | ||
| */ | ||
|
|
||
| public class solution1_1 { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices.length == 0 || prices.length == 1) { | ||
| return 0; | ||
| } | ||
| int maxProfit = 0; | ||
| int minPriceBefore = prices[0]; | ||
| for (int i = 0; i < prices.length; i++) { | ||
| int currentMaxProfit = Math.max(0, prices[i] - minPriceBefore); | ||
| maxProfit = Math.max(maxProfit, currentMaxProfit); | ||
| if (prices[i] < minPriceBefore) { | ||
| minPriceBefore = prices[i]; | ||
| } | ||
| } | ||
| return maxProfit; | ||
| } | ||
| } |
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,31 @@ | ||
| /* | ||
| * ・概要 | ||
| * 自力解法 | ||
| * 全探索バージョン、二重ループ | ||
| * DP解法としては、あるindexの要素に着目したとき、利益はそれまでの最低の価格で買った時との差分であるので、最低値のみを保持して毎回現在との差分を取れば良い。 | ||
| * これは1回の配列走査でおわるのでO(n)となり、変数だけもっておけばいいので空間もO(1) | ||
| * | ||
| * ・計算量 | ||
| * O(n^2):今回は最大でn=10^5となるので、最大10Gかかる。Macだとクロック数が数GHzなので、単純計算でも3sくらいはかかる。 | ||
| * Javaのオーバーヘッドを加味して、だいたい3倍くらいかかる(10sくらい)という予想 | ||
| * 以前共有いただいたJavaとかのベンチマーク | ||
| * https://benchmarksgame-team.pages.debian.net/benchmarksgame/box-plot-summary-charts.html | ||
| * https://github.com/niklas-heer/speed-comparison | ||
| * | ||
| * | ||
| */ | ||
|
|
||
| public class solution1_2 { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices.length == 0 || prices.length == 1) { | ||
| return 0; | ||
| } | ||
| int maxProfit = 0; | ||
| for (int i = 0; i < prices.length; i++) { | ||
| for (int j = 0; j < i; j++) { | ||
| maxProfit = Math.max(maxProfit, prices[i] - prices[j]); | ||
| } | ||
| } | ||
| return maxProfit; | ||
| } | ||
| } | ||
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,43 @@ | ||
| /* | ||
| * https://github.com/irohafternoon/LeetCode/pull/40/files | ||
| * たしかにif文で分岐させると意味合い的にもわけられていいかも | ||
| * | ||
| * https://github.com/huyfififi/coding-challenges/pull/4/files | ||
| * たしかに後ろからみていくのも悪くない。直感的なのは左からみていくだが、(未来 - 過去)という構図がわかりやすいなと思った。 | ||
| * | ||
| * https://github.com/seal-azarashi/leetcode/pull/35/files | ||
| * 大体同じ | ||
| * | ||
| * | ||
| * 以下では後ろからみていく実装を行う。 | ||
| * | ||
| * ・計算量 | ||
| * time: O(n), space: O(1) | ||
|
|
||
| * ・その他 | ||
| * maxPriceInFutureはやりすぎかも、maxPriceだけでも十分伝わりそう | ||
| * | ||
| * https://github.com/colorbox/leetcode/pull/6/files | ||
| * https://github.com/goto-untrapped/Arai60/pull/58/files#r1782742318 | ||
| * 関数型的考え、scanlを調べたところ、累積結果をすべて返す関数らしく、scanl (+) 0 [1, 2, 3, 4] -- => [0, 1, 3, 6, 10], この例だと累積和だ | ||
| * ということで最小値と最大値を累積していき、最後にその結果の配列を元にprofitをみていくことだと解釈した。 | ||
|
|
||
| */ | ||
|
|
||
| public class solution2_1 { | ||
| public int maxProfit(int[] prices) { | ||
| if (prices.length == 0 || prices.length == 1) { | ||
| return 0; | ||
| } | ||
| int maxProfit = 0; | ||
| int maxPriceInFuture = prices[prices.length - 1]; | ||
| for (int i = prices.length - 2; i >= 0; i--) { | ||
| if (maxPriceInFuture < prices[i]) { | ||
| maxPriceInFuture = prices[i]; | ||
| continue; | ||
| } | ||
| maxProfit = Math.max(maxProfit, maxPriceInFuture - prices[i]); | ||
| } | ||
| return maxProfit; | ||
| } | ||
| } |
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.
nit: 私だったら
iとjをsell_i,buy_iとしてもう少し情報を乗せるかなと思いました。The Art of Readable Codeにそんなことが載っていたな...と思い読み返してみたところ、
とし、3重ループの例を挙げて
とするよりも
とした方がわかりやすいと指摘されていました。Best Time to Buy and Sell Stockの場合同じ
pricesにアクセスしているので、The Art of Readable Codeの例とは若干設定が異なるのですが、参考まで。