Skip to content

121. Best Time to Buy and Sell Stock#35

Open
5103246 wants to merge 1 commit intomainfrom
121-best-time-to-buy-and-sell-stock
Open

121. Best Time to Buy and Sell Stock#35
5103246 wants to merge 1 commit intomainfrom
121-best-time-to-buy-and-sell-stock

Conversation

@5103246
Copy link
Copy Markdown
Owner

@5103246 5103246 commented Dec 2, 2025

Copy link
Copy Markdown

@naoto-iwase naoto-iwase left a comment

Choose a reason for hiding this comment

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

お疲れさまです。

読みやすく、コメントから他の選択肢も見えていそうだと感じました。
特に違和感を持った場所はなかったです。

- C++だと複雑になって読みにくいかな
- 関数型についてよくわからなかったのでGPTに聞いてみたが、変数の再代入をしないや状態が変化しない特徴があるらしい。(参照透過性)
- 状態が変化しないので共有データを変更しない→ロックが不要→並列処理に強い などの特徴がある(https://ja.wikipedia.org/wiki/%E9%96%A2%E6%95%B0%E5%9E%8B%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0)
- 関数型の考えや機能は他の言語で取り込まれている。(C++のlamda, rangesなど)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

関数型の見方を知っておく・意識することで、手続き型で書くコードもなんか綺麗になっていく気がしています(?)

return 0;
}

vector<int> prefix(prices.size());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

prefix だと接頭辞というニュアンスを感じます。最低価格が格納されるため、 min_prices あたりが良いと思います。

return 0;
}

vector<int> prefix(prices.size());
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::back_inserter を使用することで、あらかじめメモリを確保せずに書く方法もあります。

vector<int> prefix;
partial_sum(prices.begin(), prices.end(), back_inserter(prefix),
            [](int acc, int x){ return min(acc, x); });

ただ、今回の場合は読みやすさはあまり変わらず、あらかじめメモリを確保しておいたほうが、 push_back() 中のメモリの再確保が発生しないため、あらかじめメモリを確保してく書き方のほうが良いかもしれません。

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.

コメントありがとうございます。
back_inserterを使用した書き方もあるんですね。
参考になります。

- prices[0]を使うなら、ループはi = 1から始めた方が自然かもしれない?
- そこを変えても読みやすくなるわけでもないので、変えなくてもいいか。

## Step3
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

シンプルで読みやすいと思いました。

- https://github.com/nanae772/leetcode-arai60/pull/36/files
- buy_priceをmath.infから始めている人もいた。
- prices[0]を使うなら、ループはi = 1から始めた方が自然かもしれない?
- そこを変えても読みやすくなるわけでもないので、変えなくてもいいか。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pythonでは整数に上限がないので番兵をmath.infにするものをよく見ます
c++だと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.

あとループは変えないほうが私は読みやすいと思いました。

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.

5 participants