Find in Mountain Array - Leetcode 1095 - Python

  Рет қаралды 11,427

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 16
@pranav2066
@pranav2066 Жыл бұрын
This has become my routine. Leetcode all day and daily challenge with Neetcode at Night! Thanks for the Videos
@steeve1
@steeve1 Жыл бұрын
I think most hards are pretty silly but I liked this one, probably because i'm comfortable with binary search and didn't feel there was some obscure trick that i'm supposed to magically know.
@rudrakhare1158
@rudrakhare1158 3 ай бұрын
Corrected Code :_ class Solution: def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int: length = mountain_arr.length() # Find the peak of the mountain array l, r = 0, length - 1 while l < r: m = (l + r) // 2 if mountain_arr.get(m) < mountain_arr.get(m + 1): l = m + 1 else: r = m peak = l # Binary search on the increasing part l, r = 0, peak while l target: r = m - 1 else: return m # Binary search on the decreasing part l, r = peak, length - 1 while l target: l = m + 1 # This should be r = m - 1 elif val < target: r = m - 1 # This should be l = m + 1 else: return m return -1
@angelchen2843
@angelchen2843 8 ай бұрын
Quite easy to understandable!!!! ❤❤
@picnicbros
@picnicbros Жыл бұрын
I love binary search
@silent7152
@silent7152 Жыл бұрын
Bro help me out 1. Use while(l
@picnicbros
@picnicbros Жыл бұрын
@@silent7152 I suggest you read "Powerful Ultimate Binary Search Template" (search on google) to never have bug in your binary search again! The template is basically: left, right = min(search_space), max(search_space) while left < right: mid = left + (right - left) // 2 if condition(mid): left = mid + 1 else: right = mid return left where the else here include the exiting condition *as well as* condition when you update the right pointer. If you do that, in the else statement you can safely update the right pointer. Essentially, you *dont* want to have the exiting condition when you update the left pointer! The template also can be rewritten with those points in mind and still work: left, right = min(search_space), max(search_space) while left < right: mid = left + (right - left) // 2 if condition(mid) or exiting_condition(mid): right = mid else: left = mid + 1 return left So, for this problem, here is two method that will return the peak index: Update left pointer in if condition() method: easier since you don't have to find the exiting condition: while l < r: m = l + (r - l) // 2 if mountain_arr.get(m - 1) < mountain_arr.get(m) < mountain_arr.get(m + 1): l = m + 1 else: r = m peak = l Update right pointer in if condition or exiting_condition() method: while l < r: m = l + (r - l) // 2 left, mid, right = mountain_arr.get(m - 1), mountain_arr.get(m), mountain_arr.get(m + 1) if left > mid > right or (mid > left and mid > right) : r = m else: l = m + 1 peak = l Some side note: this template assumes that the item you want to search is in the range you define. If it is not, it will return the next index that it "assume" the item will be. For example: [1 2 3 4 5], if you search 12, it will assume that 12 will be at index 5, which does not exist in the search space, but it is correct given the condition that arr[i] < arr[i + 1] (sorted array) Also, this template return the first occurrence of your target,if your target has duplicate. For example: [1 2 3 3 3 6 7] if you search 3, it will return 2 (the first occurrence) If you want to find the last occurrence, simply search for target + 1 instead. It will then "assume" the item target + 1 will be in the next position of the last occurrence of target: [1 2 3 3 3 6 7] if you search 4 (target 3 + 1), it will return left = 5. Now, index 5 doesn't have 4, but 6, but that's okay, we simply return left - 1 and that's our last occurrence of target 3. I suggest you do Find First and Last Position of Element in Sorted Array and implement these points. Good luck!
@GuruPrasadShukla
@GuruPrasadShukla Жыл бұрын
keep solving mate!
@dera_ng
@dera_ng Жыл бұрын
Do you think I over-thought the question? Maybe if i was in a real interview then I could ask the interviewer if there could be multiple peaks. The English structure of the question promoted me to think about it a bit more and then I got hung up on the edge case (if valid) of having multiple peaks. 😢..... If it's a hard question, I was expecting it to be harder than this.
@rajarshiverma7686
@rajarshiverma7686 Жыл бұрын
Thanks for the great solution, can you also mention how to use bisect here as I have seen solution where few people used bisect
@yfjsdgzjdnfn
@yfjsdgzjdnfn Жыл бұрын
NeetCode please solve Leetcode 8 String to Integer there are many edge cases out there in that problem ,I requesting please do Video on that
@hikumai289
@hikumai289 Жыл бұрын
This is medium rather than hard. Wish all of you a good day.
@sumtpathak19
@sumtpathak19 Жыл бұрын
the reason behind using mountainArray class is to calculate no. of .get not exceed 100
@adrianengcheeern
@adrianengcheeern 10 ай бұрын
Is there a possibility that m - 1 or m + 1 would go out of range of the list?
@user-le6ts6ci7h
@user-le6ts6ci7h Жыл бұрын
You can't do linear search here eventhough the input size is of 10 pow 4 , because you can call the get at max 100 whixh is enforced by the interface, so better way is use binary search and which takes at max
Painting the Walls - Leetcode 2742 - Python
14:29
NeetCodeIO
Рет қаралды 14 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 679 М.
Trick-or-Treating in a Rush. Part 2
00:37
Daniel LaBelle
Рет қаралды 46 МЛН
Can You Find Hulk's True Love? Real vs Fake Girlfriend Challenge | Roblox 3D
00:24
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 51 МЛН
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 361 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 145 М.
Find K Closest Elements - Leetcode 658 - Python
18:21
NeetCode
Рет қаралды 75 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 567 М.
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 289 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 851 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 634 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 434 М.
Trick-or-Treating in a Rush. Part 2
00:37
Daniel LaBelle
Рет қаралды 46 МЛН