mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-28 19:07:14 +00:00
build
This commit is contained in:
@@ -476,7 +476,7 @@ Note that the returned insertion point is $i$, so we need to subtract $1$ from i
|
||||
|
||||
### 2. Converting to Element Search
|
||||
|
||||
We know that when the array does not contain `target`, $i$ and $j$ will eventually point to the first elements greater than and less than `target`, respectively.
|
||||
We know that when the array does not contain `target`, $i$ and $j$ will eventually point to the first element greater than `target` and the rightmost element less than `target`, respectively.
|
||||
|
||||
Therefore, as shown in Figure 10-8, we can construct an element that does not exist in the array to find the left and right boundaries.
|
||||
|
||||
|
||||
@@ -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>"
|
||||
{ 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user