This commit is contained in:
krahets
2026-08-18 03:09:22 +08:00
parent d8c34d88b5
commit ba5285949b
108 changed files with 237 additions and 229 deletions
@@ -26,7 +26,7 @@ The problem requires inserting `target` to the left of equal elements, which mea
To analyze this further, consider the binary search process: when `nums[m] < target`, $i$ moves, meaning that pointer $i$ is approaching elements greater than or equal to `target`. Similarly, pointer $j$ is always approaching elements less than or equal to `target`.
Therefore, when the binary search ends, $i$ must point to the first element greater than `target`, and $j$ must point to the first element less than `target`. **It follows that when the array does not contain `target`, the insertion index is $i$**. The code is shown below:
Therefore, when the binary search ends, $i$ must point to the first element greater than `target`, and $j$ must point to the rightmost element less than `target`. **It follows that when the array does not contain `target`, the insertion index is $i$**. The code is shown below:
=== "Python"
@@ -339,7 +339,7 @@ Now consider extending the binary search code. As shown in Figure 10-6, the over
- When `nums[m] < target` or `nums[m] > target`, it means `target` has not been found yet, so use the standard interval-shrinking operation of binary search to **move pointers $i$ and $j$ closer to `target`**.
- When `nums[m] == target`, it means elements less than `target` are in the interval $[i, m - 1]$, so use $j = m - 1$ to shrink the interval, thereby **moving pointer $j$ closer to elements less than `target`**.
After the loop completes, $i$ points to the leftmost `target`, and $j$ points to the first element less than `target`, **so index $i$ is the insertion point**.
After the loop completes, $i$ points to the leftmost `target`, and $j$ points to the rightmost element less than `target`, **so index $i$ is the insertion point**.
=== "<1>"
![Steps for binary search insertion point of duplicate elements](binary_search_insertion.assets/binary_search_insertion_step1.png){ class="animation-figure" }
@@ -384,7 +384,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
elif nums[m] > target:
j = m - 1 # target is in the interval [i, m-1]
else:
j = m - 1 # The first element less than target is in the interval [i, m-1]
j = m - 1 # The rightmost element less than target is in the interval [i, m-1]
# Return insertion point i
return i
```
@@ -402,7 +402,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -423,7 +423,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -444,7 +444,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -469,7 +469,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
// target is in the interval [i, m-1]
j = m - 1
} else {
// The first element less than target is in the interval [i, m-1]
// The rightmost element less than target is in the interval [i, m-1]
j = m - 1
}
}
@@ -493,7 +493,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if nums[m] > target {
j = m - 1 // target is in the interval [i, m-1]
} else {
j = m - 1 // The first element less than target is in the interval [i, m-1]
j = m - 1 // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -515,7 +515,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -537,7 +537,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -558,7 +558,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -579,7 +579,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if nums[m as usize] > target {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -600,7 +600,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
} else {
j = m - 1; // The first element less than target is in the interval [i, m-1]
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -622,7 +622,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
} else if (nums[m] > target) {
j = m - 1 // target is in the interval [i, m-1]
} else {
j = m - 1 // The first element less than target is in the interval [i, m-1]
j = m - 1 // The rightmost element less than target is in the interval [i, m-1]
}
}
// Return insertion point i
@@ -647,7 +647,7 @@ Even so, we can still keep the conditional branches expanded, as the logic is cl
elsif nums[m] > target
j = m - 1 # target is in the interval [i, m-1]
else
j = m - 1 # The first element less than target is in the interval [i, m-1]
j = m - 1 # The rightmost element less than target is in the interval [i, m-1]
end
end