1574. Shortest Subarray to be Removed to Make Array Sorted | Binary Search | 2 Pointers

preview_player
Показать описание
In this video, I'll talk about how to solve Leetcode 1574. Shortest Subarray to be Removed to Make Array Sorted | Binary Search | 2 Pointers

Let's Connect:

✨ Timelines✨
0:00 - Problem Explanation
1:08 - Example Explanation
2:34 - Intuition Building & Search Algos (Linear & Binary)
5:50 - Trying to use 2 Pointer
8:16 - Covering edge Cases
12:06 - Code & Complexity Explanation

✨ Hashtags ✨
#programming #Interviews #leetcode #faang #maang #datastructures #algorithms
Рекомендации по теме
Комментарии
Автор

for ans we need to choose minimum value btn right and n-left-1 so that it will give valid ans for one of the test case [10, 13, 17, 21, 15, 15, 9, 17, 22, 22, 13]. Your explaination is nicee sirr!!

sujalchavan
Автор

Bhaiya, the way how you said that the problem might be hinting at either binary search or DP once you saw it was a shortest subarray problem, can you say how you did that?

SohanKumar-fpix
Автор

@ARYANMITTAL since we already have increasing sequences on both sides of the array, and our job is to only find the conneting element so that both halfs are connected.

So left half increasing seq [l1, r1] and right half increasing seq [ l2, r2]
We can only check if for r1 elment, there exists an element in the other half [l2, r2] which is smaller than r1, if we do not find any element smaller, we say directly connect r1 with l2 to form a connecting link.

If we find a smaller element, that means we need to let go of r1 or l2 to skip some elements until we find any element larger than r1 in second half OR any element snaller than l2 in the left half..
We can linearly check by half using two pointers technique and once find such an element and its index we just link it to form an array.

Is this approach still O(n2) solution??

sushilsingh
welcome to shbcf.ru