Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Apr 17, 2025

問題文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.

@hroc135
Copy link

hroc135 commented Apr 18, 2025

.md になってないです!

```python
class Solution:
def load_items_until_fill(
self, num_loaded_so_far: int, capacity: int, weights: List[int]
Copy link

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を入れられるものなので、引数にしなくても良いと思います。

Copy link

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:
Copy link

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 だと変ですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

これ迷ったんですよね。
今の自分のだと確かにわかりにくいなとは思います。

@nittoco
Copy link
Owner Author

nittoco commented Apr 18, 2025

.md になってないです!

すみません!直しました

if not weights:
return 0
capacity_small = 1
capacity_large = sum(weights) + 1
Copy link

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) が代入できるのではないでしょうか。

Comment on lines +140 to +141
if num_loaded == num_loaded_so_far:
return sys.maxsize
Copy link

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( などと、載せる余裕があったかを別に返す方が好ましいと思います。

Copy link
Owner Author

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:

Choose a reason for hiding this comment

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

日数の情報はキャパの判断材料にしかすぎないので脇役で使いたい気持ちがあります。
TORUS0818/leetcode#46 (comment)

Copy link

@olsen-blue olsen-blue Apr 20, 2025

Choose a reason for hiding this comment

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

キャパシティの最適解が存在しうる数直線を考える、二分探索で抽出する数直線上のある点(とりあえずmiddle)のキャパシティ値で運べるかが知りたくなる、運べるキャパかどうかbool値でチェックする関数が必要だと感じる、みたいな流れが個人的にはしっくりきます。

Copy link
Owner Author

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

Choose a reason for hiding this comment

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

capacity_small = max(weights)でもいいのかなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

これは考えたんですけど、迷いますね。(読み手に、なぜその数字にしたのかの推理がやや必要な気もする)

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.

6 participants