Minimum Number of Removals to Make Mountain Array - Leetcode 1671 - Python

Minimum Number of Removals to Make Mountain Array - Leetcode 1671 - Python

NeetCodeIO

54 года назад

9,749 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@VanjeAv
@VanjeAv - 30.10.2024 03:52

Love you Neet ❤

Ответить
@CSwithSolve
@CSwithSolve - 30.10.2024 03:55

This was hard! but nice explanation.

Ответить
@lost-wind443
@lost-wind443 - 30.10.2024 03:59

Holy hard!

Ответить
@jamestariga08
@jamestariga08 - 30.10.2024 04:02

Papa Neetcode finally posted a new video after 3 days 🥹

Ответить
@yashwantmoharil5892
@yashwantmoharil5892 - 30.10.2024 04:20

Did it using 2 LIS arrays

Ответить
@osamahussam4351
@osamahussam4351 - 30.10.2024 04:49

Coders, is leetCode running so slowly right now ?? I can't even open my profile page
And thx a lot neetCode!!

Ответить
@muntajir646
@muntajir646 - 30.10.2024 06:22

This is not the most optimal solution I guess. Why didn't you use Binary search to reduce the time complexity to NlogN?

Ответить
@MP-ny3ep
@MP-ny3ep - 30.10.2024 07:30

One of the best explanations out there! Thank you so much for doing this!

Ответить
@Kaviarasu_NS
@Kaviarasu_NS - 30.10.2024 07:35

# was able to rewrite your solution that's more understandable to me

def minimumMountainRemovals(self, nums: List[int]) -> int:
n = len(nums)

lis = [1] * n
for r in range(1, n):
for l in range(r):
if nums[r] > nums[l]:
lis[r] = max(lis[r], lis[l] + 1)

lds = [1] * n
for r in range(n - 2, -1, -1):
for l in range(n - 1, r, -1):
if nums[r] > nums[l]:
lds[r] = max(lds[r], lds[l] + 1)


max_mountain = 0
for i in range(1, n - 1):
if lis[i] > 1 and lds[i] > 1:
cur_mountain = lis[i] + lds[i] - 1
max_mountain = max(max_mountain, cur_mountain)

return n - max_mountain

Ответить
@janaSdj
@janaSdj - 30.10.2024 07:58

Thanks

Ответить
@jegadheeswarank6290
@jegadheeswarank6290 - 30.10.2024 09:27

Smooth

Ответить
@AlfredPros
@AlfredPros - 30.10.2024 10:54

Additional optimization to papa NeetCode's solution would be to put the LIS and result loop together because their loop starts and ends on the same index.

def minimumMountainRemovals(self, nums: List[int]) -> int:
lds = [1 for i in nums]
lis = [1 for i in nums]
res = len(nums)

# Get lds
for i in range(len(nums)-2, 0, -1):
cur_max = 1
for j in range(i+1, len(nums)):
if nums[i] > nums[j] and lds[j]+1 > cur_max:
cur_max = lds[j]+1
lds[i] = cur_max

# Get lis and res
for i in range(1, len(nums)-1):
cur_max = 1
for j in range(i):
if nums[i] > nums[j] and lis[j]+1 > cur_max:
cur_max = lis[j]+1
lis[i] = cur_max

# Calc res
if cur_max != 1 and lds[i] != 1:
calc = len(nums)-cur_max-lds[i]+1
res = calc if calc < res else res

return res

And also because we loop only the required indexes, the resulting code runs faster on 936 ms (69%) on my end.

Ответить
@shibambiswas
@shibambiswas - 30.10.2024 11:20

The peak of leetcode

Ответить
@ahmedtremo
@ahmedtremo - 30.10.2024 13:18

appreciate your explanations as usual

Ответить
@samarthjain5015
@samarthjain5015 - 30.10.2024 14:21

Should have also included the O(Nlog(N)) solution using Binary Search.

Ответить
@Friend01foru
@Friend01foru - 30.10.2024 14:22

I am thinking to include ur photo in my prayer room 😁, good explanattion bro

Ответить
@abhinavnarula7300
@abhinavnarula7300 - 30.10.2024 14:23

Similar to Longest Bitonic subsequence if anyone wants to practice similar kind of a problem.

Ответить
@pastori2672
@pastori2672 - 30.10.2024 14:33

im saving the neetcode 150 for when i actually get an interview

Ответить
@pastori2672
@pastori2672 - 30.10.2024 14:46

no binary search solution ? 😢

Ответить
@victoriatfarrell
@victoriatfarrell - 30.10.2024 16:50

Good advice. Don't get discouraged -- there's always an opportunity to learn and appreciate.

Ответить
@LasTCursE69
@LasTCursE69 - 30.10.2024 17:02

The way I did it is by defining two right pointers and two left pointers (the left ones are from the nested loops themselves) and I updated the right ones as we went along. This way, you can use a single nested loop instead of doing it twice :)

