-
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
base: main
Are you sure you want to change the base?
Conversation
| - すなわち、32bitマシンにおいてint型はint32と同じ大きさで、 | ||
| 同様にuint型はuint32と同じになり、 | ||
| 表現できる最大値がそれぞれ2^31-1と2^32-1で後者の方が大きいからuint(low+high)でoverflowしない | ||
| - 計算結果は`(low+high) / 2`と同じ(overflowしなければ)?? |
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.
Go では mod 2^32 で同じ値を指すのでそうですね。言語によっては違うことが起きたりしますが。
https://go.dev/ref/spec#Integer_overflow
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.
リンクの共有ありがとうございます。middleを求める方法として、
(1) middle := left + (right-left)/2
(2) middle := int(uint(left+right) >> 1)
の二つの方法があってなんでわかりにくいやり方かつ「mod 2^32 で同じ値を指す」という条件を前提とする方法を使っているのだろうと思ったのですが、割り算を行う(1)よりローコストで計算できるというメリットあるのですね
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.
たとえば、x86 だとシフト命令には、logical (unsigned) と arithmetic (signed) があって、arithmetic は右シフトによってできる空きに最上位の符号ビットが埋められます。cast はコンパイルすればコストはないので一命令でできています。
https://en.wikibooks.org/wiki/X86_Assembly/Shift_and_Rotate
https://docs.oracle.com/cd/E19620-01/805-4693/instructionset-27/index.html
| capacities := make([]int, weightsSum-maxWeight+1) | ||
| for i := range capacities { | ||
| capacities[i] = maxWeight + i | ||
| } |
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.
pythonだとrangeで一発ですね
| return low | ||
| } | ||
|
|
||
| func isShipableWithinCapacity(weights []int, days int, capacity int) bool { |
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.
細かいですが、Shippableですね。
すみません、step2のコメントにありました。
| sum += v | ||
| } | ||
| return sum | ||
| } |
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.
ヘルパー関数なのと、スコープが短いので好みかもしれませんが、
もう少し変数名をs -> nums, v -> val くらいの長さにしても読みやすいかと思いました。
| return capacity | ||
| } | ||
| } | ||
| panic("unreacheable") |
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.
以前こんなコメントを頂いたので参考に貼っておきます。
TORUS0818/leetcode#44 (comment)
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.
ありがとうございます。
一般的に、dead code は避けるものです。
これ刺さりました。
| day++ | ||
| weight = 0 | ||
| } | ||
| weight += w |
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.
wもweightだと思うので、名前を変えてもいいかもしれません。
total_weightとかどうでしょうか。
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/description/