-
Notifications
You must be signed in to change notification settings - Fork 0
1011. Capacity To Ship Packages Within D Days #47
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?
Conversation
|
.md になってないです! |
| ```python | ||
| class Solution: | ||
| def load_items_until_fill( | ||
| self, num_loaded_so_far: int, capacity: int, weights: List[int] |
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.
引数の num_loaded_so_far は caluculate_needed_days_to_ship 関数から呼ばれる際に必ず0を入れられるものなので、引数にしなくても良いと思います。
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.
失礼しました、num_loaded_so_far = num_loaded で更新されていますね。
| num_loaded_so_far = 0 | ||
| while num_loaded_so_far < len(weights): | ||
| num_loaded = self.load_items_until_fill(num_loaded_so_far, capacity, weights) | ||
| if num_loaded == num_loaded_so_far: |
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.
num_loaded と num_loaded_so_far の時系列の違いを表現できていない気がします。num_loaded と new_num_loaded だと変ですかね。
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.
これ迷ったんですよね。
今の自分のだと確かにわかりにくいなとは思います。
すみません!直しました |
| if not weights: | ||
| return 0 | ||
| capacity_small = 1 | ||
| capacity_large = sum(weights) + 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_large の意味は、この capacity ならば運べるということなので、days >= 1 を確認すれば sum(weights) が代入できるのではないでしょうか。
| if num_loaded == num_loaded_so_far: | ||
| return sys.maxsize |
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.
これは少しテクニカルすぎるという気がしています。
たとえば、num_loaded, had_space_to_load = load_items_until_fill( などと、載せる余裕があったかを別に返す方が好ましいと思います。
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.
なるほど、ちょうどその選択肢と迷ってましたが、自分が書いたのはあまり一般的ではないんですね。
ありがとうございます。
| num_loaded += 1 | ||
| return num_loaded | ||
|
|
||
| def caluculate_needed_days_to_ship(self, capacity: int, weights: List[int]) -> int: |
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.
日数の情報はキャパの判断材料にしかすぎないので脇役で使いたい気持ちがあります。
TORUS0818/leetcode#46 (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.
キャパシティの最適解が存在しうる数直線を考える、二分探索で抽出する数直線上のある点(とりあえずmiddle)のキャパシティ値で運べるかが知りたくなる、運べるキャパかどうかbool値でチェックする関数が必要だと感じる、みたいな流れが個人的にはしっくりきます。
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.
うーむ、難しいですね。
個人的には、キャパを固定したあと、その日数を数えたいと思うきもちが強いのと、
割と汎用的に他のところでも使えそうな関数なので、作っておきたいきもちがあります
| return days | ||
|
|
||
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| capacity_small = 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_small = max(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.
これは考えたんですけど、迷いますね。(読み手に、なぜその数字にしたのかの推理がやや必要な気もする)
問題文URL: https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/
A conveyor belt has packages that must be shipped from one port to another within days days.
The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.