Skip to content

add: Capacity To Ship Packages Within D Days#26

Open
rossy0213 wants to merge 1 commit intomainfrom
arai60-44
Open

add: Capacity To Ship Packages Within D Days#26
rossy0213 wants to merge 1 commit intomainfrom
arai60-44

Conversation

@rossy0213
Copy link
Copy Markdown
Owner

```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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これでよいと思いますが、二分探索はいくつか書き方があるので、レビューワーの方が、リンクを探し出してまとめてくれることを期待します。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return l;
}

public boolean canShip(int[] weights, int cap, int days) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

capacity でいい気がします。

Comment on lines +16 to +20
if (canShip(weights, mid, days)) {
r = mid;
} else {
l = mid + 1;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的に、こちらの方が Step3 よりも二分探索だ~と分かりやすかったです。

// Time spend: 02:21
class Solution {
public int shipWithinDays(int[] weights, int days) {
int l = -1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には l = 0 での初期化の方が自然な気がしました。

```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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +117 to +119
if (daysNeed > days) {
return false;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数末尾でreturn daysNeed <= daysとするほうが条件分岐が少なくて読みやすいと思いました

早期returnで余計なloopを回さないという意図はあると思うですが、僅かな差だと思うので可読性があがるほうが良いかなと

}

while (l < r) {
int mid = (l + r) / 2;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この問題の場合は問題とならないと思うのですが、 l + r が int の範囲を超えてオーバーフローするのを防ぐため、 int mid = l + (r - l) / 2; と書いておいたほうが良いと思います。
https://ja.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%8E%A2%E7%B4%A2#%E5%AE%9F%E8%A3%85%E4%B8%8A%E3%81%AE%E9%96%93%E9%81%95%E3%81%84

よくある間違いの一つは、上記のC言語のコードで imin + (imax - imin) / 2 を (imax + imin) / 2 としてしまう事である。(imax + imin) / 2 では、imax + imin が int の値の上限 (INT_MAX) を超えて不正な値になってしまう可能性がある。(imax + imin が INT_MAX を超える可能性が全くないと保証できる場合は問題ない。)


public boolean canShip(int[] weights, int cap, int days) {
int daysNeed = 1;
int currWeight = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数名に単語の省略形を使用すると、読むにあたり認知負荷が高くなるように思います。チーム内で合意形成が得られているもの以外については、フルスペルで書くことをお勧めいたします。


while (l < r) {
int mid = (l + r) / 2;
if (canShip(weights, mid, days)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goto-untrapped さんも指摘されている点ですが、 l と r の対称性を示すため、 continue せず if else としたほうが良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants