Skip to content

solved: 122.Best Time to Buy and Sell Stock II#38

Open
t9a-dev wants to merge 1 commit intomainfrom
122.Best-Time-to-Buy-and-Sell-Stock-II
Open

solved: 122.Best Time to Buy and Sell Stock II#38
t9a-dev wants to merge 1 commit intomainfrom
122.Best-Time-to-Buy-and-Sell-Stock-II

Conversation

@t9a-dev
Copy link
Owner

@t9a-dev t9a-dev commented Dec 1, 2025

問題: 122. Best Time to Buy and Sell Stock II
次に解く問題: 139. Word Break
ファイルの構成: ./src/bin/<各ステップ>.rs

Comment on lines +29 to +31
let Some((mut holding_price, remaining_prices)) = prices.split_first() else {
return 0;
};

Choose a reason for hiding this comment

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

自分がこの書き方するならあえて下のように分けたいですね。

Suggested change
let Some((mut holding_price, remaining_prices)) = prices.split_first() else {
return 0;
};
let Some((first_price, remaining_prices)) = prices.split_first() else {
return 0;
};
let mut holding_price = *first_price;

Comment on lines +35 to +41
if price < holding_price {
holding_price = price;
continue;
}

total_profit += price - holding_price;
holding_price = price;

Choose a reason for hiding this comment

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

holding_priceの更新はif節の成立によらず行うのでわざわざ別々に書かれていることに違和感を持ちました。

Suggested change
if price < holding_price {
holding_price = price;
continue;
}
total_profit += price - holding_price;
holding_price = price;
if price < holding_price {
total_profit += price - holding_price;
}
holding_price = price;

Choose a reason for hiding this comment

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

あと、ここのif分岐をなくして常にprice - holding_priceと0のうち大きい方をtotal_profitに加えると書く方法もあると思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

holding_priceの更新はif節の成立によらず行うのでわざわざ別々に書かれていることに違和感を持ちました。

重複しているコードなので違和感を感じるのがソフトウェアエンジニアとしての普通の反応だと思いました。
提案頂いたコードはifの条件式を反転して動作することを確認しました。

            if holding_price < price {
                total_profit += price - holding_price;
            }

            holding_price = price;

あと、ここのif分岐をなくして常にprice - holding_priceと0のうち大きい方をtotal_profitに加えると書く方法もあると思います。

なるほど、こんな感じになると理解しました。
ありがとうございました。

        let min_profit = 0;

        for price in remaining_prices {
            total_profit += min_profit.max(price - holding_price);
            holding_price = price;
        }

Choose a reason for hiding this comment

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

すみません、不等号の向き逆でした.

ご提案で違和感ないです。(0はハードコードしちゃってもいいかもですが、そちらだとより意図が明確で良いですね。)

continue;
}

total_profit += price - holding_price;

Choose a reason for hiding this comment

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

また、holding_priceという命名にも違和感があります。
holdする(つまり現金から株にする)かどうかは未来のpriceに依存して初めて決まるので、単にprevious_priceの方が自然だと思います。

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