Find The Longest Increasing Subsequence - Dynamic Programming Fundamentals

preview_player
Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

Question: Given an array of all integers that may or may not be sorted, determine the length of the longest subsequence that is strictly non-decreasing.

What Is A Subsequence?

A subsequence of an array is a subset of the array that maintains temporal order.

It does not have to be contiguous but it might turn out to be contiguous by chance.

Problem Name / Variants

This problem also comes in the form of asking for the longest strictly decreasing subsequence.

This is longest non-decreasing subsequence meaning that we will have a non-strictly increasing subsequence (aka we can have deltas of 0 between contiguous elements in the subsequence).

Approach 1 (Brute Force)

We can enumerate all 2^n subsets of the original array and then test them for the non-decreasing property.

The answer will be the longest subset that has the property.

This is too expensive.

Approach 2 (Dynamic Programming)

Do you see the potential for a subproblem here?

If you do, then we have the opportunity to use dynamic programming.

Example
[ -1, 3, 4, 5, 2, 8 ]

At the index 0 I always know that I can have a subsequence of length 1.

In fact, at all positions the longest non-decreasing subsequence can be at least length 1.

We then look at index 1, I need to ask myself if the item at index 1 can lengthen the longest subseqence found at index 0.

We check if 3 is greater than or equal to 1...it is. Great. index 1 can be tacked on but...should I?

The LNDS (longest non-decreasing subsequence) at index 1 is 0.

The LNDS at index 0 is 1.

Yeah...it makes sense because if I tack 3 onto the LNDS I found for the subproblem of just [ -1 ] then at index 1 I will also have a LDNS.

So what we basically do is build a table and ask ourselves these questions all along the way.

EACH CELL REPRESENTS THE ANSWER TO THE SUBPROBLEM ASKED AGAINST the subsequence from index 0 to index i (including the element at index i).

Complexities:

Time: O( n^2 )

n is the length of the array.

For each of the n elements we will solve the LNDS problem which takes O(n) time, therefore we yield a O( n^2 ) runtime complexity.

Space: O( n )

We will store our answers for each of the n LNDS subproblems.

++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 17.12 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
Рекомендации по теме
Комментарии
Автор

Table of Contents: (yes, I know I'm screaming. I messed up...this video is old...ok.)

Me Talking (Who Cares) 0:00 - 0:28
The Problem Introduction 0:28 - 1:13
LIS vs. LNDS vs. LDS 1:13 - 2:48
The Approaches 2:48 - 4:38
Full Dynamic Programming Table Walkthrough 4:38 - 16:45
Applying This To Other Dynamic Programming Problems 16:45 - 17:18
Time Complexity 17:18 - 18:00
Space Complexity 18:00 - 18:13
Handling Dynamic Programming In The Interview 18:13 - 18:43
The Usual 18:43 - 19:01


In my opinion, this isn't practical to pull off in an interview if you have never seen this question before and that is what matters to me. So I covered the O( n ^ 2 ) way since it is more reasonable.

The code for this question (with exhaustive comments for teaching purposes) is in the description. It is for the Longest Increasing Subsequence problem. We talked about the Longest Non-Decreasing Subsequence.

BackToBackSWE
Автор

"yes, we can" is very emotional. I will be very strong when I listen to this word. Feel that I can solve this algorithm question now! thanks, Ben

tianxiaowang
Автор

Love how this dude always answers the why questions to the small things compared to other people on youtube

mokim
Автор

Not all heroes wear capes, some wear glasses and a amazon tshirt too :D

awesomeps
Автор

the high spirit in your voice and the medium pace is really motivating and keeps our brain focused. thanks buddy for your favor.

keeplearning
Автор

Dude, you nailed it straight home, perfectly. I had been struggling with this problem for most of my Saturday afternoon. After a gruesome day of zero-progress and infinite hopelessness, the sheer joy and sigh of relief of finally having understood another concept is what made the evening a little better. Thanks !

dangidelta
Автор

The way you explained the problem is absolutely brilliant. Wasted my entire day on other videos.
I'm following you for some months now, every video from you is absolutely Gold. More power to you!

debrc
Автор

I like how consistent you go through the whole run of the algorithm. It really helps understand it.

dcodernz
Автор

The more I watch your videos, the more I think that this channel deserves to grow.

puperhacker
Автор

One of the most energetic guys I found explaining DP.
Thank you.

raghavendraPi
Автор

Your videos are breaking certain things wide open for me that I've never seen before. Thanks man. I'm budgeting in time to watch at least one of your videos a night until I've seen them all

jonathanfoster
Автор

The amount of patience you have to explain this is commendable! Thanks!

ameynaik
Автор

bro you are really a good teacher who tech something more essential rather than just the solution

kiweilau
Автор

You are absolute mad-man by repeating those things. Thank you man, I can't believe you made me understund those things. Never stop!

frozen_tortus
Автор

You did great. You did not mess up. You exhibited passion, which is very engaging to those in your audience. Great presentation.

wesiegel
Автор

You are better than 3/4th of the teachers I have studied from and one of the bests on online platforms! Keep going :)

therawbean
Автор

Someone give this man a noble price...🙌❤

armaanpathan
Автор

Every time I encounter a problem and don't understand it, I go to this channel.
Huge thanks to you for explaining things so clearly, King. <3

adhamsalama
Автор

Wow.
THE BEST explanation of dynamic programming approach for this problem.
Good teaching skills are rare and you've got them.
Thanks for this!

smallcreativecorner
Автор

Man, I went through 5 youtube videos and finally understand how to implement it from yours. You da best!

jjlian