Skip to content

Conversation

hroc135
Copy link
Owner

@hroc135 hroc135 commented Feb 25, 2025

#### 2c
- 2bと同じロジックを構造体を使って実装
- numsの尻より大きいか -> targetとの大小比較 の順に調べる
- https://github.com/Yoshiki-Iwasa/Arai60/pull/36/files#r1712955053
Copy link

Choose a reason for hiding this comment

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

見直したら cmp いらなくて

        def priority(x):
            return (x <= nums[-1], target <= x)

これでよかったです。ただ、Go で書くと少し面倒ですね。

@Mike0121
Copy link

Mike0121 commented Mar 2, 2025

良いと思いました。

tail := nums[len(nums)-1]
isTargetLargerThanTail := target > tail
for left < right {
middle := left + (right-left)/2

Choose a reason for hiding this comment

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

ここで

if nums[middle] == target {
	return middle
}

のようにするのはどうでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。こちらでの指摘も含めて違う書き方ができますね。今回の問題ではnumsに重複要素が含まれないことが保証されていますが、そうでない場合はtargetに一致する要素のうちランダムなものが返ることに注意ですね。

} else {
right = middle
}
continue

Choose a reason for hiding this comment

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

ここは好みかと思うのですが、私はif-elseにした方が好きです。
ちなみに上でアーリーリターンしておけばright = middle - 1とできると思います。

としてnumsを[false,...,false,true,...,true]のような配列とみなした時、
一番左のtrueの位置を探す問題と捉える
- nums=[6,0,2,4], target=2 -> [false,false,true,true]
- nums=[6,0,2,4], target=1 -> [false,false,false,false]
Copy link

Choose a reason for hiding this comment

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

target が含まれないことは、全部調べないと分からないのではないでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

おっしゃる通りですね。
T/F を target 以上の nums の要素のうち最小のものとそれより右のもの、と定義した方がいいですかね。

nums=[6,0,2,4], target=2 -> [false, false, true, true]
nums=[6,0,2,4], target=1 -> [false, false, true, true]
nums=[6,0,2,4], target=5 -> [true, true, true, true]

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.

4 participants