111111- [py powerful- ultimate- binary- search- template- solved- many- problems](https:// leetcode.com/ discuss/ general- discussion/ 786126 / python- powerful- ultimate- binary- search- template- solved- many- problems) : TODO : add above to cheatsheet
112112
113113# ### 0-2-0) Binary search
114+
115+ - Key:
116+ ```
117+ 分析二分查找的一個技巧是:不要出現 else,而是把所有情況用 else if 寫清楚,這樣可以清楚地展現所有細節。本文都會使用 else if,旨在講清楚,讀者理解後可自行簡化。
118+ ```
119+
120+ - NOTE!!!
121+ - `left = 0, right = nums.len - 1`
122+ - `while left <= right`
123+ - `left = mid + 1`
124+ - `right = mid - 1`
125+
126+ - https://labuladong.online/algo/essential-technique/binary-search-framework/#%E9%9B%B6%E3%80%81%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%E6%A1%86%E6%9E%B6
127+
128+ ```java
129+ // java
130+ int binarySearch(int[] nums, int target) {
131+ int left = 0;
132+ // 注意
133+ int right = nums.length - 1;
134+
135+ // NOTE !!! <=, need to search when "left == right" idx as well
136+ while(left <= right) {
137+ int mid = left + (right - left) / 2;
138+ if(nums[mid] == target)
139+ return mid;
140+ else if (nums[mid] < target)
141+ // 注意
142+ left = mid + 1;
143+ else if (nums[mid] > target)
144+ // 注意
145+ right = mid - 1;
146+ }
147+ return -1;
148+ }
149+ ```
150+
114151``` python
115152# python
116153# basic
@@ -136,6 +173,69 @@ def binary_search(nums, target):
136173```
137174
138175#### 0-2-1) Binary search on ` LEFT ` boundary
176+
177+ - https://labuladong.online/algo/essential-technique/binary-search-framework/#%E8%83%BD%E5%90%A6%E7%BB%9F%E4%B8%80%E6%88%90%E4%B8%A4%E7%AB%AF%E9%83%BD%E9%97%AD%E7%9A%84%E6%90%9C%E7%B4%A2%E5%8C%BA%E9%97%B4
178+
179+ ``` java
180+
181+ /**
182+ * 2 difference between regular binary search
183+ *
184+ * 1. else if (nums[mid] == target) {
185+ * // 收缩右侧边界
186+ * right = mid - 1;
187+ * }
188+ *
189+ *
190+ *
191+ * 2. validate result step
192+ *
193+ * if (left < 0 || left >= nums.length) {
194+ * return -1;
195+ * }
196+ * // 判断一下 nums[left] 是不是 target
197+ * return nums[left] == target ? left : -1;
198+ *
199+ *
200+ */
201+
202+ // java
203+ int left_bound(int [] nums, int target){
204+ int left = 0 ;
205+ int right = nums. length - 1 ;
206+ // NOTE : WE ALWALYS USE CLOSED boundary (for logic unifying)
207+ // -> e.g. while l <= r
208+ // -> [l, r]
209+ while (left <= right){
210+ int mid = left + (right - mid) / 2 ;
211+ if (num[mid] < target){
212+ // 搜索区间变为 [mid+1, right]
213+ left = mid + 1 ;
214+ }else if (nums[mid] > target){
215+ // 搜索区间变为 [left, mid-1]
216+ right = mid - 1 ;
217+ }else if (nums[mid] == target){
218+ // DO NOT RETURN !!!, BUT REDUCE RIGHT BOUNDARY FOR FOCUSING ON LEFT BOUNDARY
219+ // 收缩右侧边界
220+ right = mid - 1 ;
221+ }
222+ }
223+ // // finally check if it will be OUT OF LEFT boundary
224+ // if (left >= nums.length || nums[left] != target){
225+ // return -1;
226+ // return left;
227+
228+
229+ // 判断 target 是否存在于 nums 中
230+ // 如果越界,target 肯定不存在,返回 -1
231+ if (left < 0 || left >= nums. length) {
232+ return - 1 ;
233+ }
234+ // 判断一下 nums[left] 是不是 target
235+ return nums[left] == target ? left : - 1 ;
236+ }
237+ ```
238+
139239``` python
140240# python
141241def binary_search_left_boundary (nums , target ):
@@ -163,33 +263,6 @@ def binary_search_left_boundary(nums, target):
163263 return l
164264```
165265
166- ``` java
167- // java
168- int left_bound(int [] nums, int target){
169- int left = 0 ;
170- int right = nums. length - 1 ;
171- // NOTE : WE ALWALYS USE CLOSED boundary (for logic unifying)
172- // -> e.g. while l <= r
173- // -> [l, r]
174- while (left <= right){
175- int mid = left + (right - mid) / 2 ;
176- if (num[mid] < target){
177- left = mid + 1 ;
178- }else if (nums[mid] > target){
179- right = mid - 1 ;
180- }else if (nums[mid] == target){
181- // DO NOT RETURN !!!, BUT REDUCE RIGHT BOUNDARY FOR FOCUSING ON LEFT BOUNDARY
182- right = mid - 1 ;
183- }
184- }
185- // finally check if it will be OUT OF LEFT boundary
186- if (left >= nums. length || nums[left] != target){
187- return - 1 ;
188- return left;
189- }
190- }
191- ```
192-
193266#### 0-2-2) Binary search on ` RIGHT ` boundary
194267``` python
195268# python
0 commit comments