It's a bit harder to read, but works faster

let rp1 = arr.length - 2;
let rp2 = arr.length - 1;


for (let lp1 = 1; lp1 < arr.length; lp1++) {
for (let lp2 = 0; lp2 < lp1; lp2++) {
if (arr[lp2] < arr[lp1]) { increase[lp1] = Math.max(increase[lp1], increase[lp2] + 1); }
if (rp1 >= 0 && rp2 > rp1 && arr[rp2] < arr[rp1]) { decrease[rp1] = Math.max(decrease[rp1], decrease[rp2] + 1)}
rp2--;
}
rp1--;
rp2 = arr.length - 1;
}

Ответить
@maheshkaranam1944
@maheshkaranam1944 - 30.10.2024 18:41

Complex codes made easy. Super sir.
Can you suggest some books to learn complete DSA ?
I wonder how you learnt DSA. could you please answer ?

Ответить
@akialter
@akialter - 30.10.2024 20:40

It will be a pro gamer move if they let yesterday problem LIS

Ответить
@gabrieldavi9174
@gabrieldavi9174 - 30.10.2024 21:25

Hi Neetcode! I got an offer from Uber!
I would never be able to pass their interview without your videos. Keep it up!

Ответить
@nirmalgurjar8181
@nirmalgurjar8181 - 30.10.2024 21:39

Bruteforce would be backtracking, generating all subseq and checking each subseq if its mountain and return its size, but time complexity would be n * 2^n then we can try bottom up dp LIS / LDS and come up n*n solution.

Ответить
@jaatharsh
@jaatharsh - 30.10.2024 22:02

wonderfull as always, thanks for gr8 explanation

Ответить
@devmahad
@devmahad - 30.10.2024 22:09

thanks :)

Ответить
@Pyramid-yc3ex
@Pyramid-yc3ex - 31.10.2024 07:10

Hi, Love your videos, they are very helpful!!!

Have you tried to use Open AI O1 model since it was released? I would like to hear your thoughts/comments on its code generation capabilities. Thanks!!

Ответить
@WebSleek
@WebSleek - 31.10.2024 08:23

Hii Neetcode I has able to come up with LIS and LDS solution but in editorial there are other approaches also
do i really need to go through them or if i am able to solve it then it good go ahead ?
pls help me out !!!

Ответить
@anupam_yadav_7
@anupam_yadav_7 - 31.10.2024 08:53

That was mesmerzing.

Ответить
@nanicreations7729
@nanicreations7729 - 01.11.2024 09:47

Thank you❤

Ответить
@adityaagarwal-ii1hv
@adityaagarwal-ii1hv - 01.11.2024 22:15

why are the code solutions removed form website?

Ответить
@ceciljoel9577
@ceciljoel9577 - 02.11.2024 15:36

How much time it took you to solve?

Ответить
@divyarajsinhrana2094
@divyarajsinhrana2094 - 05.11.2024 08:57

reaallyy appreciate your work nd passion!!! :)
Suggesting an optimization on this problem
You can do Binary search to find LIS at particular index

class Solution {
public:
int minimumMountainRemovals(vector<int>& nums) {
int sz = nums.size();
vector<int> pre(sz, 0), post(sz, 0);
vector<int> temp;
int tempSz=1, ans=0;
temp.push_back(nums[0]);
pre[0]=0;
for(int i=1;i<sz;i++)
{
if(nums[i] > temp[tempSz-1])
{
pre[i] = tempSz;
temp.push_back(nums[i]);
tempSz++;
continue;
}
int mid = lower_bound(temp.begin(), temp.end(), nums[i]) - temp.begin();
pre[i] = mid;
temp[mid] = nums[i];
}

post[sz-1]=0;
tempSz=1;
temp.clear();
temp.push_back(nums[sz-1]);
for(int i=(sz-2);i>=1;i--)
{
if(nums[i] > temp[tempSz-1])
{
post[i] = tempSz;
temp.push_back(nums[i]);
tempSz++;
if(pre[i]!=0 && post[i]!=0)
ans = max(ans, pre[i]+post[i]);
continue;
}
int mid = lower_bound(temp.begin(), temp.end(), nums[i]) - temp.begin();
post[i] = mid;
temp[mid] = nums[i];
if(pre[i]!=0 && post[i]!=0)
ans = max(ans, pre[i]+post[i]);
}
return sz-ans-1;
}
};

Just to skim thru the code for LIS portion,
1) temp is storing longest LIS till index i.
2) If arr[i] > temp.end(), means it is greater element and hence can be part of LIS in future.
3) If not, then we find index of number which is just greater than the arr[i]. All numbers before that index is LIS till index i and because we need LIS, we will store minimum number at particular index.
4) We reverse this process for decreasing sequence OR wwe traverse the sequence in reverse order

Hope it helps....any queries or suggestions are welcome
Just an FYI, it did beat 100% of users

Ответить