binary search ๆฆๅฟตๆฏ, ่ฆๆพๅทฆๅ้จ้ๆฏๅณๅ้จ๏ผ
ไธๅๆณ use left <= left
ไบๅๆณ use left < left
1. ๆพๆจก็ณๅผ first or last occurrence, ไบๅๆณ
see leetcode 34
int left = 0;
int right = nums.length - 1;
// find first occurrence
while (left < right) {
int mid = left + (right - left)/2;
if (nums[mid] < target) { // ๅพๅณๅ้จๆพ
left = mid + 1;
} else { // ๅพๅทฆๅ้จๆพ
right = mid;
}
}
return left; // right
// find last occurrence
while (left < right) {
int mid = left + (right - left + 1)/2; // notice! +1
if (nums[mid] > target) { // ๅพๅทฆๅ้จๆพ
right = mid - 1;
} else { // ๅพๅณๅ้จๆพ
left = mid;
}
}
return left;
2. ๆพ็ขบๅๅผ, ไธๅๆณ, template,
left, right = 0, len(array) - 1
while left <= right:
// (left + right) >>> 1 is best!
mid = (left + right) >>> 1 // left + (right - left) / 2
if array[mid] == target:
# find the target!!
break or return result
elif array[mid] < target:
left = mid + 1
else:
right = mid - 1
when do you use while (start < end) , when do you use while (start <= end) ?
You use while (start <= end)
if you are returning the match from inside the loop.
You use while (start < end)
if you want to exit out of the loop first, and then use the result of start
or end
to return the match.
ๅฏไปฅๅ่้็ฏ็ๆ่ทฏ๏ผ
https://leetcode-cn.com/problems/search-insert-position/solution/te-bie-hao-yong-de-er-fen-cha-fa-fa-mo-ban-python-/
3. ๆพๆ่ฟไผผๅผ (closet to ...)
int left = 0;
int right = nums.length - 1;
// find first occurrence
while (left + 1 < right) {
int mid = (left + right) >>> 1;
if (nums[mid] < target) { // ๅพๅณๅ้จๆพ
left = mid;
} else { // ๅพๅทฆๅ้จๆพ
right = mid;
}
}
if (nums[right] < target) { // right < target , target ๆๅคง
return right;
} else if (nums[left] > target) { // target < left , target ๆๅฐ
return left;
} else { // left < target < right , target ไปๆผไธญ้
return (target - nums[left] < nums[right] - target) ? left : right;
}
find peak (162)
if (nums[mid] < nums[mid+1]) {
left = mid + 1;
} else {
right = mid;
}
Rotated
while (left <= right) ...
// duplicate ๅค้ๅๆขไปถ
if (nums[start] == nums[mid] && nums[mid] == nums[end]) {
start++;
end--;
} else if (nums[start] <= nums[mid]) {
if (nums[start] <= target && target <= nums[mid]) { // in left
end = mid - 1;
} else {
start = mid + 1;
}
} else {
if (nums[mid] <= target && target <= nums[end]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
Mini in Rotated
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
// duplicate
if (nums[mid] > nums[right]) {
left = mid + 1;
} else if (nums[mid] < nums[right]) {
right = mid;
} else { // nums[mid] == nums[right] .=> add this
right--;
}
ไบๅๆณ
ไบๅๆฅๆพ tips
decrease and conquer template
ๅๅง binary search ๅฏซๆณๅๆไธ้จๅ
ๅฆไธ็จฎๅๆณ
decrease and conquer, ๆๆ้ค mid ๆ่, ๅ็บๅ
ฉ้จไปฝ
while(left < right) ้ๆจฃๅฏซ้ๅบๆ, left == right, ๆไปฅๅ left or right ้ฝๅฏไปฅ
ๆ นๆ้็ๆถ็ธฎ,ไธญ้ๅผ่ฆ่ๆ
ฎ ๅชๆๅ
ฉๅๅ
็ด ๆ, [0, 1] => mid = (0+1)/2 = 0, ๆฐธ้ ๅไธๅฐๅณไธญไฝๆธ
ไธๅๆดๅฏไปฅ่งฃๆฑบ
ๆ นๆไป้บผๆๅ็ๆณไธๆฏ่งฃ, ๅฏซif, ifๅฐ else ่ช็ถๅฐ
็ฌฌไบ่งฃ, ๅ ็บๅฏไปฅๆๅ
ฅๅจๆๅพ, ๆไปฅๅฏไปฅไธ็จ็นๅฅๅคๆท, ๆไปฅ ๅณ้็ ๅฏไปฅๆนๆ len
็ฌฌไธ่งฃ, ๅ ็บ็ๅฐ็ฎๆจๅ
็ด , ๅฐฑ้ๆ้ก็ฎ่ฆ็, ๆไปฅๅฏไปฅๅคไธๅ == ็ๆไปถๅคๆท็ดๆฅ่ฟๅ็ญๆก