Conversation
| total_weight += weight | ||
| return days_with_capacity + 1 <= days | ||
|
|
||
| return bisect_left(range(sum(weights) + 1), True, key=can_be_shipped) |
There was a problem hiding this comment.
おお、range がランダムアクセスできて、空間計算量が O(1) なの知りませんでした。よく考えるとそりゃそうですね。
せっかくなので、lo=max(weights) を与えておくと探索範囲も同じになりますね。
こういう無駄なことしてました。
https://discord.com/channels/1084280443945353267/1233295449985650688/1246144715653513318
|
|
||
| def can_be_shipped(capacity: int) -> bool: | ||
| total_weight = 0 | ||
| days_with_capacity = 0 |
There was a problem hiding this comment.
Can be shipped in N days with this capacity という気分でしょうが、days required か単に days かそのあたりじゃないかと思いますね。
There was a problem hiding this comment.
Can be shipped in N days with this capacity という気分でしょうが
そんな感じでした。たしかにdays_requiredの方が良いですね
| total_weight += weight | ||
| return days_with_capacity + 1 <= days | ||
|
|
||
| low = 0 |
There was a problem hiding this comment.
1st のように low = max(weights) とすると
if weight > capacity:
return False
が省略でき、コードが少しシンプルになると思います。
|
良いと思います |
| if total_weight + weight > capacity: | ||
| total_weight = 0 | ||
| days_with_capacity += 1 | ||
| if days_with_capacity > days: |
There was a problem hiding this comment.
(遅レスですが…)days_with_capacityを0から始めているので、このearly returnは一個ずれてますかね?だから最後にreturn Trueとできないで、days_with_capacity + 1 <= daysと再確認する必要があるかなと。
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/