-
Notifications
You must be signed in to change notification settings - Fork 0
1011. Capacity To Ship Packages Within D Days #46
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
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| def can_ship_capacity_in_days(capacity: int, days: int) -> bool: | ||
| total_weight = 0 | ||
| i = 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.
iはちょっとわかりにくい気がします
weight_index
とかどうでしょうか
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.
for i in range(len(array)): と書かれたら、i が array の添字だと思うと思うんですね。これを長々と説明的に書かれてもありがたくないのです。あと、この for を抜けたらもう忘れて良いやつだというのが暗に含意されています。
これが、for first_zero_index in だったら途中で見つけて break するから抜けても覚えている必要があるものでしょう。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
下でも書いていますが、weights で回すほうが多分素直なんでしょうね。
| total_weight = 0 | ||
| days_required = 1 | ||
| for weight in weights: | ||
| if total_weight + weight > capacity: |
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.
このコードの場合、lowの初期値が1でも動きますか?
たぶん、どこかに
if weight>capacity:
return False
を入れておいたほうが堅牢だと思います。
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.
この場合だと日付を求めているので、返すのはinfとかsys.maxintですかね
例外という選択肢もありますが、受け取る側がやや手間ではありますね
| if not weights: | ||
| return 0 | ||
|
|
||
| def calculate_shipping_days(capacity: 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.
calculate良いですが、わたしならget_days_requiredとかにするかもしれません
| total_weight = 0 | ||
| days_required = 1 | ||
| for weight in weights: | ||
| if weight > capacity: |
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.
そうですよね、bisectでrangeをつかうならこの例外処理が必要になると思います
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.
lo= を書いているので、なくても動くかとは思いますが、しかし書いたほうが読みやすいでしょうね。後々変更が入ったときにバグの原因にもなりそうですし。
| - 実態を表すよう無理に情報を詰め込むと拗らせるので、そういう場合はなるべくシンプルにして、必要ならコメント補足を心掛ける | ||
| - https://github.com/YukiMichishita/LeetCode/pull/10 | ||
| - bisect_left/right | ||
| - bisect_rightを勘違いしていた(等号は含まない) |
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.
bisect_right は、「昇順になるように挿入するとしたら可能な範囲の中で一番右の位置」を見つけてきますね。
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| def can_ship_capacity_in_days(capacity: int, days: int) -> bool: | ||
| total_weight = 0 | ||
| i = 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.
for i in range(len(array)): と書かれたら、i が array の添字だと思うと思うんですね。これを長々と説明的に書かれてもありがたくないのです。あと、この for を抜けたらもう忘れて良いやつだというのが暗に含意されています。
これが、for first_zero_index in だったら途中で見つけて break するから抜けても覚えている必要があるものでしょう。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.fcs3httrll4l
下でも書いていますが、weights で回すほうが多分素直なんでしょうね。
| total_weight = 0 | ||
| days_required = 1 | ||
| for weight in weights: | ||
| if weight > capacity: |
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.
lo= を書いているので、なくても動くかとは思いますが、しかし書いたほうが読みやすいでしょうね。後々変更が入ったときにバグの原因にもなりそうですし。
| if not weights: | ||
| return 0 | ||
|
|
||
| def calculate_shipping_days(capacity: 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.
好みかもですが、日数の情報は、キャパの判断材料にしかすぎないので脇役で使いたいという気持ちがありますね。
https://github.com/olsen-blue/Arai60/pull/44/files#diff-4e146417f14c744a10f851601f26cd2cb17b420ff966720e568f6f5679aa475eR120-R123
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.
同意です。
他の方が書かれていた、所用日数を計算する、という選択肢が頭になかったので書いてみました。
| for weight in weights: | ||
| if total_weight + weight > capacity: | ||
| total_weight = 0 | ||
| days_required += 1 | ||
|
|
||
| total_weight += weight |
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 の中に計算式があるのは、なんか変な感じがしました。
ここの書き方は下記のように改善できる気がしました。
https://github.com/hayashi-ay/leetcode/pull/55/files#diff-4e146417f14c744a10f851601f26cd2cb17b420ff966720e568f6f5679aa475eR97-R101
total_weight は思考の中心にあるコップみたいなものだと思うんですよね。もうちょい上のほうに書いてあげたいです。
https://github.com/saagchicken/coding_practice/pull/10/files#r1995442298
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.
私もそのイメージでした。
空のコップ(total_weight)を用意して、水(weight)を入れていく、ただ溢れそうになったら(if total_weight + weight > capacity:)新しいコップを用意して(total_weight = 0)、使用したコップの数を記録しておくカウンターを1進める(days_required += 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.
この辺りでも同じ感情を持ちました
Fuminiton/LeetCode#30 (comment)
Fuminiton/LeetCode#30 (comment)
|
|
||
| total_weight += weight | ||
|
|
||
| return days_required <= days |
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.
私はdays requiredがdaysを超えた時点で弾きたくなります
| while days: | ||
| if total_weight + weights[i] > capacity: | ||
| total_weight = 0 | ||
| days -= 1 | ||
| continue | ||
|
|
||
| total_weight += weights[i] | ||
| i += 1 | ||
| if i == len(weights): | ||
| return True |
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.
こう書くなら、個人的にはこう変形した方がわかりやすい気がしています
while days:
if i == len(weights):
return True
if total_weight + weights[i] > capacity:
total_weight = 0
days -= 1
total_weight += weights[i]
i += 1
return False
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/