-
Notifications
You must be signed in to change notification settings - Fork 0
1011. Capacity To Ship Packages Within D Days #42
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
Open
tom4649
wants to merge
3
commits into
main
Choose a base branch
from
1011.Capacity-To-Ship-Packages-Within-D-Days
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # 1011. Capacity To Ship Packages Within D Days | ||
|
|
||
| https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/ | ||
|
|
||
| TODO: 二分探索を自分で書く | ||
|
|
||
| - capacityを指定すればそれが可能かはO(N)で判定できる | ||
| - 上限をどのように定めるかが問題。単純にsum(weights)としたとき220ms、minを使ったとき119msとなった。 | ||
| - 問題のWeight上限を使う方法は正攻法なのだろうか | ||
| - 自力で解法を思いついたが、二分探索のヒントがあったから | ||
|
|
||
| n = len(weights), M = maximum_valueとすると | ||
| - 時間計算量: O(n*log(M)) | ||
| - 空間計算量: O(1) (入力は含めない) | ||
|
|
||
|
|
||
| コメント集に該当箇所はない | ||
|
|
||
| https://github.com/naoto-iwase/leetcode/pull/27/changes | ||
|
|
||
| capacityの下限はmax(weights)とできるのか | ||
| - これを反映してsol2.pyとしたが、149msとなり逆に遅くなった | ||
| - maxをとるO(n)の操作に余分な時間がかかるケースが多いのだろう。二分探索は途中で打ち切られるのでそれほど重くない。 | ||
|
|
||
|
|
||
| https://github.com/mamo3gr/arai60/blob/1011_capacity-to-ship-packages-within-d-days/1011_capacity-to-ship-packages-within-d-days/memo.md | ||
|
|
||
| 色々なコードがまとめられている | ||
|
|
||
| https://github.com/h1rosaka/arai60/pull/46/changes | ||
|
|
||
| 上でも述べられているけど、 `for _ in range(days):` で書くこともできるのは思いつかなかった |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import bisect | ||
|
|
||
| MAXIMUM_WEIGHT = 500 | ||
|
|
||
|
|
||
| class Solution: | ||
| def shipWithinDays(self, weights: list[int], days: int) -> int: | ||
| def can_ship(capacity): | ||
| used_days = 1 | ||
| shipped_weight_on_current_day = 0 | ||
| for weight in weights: | ||
| if shipped_weight_on_current_day + weight <= capacity: | ||
| shipped_weight_on_current_day += weight | ||
| continue | ||
| used_days += 1 | ||
| if used_days > days or weight > capacity: | ||
| return False | ||
| shipped_weight_on_current_day = weight | ||
| return True | ||
|
|
||
| maximum_value = min(sum(weights), MAXIMUM_WEIGHT * (len(weights) // days + 1)) | ||
|
|
||
| return bisect.bisect_left(range(1, maximum_value), True, key=can_ship) + 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def shipWithinDays(self, weights: list[int], days: int) -> int: | ||
| def can_ship(capacity): | ||
| used_days = 1 | ||
| shipped_weight_on_current_day = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的にはここの変数名はtotal_weightとかで充分だと思いました。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かに長いですね。 |
||
| for weight in weights: | ||
| if shipped_weight_on_current_day + weight <= capacity: | ||
| shipped_weight_on_current_day += weight | ||
| continue | ||
| used_days += 1 | ||
| if used_days > days or weight > capacity: | ||
| return False | ||
| shipped_weight_on_current_day = weight | ||
| return True | ||
|
|
||
| minimum_value = max(weights) | ||
| maximum_value = sum(weights) | ||
|
|
||
| return ( | ||
| bisect.bisect_left(range(minimum_value, maximum_value), True, key=can_ship) | ||
| + minimum_value | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def shipWithinDays(self, weights: list[int], days: int) -> int: | ||
| def can_ship(capacity): | ||
| used_days = 1 | ||
| total_weight = 0 | ||
| for weight in weights: | ||
| if total_weight + weight <= capacity: | ||
| total_weight += weight | ||
| continue | ||
| used_days += 1 | ||
| if used_days > days or weight > capacity: | ||
| return False | ||
| total_weight = weight | ||
| return True | ||
|
|
||
| minimum_value = max(weights) | ||
| maximum_value = sum(weights) | ||
|
|
||
| return ( | ||
| bisect.bisect_left(range(minimum_value, maximum_value), True, key=can_ship) | ||
| + minimum_value | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
すでに 35.search insert position で練習済みかと思いますが、二分探索を自力で実装するのがこの問題の期待値なのかなと思いました。
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.
なるほど。もう一度復習する予定なので、そのときに書き直してみます。