Skip to content
Merged
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
26 changes: 26 additions & 0 deletions 121/1.cpp
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;
}
};
18 changes: 18 additions & 0 deletions 121/2.cpp
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;
}
};
16 changes: 16 additions & 0 deletions 121/3.cpp
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;
Copy link
Copy Markdown

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() を使いたいです。

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.

CのマクロではなくC++の標準ライブラリから使う機能を選んだほうが良さそうですね、以後気をつけます。

int maxProfit = 0;
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.

ありがとうございます、以後気をつけます。

for(int i = 0; i < prices.size(); i++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

prices[i] の形でしか prices を参照していないため、コードをシンプルにするため、 for (int price : prices) { でループを回したほうが良いと思います。

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.

手癖でインデックスを使うfor文で回してしまっていたので今後使い分けます。

if (prices[i] < minPrice) {
minPrice = prices[i];
} else {
maxProfit = max(maxProfit, prices[i] - minPrice);
}
}

return maxProfit;
}
};
18 changes: 18 additions & 0 deletions 121/4.cpp
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;
}
};