-
Notifications
You must be signed in to change notification settings - Fork 0
121. Best Time to Buy and Sell Stock #6
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| vector<int> minPricesFromStart; | ||
| vector<int> maxPricesFromEnd; | ||
|
|
||
| int minPrice = INT_MAX; | ||
| for(int i = 0; i < prices.size(); i++){ | ||
| minPrice = min(minPrice, prices[i]); | ||
| minPricesFromStart.push_back(minPrice); | ||
| } | ||
| int maxPrice = -1; | ||
| for(int i = prices.size()-1; i >= 0; i--){ | ||
| maxPrice = max(maxPrice, prices[i]); | ||
| maxPricesFromEnd.push_back(maxPrice); | ||
| } | ||
| reverse(maxPricesFromEnd.begin(), maxPricesFromEnd.end()); | ||
|
|
||
| int maxProfit = 0; | ||
| for(int i = 0; i < prices.size(); i++){ | ||
| int currentProfit = maxPricesFromEnd[i] - minPricesFromStart[i]; | ||
| maxProfit = max(maxProfit, currentProfit); | ||
| } | ||
| return maxProfit; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| int maxPrice = 0; | ||
| int minPrice = INT_MAX; | ||
| int maxProfit = 0; | ||
| for (int i = 0; i < prices.size(); i++) { | ||
| if (prices[i] < minPrice) { | ||
| minPrice = prices[i]; | ||
| } else { | ||
| maxPrice = prices[i]; | ||
| int currentProfit = maxPrice - minPrice; | ||
| maxProfit = max(maxProfit, currentProfit); | ||
| } | ||
| } | ||
| return maxProfit; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| int minPrice = INT_MAX; | ||
| int maxProfit = 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. ありがとうございます、以後気をつけます。 |
||
| for(int i = 0; i < prices.size(); i++) { | ||
|
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. 手癖でインデックスを使うfor文で回してしまっていたので今後使い分けます。 |
||
| if (prices[i] < minPrice) { | ||
| minPrice = prices[i]; | ||
| } else { | ||
| maxProfit = max(maxProfit, prices[i] - minPrice); | ||
| } | ||
| } | ||
|
|
||
| return maxProfit; | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public: | ||
| int maxProfit(vector<int>& prices) { | ||
| if(prices.empty())abort(); | ||
|
|
||
| int minPrice = prices[0]; | ||
| int currentProfit = 0; | ||
| for(int price: prices) { | ||
| if (price < minPrice) { | ||
| minPrice = price; | ||
| } else { | ||
| currentProfit = max(currentProfit, price - minPrice); | ||
| } | ||
| } | ||
|
|
||
| return currentProfit; | ||
| } | ||
| }; |
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.
個人的には
std::numeric_limits<int>::max()を使いたいです。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.
CのマクロではなくC++の標準ライブラリから使う機能を選んだほうが良さそうですね、以後気をつけます。