Skip to content

35. Search Insert Position#41

Open
dxxsxsxkx wants to merge 1 commit into322_coin_changefrom
35_search_insert_position
Open

35. Search Insert Position#41
dxxsxsxkx wants to merge 1 commit into322_coin_changefrom
35_search_insert_position

Conversation

@dxxsxsxkx
Copy link
Copy Markdown
Owner


// 閉区間の場合、mid が begin と一致すると区間が縮まらない
if (target <= nums[mid]) {
answer = mid; // true の頭を取る
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここでif (begin == end) {return end + 1}としてしまってもいいと思いました。

int begin = 0;
int end = nums.size() - 1;

int answer = nums.size();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良い代替案が思いつくわけではないのですが、answerという名前の変数が頻繁に変わるのは少し違和感があるかもしれません。

}
}

// 7. 最後はどっちでも良い
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

end の位置には target より小さい値があるため、 begin でないとまずいように思いました。


## Step 1

時間計算量 $O(N \log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[nits]

Suggested change
時間計算量 $O(N \log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。
時間計算量 $O(\log N)$ の制約があるので二分探索でやることが求められているんだろう。`step1.cpp` に書いた。

// 閉区間の場合、mid が begin と一致すると区間が縮まらない
if (target <= nums[mid]) {
answer = mid; // true の頭を取る
end = 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.

end より右は常にtrue (nums[i] >= target) ということですね。

answer = mid; // true の頭を取る
end = mid - 1; // さらに左側を探索
} else {
begin = mid + 1; // 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.

begin より左側は常にfalseということですね。


int answer = nums.size();

// 一意に定まったら終了、つまり begin > end となったら終了
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

答えが [begin, end] の中にある状態を保つ

なのに

begin > end となったら終了

というのは違和感がありました。言葉通り捉えると、解が存在しないように思います。

@mamo3gr
Copy link
Copy Markdown

mamo3gr commented Mar 10, 2026

探索の3パターンを書き分けてみるとより理解が深まると思います。
ちなみに、私は ng (nums[i] < target) と ok (target <= nums[i]) の境界を求める、と考えるのが一番分かりやすかったです。

#include <vector>

class Solution {
public:
    int searchInsert(std::vector<int>& nums, int target) {
        int ng = -1;
        int ok = nums.size();

        while (ok - ng > 1) {
            int mid = ng + (ok - ng) / 2;
            if (nums[mid] >= target) {
                ok = mid;
            } else {
                ng = mid;
            }
        }

        return ok;
    }
};

参考:https://qiita.com/drken/items/97e37dd6143e33a64c8c

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