Longest Increasing Subsequence O(n log n) dynamic programming Java source code

preview_player
Показать описание
Given an array of random numbers, find a longest increasing subsequence. This subsequence is not necessarily contiguous, or unique.

In this tutorial we illustrate a dynamic programming (DP) solution that runs in O(nlogn). Source code is available in Java. Two proofs are also presented. The first proof is done by induction. The second proof makes use of the fact that Patience Sort algorithm minimizes the number of piles, and since each pile contains cards in decreasing order, any increasing sequence can have at most one card from each pile.

This programming challenge is commonly given during technical/coding interviews at engineering firms such as Google and Microsoft.

Source code, implemented in Java:

"Patience Sort" is a general purpose sorting algorithm that in the process of sorting also finds a longest increasing subsequence. This video explains how the Patience Sort works by placing numbers (playing cards are used for illustration) into piles of decreasing values. If we also keep a pointer from the placed card to the top of the pile immediately on the left side, chains of pointers get created, leading to the left most pile. So at the end, you can pick any card on the right most pile and follow the pointers to the left most pile, recovering the (or one of the) longest increasing subsequence in reverse order.

Informal proof:
Since the cards within a pile form a decreasing sequence, any increasing sequence can use at most one card from each pile. So regardless of what strategy you use to play the game, the number of piles is always ≥ the length of any increasing sequence.
Since "Patience Sort" greedy strategy minimizes the number of piles and each pile is linked via pointers, following the pointers gives us a longest increasing sequence.

Proof by induction:
The first card, lets call it card-1, is put into pile-1 and obviously creates a subsequence of length 1. That’s our base case.

For the second step, assume that after placing the nth card, card-N, into a pile-K, a subsequence of length K is created. Lets see if this holds true for the next card, cardN+1. If cardN+1 is greater than card-N, it won’t be placed onto any pile to the left of pile-K since "Patience Sort" greedy strategy would have placed the previous card, card-N, into that pile instead of pile-K.

And it won’t be placed into pile-K since the rule for "Patience" game does not allow it.

Thus, cardN+1 is placed into some pile to the right, thereby, increasing the length of the previously calculated subsequence.

Finding the needed pile takes O(log n) time using binary search. This needs to be done for each card, so the overall running time is O(n log n).

leetcode 300 "Given an unsorted array of integers, find the length of longest increasing subsequence"

Written and narrated by Andre Violentyev
Рекомендации по теме
Комментарии
Автор

1:22 "To understand how it works, let's develop our intuition..." Man, I wish more math and computer science teachers would be better at this.

ericfricke
Автор

The demo with the card is where exactly I understood "How the algorithm works?". Before watching this video, went through various other videos but couldn't understand the pointer concept correctly. A little graphical content makes learning better. Thanks for this video

madhivarman
Автор

Such a wonderful explanation in such less time, I have already wasted 20 min of reading in order to understand this, but understood it, in just 5 min.

mr.curious
Автор

Awesome. That’s the best explanation of finding the longest increasing subsequence I’ve seen. Thanks for the video!

maridavies
Автор

There are only 2 YouTube videos explaining the problem in O(nlogn) time. Your explanation is very clear and you are awesome!

annas
Автор

I cam here from other channel's comment section,
Not going back now...

mayankagrawal
Автор

I can't believe 23 people have disliked this video. In my opinion, this is the best possible explanation for this solution. Thanks for the video !!

uddeshyaagrawal
Автор

Thank you for including proof of correctness in your video. It really helped convince my stupid little brain

HetThakkar
Автор

I keep coming back to watch this again time over time. The best explanation of the algorithm of all time.

mike-yjmm
Автор

Simple, concise and the best explanation of longest increasing subsequence! Subscribed :) We need more of this!

narihanellaithy
Автор

I solved it. I understood the problem after looking at your cards example, the example made this algorithm clear for me.

anandsrikumar
Автор

the best explanation so far for longest increasing subsequence problem in nlgn time.

vineetbhargava
Автор

Damn, that's impressive.

Most dp algorithms are n*m, so n log n is a nice improvement!

dorondavid
Автор

I have much appreciation for the one making this video. The algorithm is very simple with image illustration making things easy to understand. Even though, I think there is a problem in your proof. The main thing needs to be hold is "the longest increasing subsequence." "Card n+1 must go into some pile on the right" doesn’t lead to anything

playerunknown
Автор

Phenomenal explanation of LIS! I couldn't find any video describing why patience sort actually works for LIS! Amazing work sir! Thank you for your time and contribution for spreading the knowledge.

adrijachakraborty
Автор

Hands down, the best explanation I've seen on YouTube

benmontgomery
Автор

I really appreciate the graphics, and your calm voice. You are very relaxed for someone whose last name is VIOLENTey

nobsreviews
Автор

WOw sir, you made my day.I came to your video after the lonegest sub sequence problem

PhoenixRisingFromAshes
Автор

This video is the best explanation for the longest increasing subsequence I have seen. Thank you a lot for it, I enjoyed a lot, and we hope we can see more videos from you.

SeyhunSaryldz
Автор

Really good explanation! Watched twice and finally understand the concept. I like your example and pace of talk (very calm, clear but also cheerful, and the pauses is neat). Subscribed!

chelseali
welcome to shbcf.ru