Skip to content

Conversation

@hroc135
Copy link
Owner

@hroc135 hroc135 commented Feb 26, 2025

- すなわち、32bitマシンにおいてint型はint32と同じ大きさで、
同様にuint型はuint32と同じになり、
表現できる最大値がそれぞれ2^31-1と2^32-1で後者の方が大きいからuint(low+high)でoverflowしない
- 計算結果は`(low+high) / 2`と同じ(overflowしなければ)??
Copy link

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

Copy link
Owner Author

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)よりローコストで計算できるというメリットあるのですね

Copy link

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

Comment on lines +154 to +157
capacities := make([]int, weightsSum-maxWeight+1)
for i := range capacities {
capacities[i] = maxWeight + i
}
Copy link

Choose a reason for hiding this comment

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

ここあんまり格好良くないですが、仕方がないですね。

Copy link
Owner Author

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 {
Copy link

@Mike0121 Mike0121 Mar 2, 2025

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
}
Copy link

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")

Choose a reason for hiding this comment

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

以前こんなコメントを頂いたので参考に貼っておきます。
TORUS0818/leetcode#44 (comment)

Copy link
Owner Author

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

Choose a reason for hiding this comment

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

wもweightだと思うので、名前を変えてもいいかもしれません。
total_weightとかどうでしょうか。

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.

5 participants