-
Notifications
You must be signed in to change notification settings - Fork 0
1011. Capacity To Ship Packages Within D Days #39
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
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
1011
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,59 @@ | ||
| # step1 何も見ずに解く | ||
| まず左から荷物を分類していって、すべてのパターンを計算することを思いついたが指数関数時間かかるので他の方法にする。 | ||
| 絶対一発で荷物の範囲を決めることはできないので何回も調査することになる。 | ||
| 求めたいのは、指定された日数で荷物を全て移動できる最小の最大積載量になる。 | ||
| この範囲は、weightsの最大値とwights全ての合計の間にあり、下限は1で上限が500 * 5 * 10^4 = 2.5 * 10^7になる。 | ||
| 下限から愚直に+1して最小値を求めるのは効率が悪いので二分探索を使う。 | ||
| ある最大積載量で指定された日数で荷物を運び切れるかはweightsの長さをLとすると時間計算量O(L)で計算できる。 | ||
| よって、全ての荷物の合計をWとすると、時間計算量はO(L * log(W))で、Lは最大で5 * 10^4、Wも最大でおおよそ2.5 * 10^7なので1秒以内に間に合う | ||
|
|
||
| - 求めたいもの(target) | ||
| - 荷物を全て移動できる最小の最大積載量 | ||
| - 範囲 | ||
| - left | ||
| - leftより左はtargetを満たさない(指定された日数で荷物を運びきれない) | ||
| - right | ||
| - rightより右はtargetを満たさない(targetより大きい) | ||
| - 終了条件 | ||
| - left == rightとなるとき | ||
| - middleは以下で分類する | ||
| - middleがtargetより左にある | ||
| - leftより左はtargetを満たさないのでleft = middle + 1に更新できる | ||
| - middleがtargetあるいはtargetより右にある | ||
| - rightより右はtargetを満たさないのでright = middleに更新できる | ||
| - middleの出し方 | ||
| - 常に区間を1狭めるためにはmiddle = (left + right) / 2 | ||
|
|
||
|
|
||
| ```ruby | ||
| # @param {Integer[]} weights | ||
| # @param {Integer} days | ||
| # @return {Integer} | ||
| def ship_within_days(weights, days) | ||
| return 0 if weights.size.zero? | ||
|
|
||
| can_ship_within_days = lambda do |max_load_capacity| | ||
| package = 0 | ||
| day_count = 1 | ||
| weights.each do |weight| | ||
| package += weight | ||
| next if package <= max_load_capacity | ||
| day_count += 1 | ||
| return false if day_count > days | ||
| package = weight | ||
| end | ||
| true | ||
| end | ||
| left = weights.max | ||
| right = weights.sum | ||
| while left < right | ||
| middle = (left + right) / 2 | ||
| if can_ship_within_days.call(middle) | ||
| right = middle | ||
| else | ||
| left = middle + 1 | ||
| end | ||
| end | ||
| left | ||
| end | ||
| ``` | ||
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,45 @@ | ||
| # step2 他の方の解答を見る | ||
| https://github.com/Satorien/LeetCode/pull/44 | ||
|
|
||
| step1のコードはdaysが0以下の時にweighsの合計を出してしまっていたので、-1を返すことにする。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer[]} weights | ||
| # @param {Integer} days | ||
| # @return {Integer} | ||
| def ship_within_days(weights, days) | ||
| return 0 if weights.size.zero? | ||
| return -1 if days <= 0 # 運べない | ||
|
|
||
| can_ship_within_days = lambda do |max_load_capacity| | ||
| package = 0 | ||
| day_count = 1 | ||
| weights.each do |weight| | ||
| package += weight | ||
| next if package <= max_load_capacity | ||
| day_count += 1 | ||
| return false if day_count > days | ||
| package = weight | ||
| end | ||
| true | ||
| end | ||
| left = weights.max | ||
| right = weights.sum | ||
| while left < right | ||
| middle = (left + right) / 2 | ||
| if can_ship_within_days.call(middle) | ||
| right = middle | ||
| else | ||
| left = middle + 1 | ||
| end | ||
| end | ||
| left | ||
| end | ||
| ``` | ||
|
|
||
| https://github.com/ruby/ruby/blob/5257e1298c4dc4e854eaa0a9fe5e6dc5c1495c91/array.c#L3543 | ||
| Rubyの`Array#bsearch`はオーバーフロー回避のために | ||
| ``` | ||
| mid = low + ((high - low) / 2); | ||
| ``` | ||
| と計算していて面白かった。 |
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,34 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| def ship_within_days(weights, days) | ||
| return 0 if weights.size.zero? | ||
| return -1 if days <= 0 # 運べない | ||
|
|
||
| can_ship_within_days = lambda do |max_load_capacity| | ||
| package_weight = 0 | ||
| day_count = 1 | ||
| weights.each do |weight| | ||
| package_weight += weight | ||
| next if package_weight <= max_load_capacity | ||
|
|
||
| day_count += 1 | ||
| return false if day_count > days | ||
| package_weight = weight | ||
| end | ||
| true | ||
| end | ||
|
|
||
| left = weights.max | ||
| right = weights.sum | ||
| while left < right | ||
| middle = (left + right) / 2 | ||
| if can_ship_within_days.call(middle) | ||
| right = middle | ||
| else | ||
| left = middle + 1 | ||
| end | ||
| end | ||
| left | ||
| end | ||
|
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. 良いと思いました。 |
||
| ``` | ||
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 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
特に問題ないです。
二分探索のところに目を通すくらいです。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.c15qprmvxkc2