-
Notifications
You must be signed in to change notification settings - Fork 0
Create 1011.Capacity To Ship Packages Within D Days.md #44
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
fuga-98
wants to merge
1
commit 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
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,159 @@ | ||
| # 進め方 | ||
|
|
||
| Step1 : 問題を解く。 | ||
|
|
||
| Step2 : 他の人のPRを参照し、コメントする。 | ||
|
|
||
| Step3 : 3回続けてエラーが出ないように書く。ドキュメントを参照する。 | ||
|
|
||
| # 実践 | ||
|
|
||
| ## Step1 | ||
|
|
||
| ### 思考ログ | ||
|
|
||
| やり方がまったくわからない。 | ||
|
|
||
| あ、累積和を取りそう。 | ||
|
|
||
| weights = [1,2,3,1,1], days = 4として | ||
|
|
||
| prefix = [1,3,5,6,7] | ||
|
|
||
| ここを二分探索するのかなあ、わからん。 | ||
|
|
||
| ## Step2 | ||
|
|
||
| ### 同じ問題を解いた人のプルリクを見る | ||
|
|
||
| https://github.com/olsen-blue/Arai60/pull/44/files | ||
|
|
||
| - 競プロではよくある問題らしい。 | ||
| - あー最小のweightを二分探索するのか。 | ||
| - なんか凄いアルゴリズムがあると思っていた。 | ||
| - こういう使い方もできるんですね。FFFTTTTとなるものの最小を求めることには使えそうですね。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| def can_shipped(max_weight): | ||
| total_days = 0 | ||
| i = 0 | ||
| while i < len(weights): | ||
| total_days += 1 | ||
| package = 0 | ||
| while i < len(weights): | ||
| if weights[i] + package > max_weight: | ||
| break | ||
| package += weights[i] | ||
| i += 1 | ||
| if total_days > days: | ||
| return False | ||
| return True | ||
|
|
||
| lo = 0 | ||
| hi = sum(weights) + 1 | ||
| while lo < hi: | ||
| mid = (lo + hi) // 2 | ||
| if can_shipped(mid): | ||
| hi = mid | ||
| continue | ||
| lo = mid + 1 | ||
| return hi | ||
| ``` | ||
|
|
||
| これもbisectを用いて求められるらしい。面白い。 | ||
|
|
||
| https://github.com/hroc135/leetcode/pull/42 | ||
|
|
||
| - Pythonはintの大きさ気にしなくて良いの幸せな気持ちになりました。 | ||
| - dead codeは避けるhttps://github.com/TORUS0818/leetcode/pull/44#discussion_r1996702695 | ||
|
|
||
| https://github.com/nittoco/leetcode/pull/47/files | ||
|
|
||
| - Daysが0以下の処理はどうするか | ||
| - rangeの実装を読んでいる。 | ||
| - https://github.com/python/cpython/blob/9ad0c7b0f14c5fcda6bfae6692c88abb95502d38/Objects/rangeobject.c#L318 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| def can_ship(capacity): | ||
| i = 0 | ||
| total_days = 0 | ||
| while i < len(weights): | ||
| loading = 0 | ||
| total_days += 1 | ||
| while i < len(weights): | ||
| if weights[i] + loading > capacity: | ||
| break | ||
| loading += weights[i] | ||
| i += 1 | ||
| if total_days > days: | ||
| return False | ||
| return True | ||
| return bisect_left(range(sum(weights) + 1), True, key=can_ship) | ||
| ``` | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| if not days: | ||
| return -1 | ||
|
|
||
| def can_ship(capacity): | ||
| needed_days = 1 | ||
| loading = 0 | ||
| for weight in weights: | ||
| if weight > capacity: | ||
| return False | ||
| if loading + weight <= capacity: | ||
| loading += weight | ||
| continue | ||
| needed_days += 1 | ||
| if needed_days > days: | ||
| return False | ||
| loading = weight | ||
| return True | ||
|
|
||
| return min(weights) + bisect_left(range(min(weights), sum(weights) + 1), True, key=can_ship) | ||
| ``` | ||
|
|
||
| - bisect_left(range(max(weights), sum(weights) + 1), True, key=is_loadable_capacity) | ||
| - 部分的な区間の中の、相対的なindexを返してしまうので、+ max(weights)が必要になった。割と仕様を知らないといけないのが微妙そう。 | ||
|
|
||
| ## Step3 | ||
|
|
||
| ### 3回連続で再現できるようになる | ||
|
|
||
| return bisect_left(range(sum(weights)), True, key=can_ship) | ||
|
|
||
| range(3)は0,1,2を返すので、なんでsum(weights)でよいかわからなかったが、見つからなかったらsum(weights)を返すのか。range(3)でFFFなら3を返しますね。 | ||
|
|
||
| 最小のTが帰ってきてほしいのでsum + 1したいですね。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def shipWithinDays(self, weights: List[int], days: int) -> int: | ||
| assert days | ||
|
|
||
| def can_ship(capacity): | ||
| loading = 0 | ||
| required_days = 1 | ||
| for w in weights: | ||
| if w > capacity: | ||
|
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. bisect_left の lo を max(weights) にしているのでここに引っかかることはないと思います。
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. レビューありがとうございます。 |
||
| return False | ||
| loading += w | ||
| if loading <= capacity: | ||
| continue | ||
| required_days += 1 | ||
| loading = w | ||
| if required_days > days: | ||
| return False | ||
| return True | ||
|
|
||
| return bisect_left(range(sum(weights) + 1), True,lo=max(weights), key=can_ship) | ||
| ``` | ||
|
|
||
| if w > capacity: | ||
| return False はなくても良いが、can_shipに何が来ても良いように。 | ||
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.
個人的には、assert int には不等号を付けたいですね。