Repository navigation
Expand file tree
/
Copy pathbinarySearchPatterns.ts
More file actions
599 lines (518 loc) · 19.8 KB
/
Copy pathbinarySearchPatterns.ts
File metadata and controls
599 lines (518 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* ============================================================================
* BINARY SEARCH PATTERNS
* ============================================================================
*
* PATTERN:
* - **Halve a MONOTONIC search space each step.** Anything that lets you ask
* "is the answer left or right of mid?" with one O(1)/O(k) check drops a
* linear O(n) scan to O(log n).
*
* THE BRUTE-FORCE → OPTIMIZED STORY:
* - Brute force is always the linear scan: check every candidate → O(n).
* - Sortedness (or any monotonic predicate) means one comparison at `mid`
* eliminates HALF the remaining candidates — that pruning is the pattern.
*
* THE THREE SUB-FAMILIES (know which one you're in!):
* 1. Exact match binarySearch, searchMatrix (matrix as flat array)
* 2. Boundary (lower/upper bound) searchInsert, searchRange
* → loop keeps `left` as "first index that could still work"; beware
* off-by-ones — `left < right` vs `left <= right`, `mid` vs `mid + 1`.
* 3. Search-on-ANSWER minEatingSpeed, mySqrt, findDuplicateBinarySearch
* → the array isn't sorted; the FEASIBILITY of candidate answers is
* monotonic ("if speed k works, k+1 works"), so binary search the
* answer range and test feasibility at each mid. Also: rotated-array
* `search` — find the sorted half, decide which side, recurse.
*
* RECOGNITION CUES:
* - "sorted array" → sub-family 1/2. "minimum/maximum X such that CONDITION"
* → sub-family 3, even with no sorted array in sight.
*
* REAL-WORLD ANALOGIES:
* - Version bisection (git bisect): first bad commit = lower bound.
* - Capacity planning: minimum instance size that satisfies the load test.
*
* Time: O(log n) per search (× O(k) per feasibility check in family 3).
* Space: O(1) iterative.
* ============================================================================
*/
/**
* 1. Binary Search (LeetCode 704)
* Given an array of integers nums which is sorted in ascending order, and an integer target,
* write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
*/
export function binarySearch(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
/**
* 2. Search Insert Position (LeetCode 35)
* Given a sorted array of distinct integers and a target value, return the index if the target is found.
* If not, return the index where it would be if it were inserted in order.
*/
export function searchInsert(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
/**
* 3. Find First and Last Position of Element in Sorted Array (LeetCode 34)
* Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
*/
export function searchRange(nums: number[], target: number): number[] {
const first = findFirst(nums, target);
if (first === -1) return [-1, -1];
const last = findLast(nums, target);
return [first, last];
}
function findFirst(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
let result = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
result = mid;
right = mid - 1; // Continue searching left
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
function findLast(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
let result = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
result = mid;
left = mid + 1; // Continue searching right
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
/**
* 4. Search in Rotated Sorted Array (LeetCode 33)
* There is an integer array nums sorted in ascending order (with distinct values).
* Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
*/
export function search(nums: number[], target: number): number {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
return mid;
}
// Check which half is sorted
if (nums[left] <= nums[mid]) {
// Left half is sorted
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
// Right half is sorted
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
/**
* 5. Find Minimum in Rotated Sorted Array (LeetCode 153)
* Given the sorted rotated array nums of unique elements, return the minimum element of this array.
*/
export function findMin(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[right]) {
// Minimum is in right half
left = mid + 1;
} else {
// Minimum is in left half (including mid)
right = mid;
}
}
return nums[left];
}
/**
* 6. Find Peak Element (LeetCode 162)
* A peak element is an element that is strictly greater than its neighbors.
* Given a 0-indexed integer array nums, find a peak element, and return its index.
*/
export function findPeakElement(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (nums[mid] > nums[mid + 1]) {
// Peak is in left half (including mid)
right = mid;
} else {
// Peak is in right half
left = mid + 1;
}
}
return left;
}
/**
* 7. Search a 2D Matrix (LeetCode 74)
* Write an efficient algorithm that searches for a value target in an m x n integer matrix.
* This matrix has the following properties:
* - Integers in each row are sorted from left to right.
* - The first integer of each row is greater than the last integer of the previous row.
*/
export function searchMatrix(matrix: number[][], target: number): boolean {
const m = matrix.length;
const n = matrix[0].length;
let left = 0;
let right = m * n - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const row = Math.floor(mid / n);
const col = mid % n;
if (matrix[row][col] === target) {
return true;
} else if (matrix[row][col] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
/**
* 8. Koko Eating Bananas (LeetCode 875)
* Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas.
* The guards have gone and will come back in h hours.
* Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile.
* Return the minimum integer k such that she can eat all the bananas within h hours.
*/
export function minEatingSpeed(piles: number[], h: number): number {
let left = 1;
let right = Math.max(...piles);
while (left < right) {
const mid = Math.floor((left + right) / 2);
if (canEatAll(piles, mid, h)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
function canEatAll(piles: number[], speed: number, hours: number): boolean {
let totalHours = 0;
for (const pile of piles) {
totalHours += Math.ceil(pile / speed);
}
return totalHours <= hours;
}
/**
* 9. Find the Duplicate Number (LeetCode 287) - Binary Search Approach
* Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
* There is only one repeated number in nums, return this repeated number.
*/
export function findDuplicateBinarySearch(nums: number[]): number {
let left = 1;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
let count = 0;
// Count numbers <= mid
for (const num of nums) {
if (num <= mid) {
count++;
}
}
if (count > mid) {
// Duplicate is in [1, mid]
right = mid;
} else {
// Duplicate is in [mid + 1, n]
left = mid + 1;
}
}
return left;
}
/**
* 10. Median of Two Sorted Arrays (LeetCode 4)
* Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
*/
export function findMedianSortedArrays(nums1: number[], nums2: number[]): number {
const totalLength = nums1.length + nums2.length;
const isEven = totalLength % 2 === 0;
if (isEven) {
const leftMedian = findKth(nums1, nums2, Math.floor(totalLength / 2));
const rightMedian = findKth(nums1, nums2, Math.floor(totalLength / 2) + 1);
return (leftMedian + rightMedian) / 2;
} else {
return findKth(nums1, nums2, Math.floor(totalLength / 2) + 1);
}
}
function findKth(nums1: number[], nums2: number[], k: number): number {
let left1 = 0;
let left2 = 0;
while (k > 1) {
const halfK = Math.floor(k / 2);
const mid1 = Math.min(left1 + halfK - 1, nums1.length - 1);
const mid2 = Math.min(left2 + halfK - 1, nums2.length - 1);
if (nums1[mid1] <= nums2[mid2]) {
k -= (mid1 - left1 + 1);
left1 = mid1 + 1;
} else {
k -= (mid2 - left2 + 1);
left2 = mid2 + 1;
}
}
if (left1 >= nums1.length) return nums2[left2];
if (left2 >= nums2.length) return nums1[left1];
return Math.min(nums1[left1], nums2[left2]);
}
/**
* 11. Sqrt(x) (LeetCode 69)
* Given a non-negative integer x, return the square root of x rounded down to the nearest integer.
*/
export function mySqrt(x: number): number {
if (x < 2) return x;
let left = 2;
let right = Math.floor(x / 2);
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const square = mid * mid;
if (square === x) {
return mid;
} else if (square < x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return right;
}
/**
* 12. Valid Perfect Square (LeetCode 367)
* Given a positive integer num, write a function which returns True if num is a perfect square else False.
*/
export function isPerfectSquare(num: number): boolean {
if (num < 1) return false;
let left = 1;
let right = num;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const square = mid * mid;
if (square === num) {
return true;
} else if (square < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
// ============================================================================
// Bonus: Recursive Binary Search Variants (for learning)
// ============================================================================
/**
* Alternative recursive style similar to the student's version:
* - Uses left/right bounds
* - Returns the VALUE when found, and -1 when not found
*
* Note: Returning the index is usually more flexible, but this variant
* is useful to see how the same left/right logic can return the value.
*
* @example
* binarySearchRecursiveValue([1, 2, 3, 4, 5], 4); // 4
* binarySearchRecursiveValue([1, 2, 3, 4, 5], 6); // -1
*/
export function binarySearchRecursiveValue(nums: number[], target: number): number {
return binarySearchRecursiveValueHelper(nums, target, 0, nums.length - 1);
}
function binarySearchRecursiveValueHelper(
nums: number[],
target: number,
left: number,
right: number
): number {
if (left > right) return -1;
const mid = Math.floor((left + right) / 2);
if (nums[mid] === target) {
return nums[mid];
}
if (target < nums[mid]) {
return binarySearchRecursiveValueHelper(nums, target, left, mid - 1);
}
return binarySearchRecursiveValueHelper(nums, target, mid + 1, right);
}
/**
* ============================================================================
* THE TWO BOUNDARY TEMPLATES — lowerBound / upperBound
* ============================================================================
* These two are not "extra problems". They are the HALF-OPEN template, and
* between them they answer almost every non-exact-match binary search you will
* ever be asked. Learning these two shapes is worth more than memorising ten
* individual problems.
*
* ============================================================================
* 🔑 CLOSED vs HALF-OPEN — the distinction behind nearly every off-by-one
* ============================================================================
* Every function above this point uses the CLOSED template. These two use the
* HALF-OPEN one. Mixing them is the single most common source of binary-search
* bugs, so keep the difference explicit:
*
* CLOSED [left, right] right = n - 1 while (left <= right) right = mid - 1
* → for EXACT MATCH. Exits with left > right, i.e. "not found".
*
* HALF-OPEN [left, right) right = n while (left < right) right = mid
* → for BOUNDARIES. Exits with left === right, and that meeting
* point IS the answer — it is never "not found".
*
* The half-open version never needs a -1 anywhere, which is exactly why it
* cannot overshoot: `right = mid` keeps mid inside the live range, so the
* range always shrinks but never skips the candidate.
*
* ============================================================================
* WHAT EACH ONE RETURNS
* ============================================================================
* lowerBound(nums, t) → the FIRST index whose value is >= t
* upperBound(nums, t) → the FIRST index whose value is > t
*
* ⭐ THE ONLY DIFFERENCE IS ONE CHARACTER: `<` becomes `<=`. That is the whole
* distinction, and being able to say so is the point of putting them together.
*
* And once you have both, three more answers are free:
* - count of t → upperBound - lowerBound
* - insert position → lowerBound (that IS LeetCode 35)
* - first/last index of t → lowerBound, and upperBound - 1 (LeetCode 34)
*
* ⚠️ ONE CAVEAT ON "lowerBound IS LeetCode 35": that holds because LC35
* guarantees DISTINCT values. `searchInsert` above uses the exact-match
* template, so on an array WITH duplicates it returns whichever matching index
* `mid` happens to land on — e.g. index 2 for target 3 in [1,3,3,3,5,8] —
* while `lowerBound` always returns the FIRST such index (1). Both are correct
* answers to "where is 3?"; only one answers "where does the run of 3s START?".
* Whenever you need a first or last occurrence, use the boundary templates.
*
* DRY-RUN on nums = [1, 3, 3, 3, 5, 8], target = 3:
*
* index : 0 1 2 3 4 5
* value : 1 3 3 3 5 8
* ↑ ↑ ↑
* | | └─ upperBound(3) = 4 (first value > 3)
* | └───────────── lowerBound(3) = 1 (first value >= 3)
* └───────────────── lowerBound(1) = 0
*
* count of 3 = 4 - 1 = 3 ✓
*
* And for a target that is absent, both collapse to the insertion point:
* lowerBound([1,3,5], 4) = 2 = upperBound([1,3,5], 4) → 4 is not present
* That equality is a clean membership test: `lo < n && nums[lo] === t`.
*
* @example
* lowerBound([1, 3, 3, 3, 5, 8], 3); // 1
* upperBound([1, 3, 3, 3, 5, 8], 3); // 4
* lowerBound([1, 3, 5], 4); // 2 (insertion point; 4 is absent)
* lowerBound([1, 2, 3], 99); // 3 (past the end — never -1)
*
* Time: O(log n). Space: O(1).
*/
export function lowerBound(nums: number[], target: number): number {
let left = 0;
let right = nums.length; // HALF-OPEN: one PAST the last index
while (left < right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] < target) {
left = mid + 1; // mid is too small — it cannot be the answer
} else {
right = mid; // mid might BE the answer, so keep it in range
}
}
return left; // left === right, and that meeting point is the boundary
}
/**
* ----------------------------------------------------------------------------
* upperBound — first index whose value is STRICTLY GREATER than target.
* ----------------------------------------------------------------------------
* Identical to `lowerBound` except for `<=` in place of `<`. See the shared
* doc block above for the closed-vs-half-open explanation and the dry-run.
*
* @example
* upperBound([1, 3, 3, 3, 5, 8], 3); // 4
* upperBound([1, 3, 3], 3); // 3 (all values <= 3)
* upperBound([1, 3, 3], 0); // 0 (every value is already > 0)
*
* Time: O(log n). Space: O(1).
*/
export function upperBound(nums: number[], target: number): number {
let left = 0;
let right = nums.length;
while (left < right) {
const mid = Math.floor(left + (right - left) / 2);
// The ONLY change from lowerBound: <= instead of <, so a value EQUAL
// to the target is also "too small" and gets skipped past.
if (nums[mid] <= target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
/**
* ----------------------------------------------------------------------------
* countOccurrences — how many times does target appear in a sorted array?
* ----------------------------------------------------------------------------
* The payoff for having both boundaries: the answer is the DISTANCE between
* them, in O(log n) rather than the O(n) a linear count would take.
*
* This is also the cleanest way to think about LeetCode 34 (First and Last
* Position): the range is [lowerBound, upperBound - 1], and it is empty
* exactly when the two bounds are equal.
*
* @example
* countOccurrences([1, 3, 3, 3, 5], 3); // 3
* countOccurrences([1, 3, 3, 3, 5], 4); // 0 (absent → bounds are equal)
* countOccurrences([], 1); // 0
*
* Time: O(log n) — two binary searches. Space: O(1).
*/
export function countOccurrences(nums: number[], target: number): number {
return upperBound(nums, target) - lowerBound(nums, target);
}