-
Notifications
You must be signed in to change notification settings - Fork 0
1011 capacity to ship packages within d days medium #23
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
base: main
Are you sure you want to change the base?
1011 capacity to ship packages within d days medium #23
Conversation
nanae772
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
お疲れ様です。全体的に丁寧に考察されていてコードも非常に読みやすく良いコードだなと思いました!
| left = 0 | ||
| right = sum(weights) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この問題に適した名前にしてもよいのかなと思いました。
以下はやや冗長な名前付けですが例えば
- left -> max_capacity_cannot_ship
- right -> min_capacity_can_ship
とかにするとreturnしている変数が求めたいものと一致しているなと分かりやすいかなと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。動的計画法ではさすがに dp等と書くのは避けているのですが、二分探索では left , right を私は基本的に使ってしまっています。が、問題ごとに適切な名前をつけてみるように努力してみます。ご指摘ありがとうございます。
今回の場合、自分なら、rightは常にcan_shipでTrueになる、つまり荷物を運ぶのに十分な容量を表し、left は逆に不十分な容量を表すので、
left->insufficient_capacityright->sufficient_capacity
などがわかりやすいかなと感じました。
(二分探索の途中では必ずしもmax/minであるわけではないので、不変条件を表す変数名にしてみました)
| - 二分探索を自分で書く前に、まずは`bisect`モジュールを使って通るかを見るのが良い。 | ||
| - `bisect`モジュールでは`key`引数が使えるので、`can_ship`関数をそのまま渡せる。 | ||
| - ソート済みのリストを用意する必要があるが、`range`で用意できる。`bool`の配列ではソートすると`False`が先に来るので、`True`が初めて出現するインデックスを求めることになる。 | ||
| - `bisect`に渡せる配列は`SupportsLenAndGetItem`プロトコルを満たしていれば良いので、`__len__`と`__getitem__`を実装したクラスならば良い。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このあたり勉強になりました!
|
|
||
| left = 0 | ||
| right = sum(weights) | ||
| while right - left > 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
読みやすいです。個人的には右側が閉区間で左側が開区間という取り方はあまり見慣れないですが、私の経験が少ないだけかもしれません。
問題へのリンク
Capacity To Ship Packages Within D Days
言語
Python
自分の解法
step1
(left, right]の範囲で二分探索するパターン。can_ship_within_days関数内でweight > capacityのチェックをしているが、はじめは抜けていた上、なかなか気づきづらい。leftの初期値を0にしていたが、max(weights)などにすればこのチェックは不要になる。その場合、二分探索も変わる。if max(weights) > capacity: return Falseとすべき。weights = [100,]のとき、can_ship_within_days(2, 50)はFalseを返すべきだが、上記のコードではTrueを返してしまう。これは2日に分ければ大きな荷物も運べるということになってしまうため。cargo_weightは複数の荷物をまとめた重さを表す変数としているが、current_weightの方がわかりやすいと思ったcan_ship_within_daysという名前の関数でdaysが引数にないのは違和感があるので、daysを引数にした。が、can_shipにリネームしてdaysをキャプチャする形にしたほうが自然だと思った。weightsの要素数をN、weightsの要素の和をSとすると、O(N log(S))O(N)step2
can_ship_within_days関数をcan_shipにリネームstep3
step4 (FB)
別解・模範解答
bisectモジュールを使う方法bisectモジュールを使って通るかを見るのが良い。bisectモジュールではkey引数が使えるので、can_ship関数をそのまま渡せる。rangeで用意できる。boolの配列ではソートするとFalseが先に来るので、Trueが初めて出現するインデックスを求めることになる。次に解く問題の予告