-
Notifications
You must be signed in to change notification settings - Fork 0
add: Capacity To Ship Packages Within D Days #26
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| ```java | ||
| // Using binary search to find the minimum cap that we can ship the packages | ||
| // Min(left) will be the max value in the array, Max(right) will be sum of the values(when we need to ship everything in a day) | ||
| // During the search, if we can ship it meaning the smallest cap in left ~ mid | ||
| // if we can't, meaning we need to search between mid + 1 ~ right | ||
| // Time complexity: O(NlogM) -> N: number of weights, M: sum of values - max value | ||
| // Space complexity: O(1) | ||
| // Time spend: 08:34 | ||
| class Solution { | ||
| public int shipWithinDays(int[] weights, int days) { | ||
| int l = -1; | ||
| int r = 0; | ||
| for (int w : weights) { | ||
| if (w > l) { | ||
| l = w; | ||
| } | ||
| r += w; | ||
| } | ||
|
|
||
| while (l < r) { | ||
| int mid = (l + r) / 2; | ||
|
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. この問題の場合は問題とならないと思うのですが、 l + r が int の範囲を超えてオーバーフローするのを防ぐため、
|
||
| if (canShip(weights, mid, days)) { | ||
| r = mid; | ||
| } else { | ||
| l = mid + 1; | ||
| } | ||
| } | ||
|
|
||
| return l; | ||
| } | ||
|
|
||
| public boolean canShip(int[] weights, int cap, int days) { | ||
|
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. capacity でいい気がします。 |
||
| int day = 1; | ||
| int temp = 0; | ||
| for (int w : weights) { | ||
| if (temp + w <= cap) { | ||
| temp += w; | ||
| continue; | ||
| } | ||
| day++; | ||
| if (day > days) { | ||
| return false; | ||
| } | ||
| temp = w; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| ```java | ||
| // Time spend: 03:21 | ||
| class Solution { | ||
| public int shipWithinDays(int[] weights, int days) { | ||
| int l = -1; | ||
| int r = 0; | ||
| for (int weight : weights) { | ||
| if (weight > l) { | ||
| l = weight; | ||
| } | ||
| r += weight; | ||
| } | ||
|
|
||
| while (l < r) { | ||
| int mid = (l + r) / 2; | ||
| if (canShip(weights, mid, days)) { | ||
| r = mid; | ||
| } else { | ||
| l = mid + 1; | ||
| } | ||
|
Comment on lines
+16
to
+20
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. 個人的に、こちらの方が Step3 よりも二分探索だ~と分かりやすかったです。 |
||
| } | ||
|
|
||
| return l; | ||
| } | ||
|
|
||
| public boolean canShip(int[] weights, int cap, int days) { | ||
| int daysNeed = 1; | ||
| int currWeight = 0; | ||
| for (int weight : weights) { | ||
| if (currWeight + weight > cap) { | ||
| daysNeed++; | ||
| if (daysNeed > days) { | ||
| return false; | ||
| } | ||
| currWeight = 0; | ||
| } | ||
| currWeight += weight; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| ```java | ||
| // Time spend: 03:11 | ||
| class Solution { | ||
| public int shipWithinDays(int[] weights, int days) { | ||
| int l = -1; | ||
| int r = 0; | ||
| for (int weight : weights) { | ||
| if (weight > l) { | ||
| l = weight; | ||
| } | ||
| r += weight; | ||
| } | ||
|
|
||
| while (l < r) { | ||
| int mid = (l + r) / 2; | ||
| if (canShip(weights, mid, days)) { | ||
|
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. @goto-untrapped さんも指摘されている点ですが、 l と r の対称性を示すため、 continue せず if else としたほうが良いと思います。 |
||
| r = mid; | ||
| continue; | ||
| } | ||
| l = mid + 1; | ||
| } | ||
|
|
||
| return l; | ||
| } | ||
|
|
||
| public boolean canShip(int[] weights, int cap, int days) { | ||
| int daysNeed = 1; | ||
| int currWeight = 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. 変数名に単語の省略形を使用すると、読むにあたり認知負荷が高くなるように思います。チーム内で合意形成が得られているもの以外については、フルスペルで書くことをお勧めいたします。 |
||
| for (int weight : weights) { | ||
| if (currWeight + weight > cap) { | ||
| currWeight = 0; | ||
| daysNeed++; | ||
| if (daysNeed > days) { | ||
| return false; | ||
| } | ||
| } | ||
| currWeight += weight; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```java | ||
| // Time spend: 02:47 | ||
| class Solution { | ||
| public int shipWithinDays(int[] weights, int days) { | ||
| int l = -1; | ||
| int r = 0; | ||
| for (int w : weights) { | ||
| l = Math.max(w, l); | ||
| r += w; | ||
| } | ||
|
|
||
| while (l < r) { | ||
| int mid = (l + r) / 2; | ||
| if (canShip(weights, days, mid)) { | ||
| r = mid; | ||
| continue; | ||
| } | ||
| l = mid + 1; | ||
| } | ||
|
|
||
| return l; | ||
| } | ||
|
|
||
| public boolean canShip(int[] weights, int days, int cap) { | ||
| int daysNeed = 1; | ||
| int currWeight = 0; | ||
| for (int w : weights) { | ||
| if (currWeight + w > cap) { | ||
| currWeight = 0; | ||
| daysNeed++; | ||
| if (daysNeed > days) { | ||
| return false; | ||
| } | ||
| } | ||
| currWeight += w; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```java | ||
| // Time spend: 02:21 | ||
| class Solution { | ||
| public int shipWithinDays(int[] weights, int days) { | ||
| int l = -1; | ||
|
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. 個人的には |
||
| int r = 0; | ||
| for (int w : weights) { | ||
| l = Math.max(l, w); | ||
| r += w; | ||
| } | ||
|
|
||
| while (l < r) { | ||
| int mid = (l + r) / 2; | ||
| if (canShip(weights, days, mid)) { | ||
| r = mid; | ||
| continue; | ||
| } | ||
| l = mid + 1; | ||
| } | ||
|
|
||
| return l; | ||
| } | ||
|
|
||
| public boolean canShip(int[] weights, int days, int cap) { | ||
| int daysNeed = 1; | ||
| int currWeight = 0; | ||
| for (int w : weights) { | ||
| if (currWeight + w > cap) { | ||
| currWeight = 0; | ||
| daysNeed++; | ||
| if (daysNeed > days) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+117
to
+119
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で余計なloopを回さないという意図はあると思うですが、僅かな差だと思うので可読性があがるほうが良いかなと |
||
| } | ||
| currWeight += w; | ||
| } | ||
|
|
||
| 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.
これでよいと思いますが、二分探索はいくつか書き方があるので、レビューワーの方が、リンクを探し出してまとめてくれることを期待します。
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://discord.com/channels/1084280443945353267/1252267683731345438/1253724345369624596
Find Minimum in Rotated Sorted Array Exzrgs/LeetCode#11 (comment)
↑これとかまとまってるかなと思います
https://discord.com/channels/1084280443945353267/1192736784354918470/1235596462084067369
300. Longest Increasing Subsequence YukiMichishita/LeetCode#7 (comment)