- Notifications
You must be signed in to change notification settings - Fork2.4k
refactor: js - binary search#464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
30 changes: 19 additions & 11 deletionsjavascript/153-Find-Minimum-in-Rotated-Sorted-Array.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
/** | ||
* @param {number[]} nums | ||
* Time O(log(N)) | Space O(1) | ||
* @return {number} | ||
*/ | ||
var findMin = function(nums) { | ||
let [ left, right ] = [ 0, (nums.length - 1) ]; | ||
while (left < right) { | ||
const mid = (left + right) >> 1; | ||
const guess = nums[mid]; | ||
const [ leftNum, rightNum ] = [ nums[left], nums[right] ]; | ||
const isTarget = leftNum < rightNum; | ||
if (isTarget) return leftNum; | ||
const isTargetGreater = leftNum <= guess; | ||
if (isTargetGreater) left = mid + 1; | ||
const isTargetLess = guess < leftNum; | ||
if (isTargetLess) right = mid; | ||
} | ||
return nums[left]; | ||
}; |
55 changes: 29 additions & 26 deletionsjavascript/33-Search-in-Rotated-Sorted-Array.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
64 changes: 39 additions & 25 deletionsjavascript/4-Median-Of-Two-Sorted-Arrays.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
/** | ||
* @param {number[]} nums1 | ||
* @param {number[]} nums2 | ||
* Time O(log(N * M)) | Space O(N) | ||
* @return {number} | ||
*/ | ||
var findMedianSortedArrays = function(nums1, nums2) { | ||
const canSwap = nums2.length < nums1.length; | ||
if (canSwap) [nums1, nums2] = [nums2, nums1]; | ||
let [ left, right ] = [ 0, nums1.length - 1 ]; | ||
const totalLength = nums1.length + nums2.length | ||
const mid = totalLength >> 1; | ||
const isEven = (totalLength % 2) === 0; | ||
while (true) { | ||
const mid1 = left + right; | ||
const mid2 = (mid - mid1) - 2; | ||
const { aLeft, aRight, bLeft, bRight } = getPointers(nums1, mid1, nums2, mid2); | ||
const isTarget = (aLeft <= bRight) && (bLeft <= aRight); | ||
if (isTarget) return isEven | ||
? (Math.max(aLeft, bLeft) + Math.min(aRight, bRight)) / 2 | ||
: Math.min(aRight, bRight); | ||
const isTargetGreater = aLeft <= bRight; | ||
if (isTargetGreater) left = mid1 + 1; | ||
const isTargetLess = bRight < aLeft; | ||
if (isTargetLess) right = mid1 - 1; | ||
} | ||
} | ||
const getPointers = (nums1, mid1, nums2, mid2) => { | ||
const getLeft = (nums, index) => 0 <= index | ||
? nums[index] | ||
: -Infinity; | ||
const [ aLeft, bLeft ] = [ getLeft(nums1, mid1), getLeft(nums2, mid2) ]; | ||
const getRight = (nums, index) => (index + 1) < nums.length | ||
? nums[index + 1] | ||
: Infinity; | ||
const [ aRight, bRight ] = [ getRight(nums1, mid1), getRight(nums2, mid2) ]; | ||
return { aLeft, aRight, bLeft, bRight }; | ||
} |
27 changes: 13 additions & 14 deletionsjavascript/704-Binary-Search.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* Time O(log(N)) | Space O(1) | ||
* @return {number} | ||
*/ | ||
var search = function(nums, target) { | ||
let [ left, right ] = [ 0, (nums.length - 1) ]; | ||
while (left <= right) { | ||
const mid = (left + right) >> 1; | ||
const guess = nums[mid]; | ||
const isTarget = guess === target; | ||
if (isTarget) return mid; | ||
const isTargetGreater = guess < target; | ||
if (isTargetGreater) left = mid + 1; | ||
const isTargetLess = target < guess; | ||
if (isTargetLess) right = mid - 1; | ||
} | ||
return -1; | ||
}; | ||
93 changes: 14 additions & 79 deletionsjavascript/74-Search-A-2D-Matrix.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,27 @@ | ||
/** | ||
* @param {number[][]} matrix | ||
* @param {number} target | ||
* Time O(log(ROWS * COLS)) | Space O(1) | ||
* @return {boolean} | ||
*/ | ||
var searchMatrix = function(matrix, target) { | ||
const [ rows, cols ] = [ matrix.length, matrix[0].length ]; | ||
let [ left, right ] = [ 0, ((rows * cols) - 1) ]; | ||
while (left <= right) { | ||
const mid = (left + right) >> 1; | ||
const [ row, col ] = [ (Math.floor(mid / cols)), (mid % cols) ] | ||
const guess = matrix[row][col]; | ||
const isTarget = guess === target; | ||
if (isTarget) return true; | ||
const isTargetGreater = guess < target; | ||
if (isTargetGreater) left = mid + 1; | ||
const isTargetLess = target < guess; | ||
if (isTargetLess) right = mid - 1; | ||
} | ||
return false; | ||
} | ||
41 changes: 19 additions & 22 deletionsjavascript/875-Koko-Eating-Bananas.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
/** | ||
* @param {number[]} piles | ||
* @param {number} h | ||
* Time O(N * log(M)) | Space O(1) | ||
* @return {number} | ||
*/ | ||
var minEatingSpeed = function(piles, h) { | ||
let [ left, right ] = [ 1, Math.max(...piles) ]; | ||
while (left < right) { | ||
const mid = (left + right) >> 1; | ||
const hourSpent = getHourSpent(mid, piles); | ||
const isTargetGreater = h < hourSpent; | ||
if (isTargetGreater) left = mid + 1; | ||
const isTargetLess = hourSpent <= h; | ||
if (isTargetLess) right = mid; | ||
} | ||
return right; | ||
} | ||
const getHourSpent = (mid, piles, hourSpent = 0) => { | ||
for (const pile of piles) { | ||
hourSpent += Math.ceil(pile / mid); | ||
} | ||
returnhourSpent; | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.