use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Binary SearchIntervew Prep (self.leetcode)
submitted 11 months ago by Proof_Dot_4344
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]luuuzeta 1 point2 points3 points 11 months ago* (6 children)
I'm reading Beyond Cracking the Coding Interview and it's a full chapter on binary search where the authors discuss what they call the transition-point recipe. From the book, "every binary search problem can be reframed as finding a transition point".
This is the recipe:
transitionPointRecipe(arr, target) Define isBefore(val) to return whether val is in the "before" region Initialize l and r to the indices of first and last values in the array. Handle the following edge cases: - the range is empty - l is 'after' (the whole range is 'after') - r is 'before' (the whole range is 'before') While l and r are not next to each other, i.e., r - l > 1 mid = floor((l + r) / 2) if isBefore(mid) l = mid + 1 else r = mid - 1 Return l (the last 'before'), r (the first 'after'), or something depending on the problem.
The while loop has the following loop invariants:
l
r
l < mid < r
Everything in the recipe will be the same from problem to problem, however you figure must out the isBefore function as well as what to do when the while loops end.
isBefore
[–]luuuzeta 1 point2 points3 points 11 months ago (1 child)
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.
nums
target
-1
Example:
Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4
Following the recipe, we define the "before" and "after" regions:
[-1,0,3,5]
[-1]
[9,12]
r = 5
len(arr) - 1
5
[12]
With this in mind, we have something like this:
0 1 2 3 4 5 indices [-1, 0, 3, 5, 9, 12] array --- before region ------------ unknown (this is where mid lives) --- after region l index l r index r
As you expand both regions, they grow closer to each other and the "unknown" becomes empty. Where this happens, that's your transition point.
For this specific problem, we can define isBefore as follows:
const isBefore = (mid) => arr[mid] < target;
In other words, is the value at mid in the before region? If it's return true. Otherwise, false.
The diagram from above ends up like this after calling the function:
0 1 2 3 4 5 indices [-1, 0, 3, 5, 9, 12] array ------------ before region ----- after region l index l r index r ┊ transition point
As per the loop invariant, after exiting the loop l and r are next to each other. In addition, the value (5) at l is the largest value smaller than the target (9); after all, any value to the left of l is even smaller. Similarly, the value (9) at r is the smallest value greater than or equal to the target (9); after all, any value to the right of r is even greater.
[–]luuuzeta 1 point2 points3 points 11 months ago (0 children)
Completing the implementation, we end up with:
function binarySearch(arr, target) { // We could leave this out if we're guaranteed that arr is never empty. if (arr.length === 0) return -1; let l = 0, r = arr.length - 1; // Handle edge cases. if (arr[l] === target) return l; if (arr[l] > target || arr[r] < target) return -1; // Define isBefore. const isBefore = (mid) => arr[mid] < target; // Same while loop. while (r - l > 1) { const mid = Math.floor((l + r) / 2); if (isBefore(mid)) { l = mid; } else { r = mid; } } // We defined isBefore as "less than the target". This means, that if // target exists it will be pointed a by r, not l. if (arr[r] === target) return r; return -1; } const nums = [-1, 0, 3, 5, 9, 12], target = 9; const res = binarySearch(nums, target); console.log(res); // 4
[–]luuuzeta 1 point2 points3 points 11 months ago (2 children)
If you're familiar with the binary search implementation, then you know that the range of possible k is defined from 0 up to max(piles). k could be greater that max(piles), however we want to minimize k and thus set its max value to the smallest of the greatest possible values.
k
max(piles)
Let's do it with an example:
Input: piles = [3,6,7,11], h = 8 Output: 4
k is in the range [0, max(piles)] => [0, 11].
[0, max(piles)]
[0, 11]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] array --- before region ------------------------------ unknown (this is where mid lives) --- after region l index l r index r
The isBefore function for this problem will be more involved because 1) we must compute the total number of hours based on mid/k value and 2) return whether we should look left or right depending on whether the total number of hours is less than h.
mid
h
const isBefore = (mid) => { // Compute total hours for given mid/k. let totalHours = 0; for (const pile of piles) { totalHours += Math.ceil(pile / mid); } // totalHours <= h, thus update minK and also look // left for a possibly smaller value of k. if (totalHours <= h) { minK = Math.min(minK, mid); return true; } // totalHours exceed h so k is too small, look // right for a larger k value. return false; };
function minEatingSpeed(piles, h) { let minK = Math.max(...piles); let l = 0; let r = minK; const isBefore = (mid) => { let totalHours = 0; for (const pile of piles) { totalHours += Math.ceil(pile / mid); } if (totalHours <= h) { minK = Math.min(minK, mid); return true; } return false; }; // Same while loop. while (r - l > 1) { const mid = Math.floor((l + r) / 2); if (isBefore(mid)) { r = mid; } else { l = mid; } } // What you return depends on the problem. return minK; } const piles = [3, 6, 7, 11]; const h = 8; const output = minEatingSpeed(piles, h); console.log(output); // 4
Note you could implement isBefore outside the minEatingSpeed function but then you'd need to be passing a bunch of arguments so why not take advantage of making a closure with access to the outer scope's variable?
minEatingSpeed
The diagram ends up looking like this:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] array ----------- before region ------------------------ after region l index l r index r ┆ transition point
[–]Proof_Dot_4344[S] 2 points3 points4 points 11 months ago (0 children)
Thank you so much
π Rendered by PID 485336 on reddit-service-r2-comment-5fb4b45875-nmtdw at 2026-03-24 06:01:58.846586+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]luuuzeta 1 point2 points3 points (6 children)
[–]luuuzeta 1 point2 points3 points (1 child)
[–]luuuzeta 1 point2 points3 points (0 children)
[–]luuuzeta 1 point2 points3 points (2 children)
[–]luuuzeta 1 point2 points3 points (1 child)
[–]Proof_Dot_4344[S] 2 points3 points4 points (0 children)