Skip to content

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

Merged
merged 2 commits into from
Mar 18, 2024
Merged

121. Best Time to Buy and Sell Stock #6

merged 2 commits into from
Mar 18, 2024

Conversation

colorbox
Copy link
Owner

@colorbox colorbox commented Mar 17, 2024

int maxProfit(vector<int>& prices) {
int minPrice = INT_MAX;
int maxProfit = 0;
for(int i = 0; i < prices.size(); i++) {
Copy link

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
Owner Author

Choose a reason for hiding this comment

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

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

class Solution {
public:
int maxProfit(vector<int>& prices) {
int minPrice = INT_MAX;
Copy link

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
Owner Author

Choose a reason for hiding this comment

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

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

public:
int maxProfit(vector<int>& prices) {
int minPrice = INT_MAX;
int maxProfit = 0;
Copy link

Choose a reason for hiding this comment

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

関数名と同じ変数名を使うと、名前を隠してしまい、思わぬバグを入れる場合があります。できれば異なる名前を付けたほうが良いと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

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

@colorbox colorbox merged commit db1163b into main Mar 18, 2024
@colorbox colorbox deleted the 121 branch March 18, 2024 16:50
@colorbox colorbox changed the title Add solved codes for 121. Best Time to Buy and Sell Stock Add solved codes for `121. Best Time to Buy and Sell Stock May 13, 2024
@colorbox colorbox changed the title Add solved codes for `121. Best Time to Buy and Sell Stock 121. Best Time to Buy and Sell Stock May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants