Skip to content

122. best time to buy and sell stock ii#39

Open
5ky7 wants to merge 4 commits intomainfrom
122.-Best-Time-to-Buy-and-Sell-Stock-II
Open

122. best time to buy and sell stock ii#39
5ky7 wants to merge 4 commits intomainfrom
122.-Best-Time-to-Buy-and-Sell-Stock-II

Conversation

@5ky7
Copy link
Copy Markdown
Owner

@5ky7 5ky7 commented Mar 10, 2026

5ky7 added 4 commits March 6, 2026 17:54
Added detailed explanations and multiple code implementations for the 'Best Time to Buy and Sell Stock II' problem.
int buy_price;
int total_profit = 0;
for (int i = 0; i < prices.size() - 1; ++i) {
if (has_stock) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continue をせず、 else 節を使ったほうがシンプルに感じました。

            if (has_stock) {
                if (prices[i] > prices[i + 1]) {
                    total_profit += prices[i] - buy_price;
                    has_stock = false;  
                }
            } else {
                if (prices[i] < prices[i + 1]) {
                    buy_price = prices[i];
                    has_stock = true;
                }
            }

@mamo3gr
Copy link
Copy Markdown

mamo3gr commented Mar 18, 2026

特に違和感ありませんでした 👍

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.

3 participants