Solving All Blind75 Binary Search Pattern LeetCode Problems

preview_player
Показать описание
Today I solve and explain 2 Blind75 Binary Search Pattern problems live in one sitting.

If you found this helpful Like and Subscribe! I solve LeetCode Algorithms (Python/ Java) and SQL Problems daily!

#leetcode #python #python3 #tutorial #coding #programming
Рекомендации по теме
Комментарии
Автор

How is your Binary Search studying going? Comment below a Leetcode question you need help with!! 🙏

datawithcarter
Автор

The solution to the first problem doesn't work for me, but I think they've updated Neetcode's test for this question. In cases where the array has _not_ been rotated, we never achieve an answer here since it will continue to search the right side of the array in the default else statement, and we'll never find a situation where nums[m - 1] is greater than nums[m]. I needed to add a condition to return nums[l] when nums[m] is greater than or equal to nums[l], and less than nums[r]. This situation should always mean that the array between left and right are sorted, meaning the left pointer is pointing at the first index of sorted array.

There is also an issue when the array has a length of one. That's easily solvable with an if statement before starting to return the one item.

Last issue was with array of length 2. I needed to update my while statement to read: while (l <= r). In the provided solution here, in a two item array that's been rotated to [2, 1], it would correctly realize the dip is on the right side, move the left point at index 1, and keep the right pointer at index 1, making them equal and then exiting the loop, ultimately returning nothing. Adding the less than _or equal_ condition to the while loop makes sure it runs that last loop and checks the number you've found when you've drilled down to the last possible index.

Maybe I messed something up, because I am working in JS, and not Python, but I don't think this solution as written would work in the circumstances that provide errors on Neetcode.

The hangups were [4, 5, 6, 7], [1], and [2, 1]


.... re-reading it, I think I made a mistake by not including the final return nums[l]... I think that alleviates all the issues in those 3 arrays. I'll leave the comment in case someone makes the same mistake.

dalespiteri