3.7.11 · D1 · HinglishAlgorithm Paradigms

FoundationsDP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

2,465 words11 min read↑ Read in English

3.7.11 · D1 · Coding › Algorithm Paradigms › DP problems — Longest Increasing Subsequence (LIS) — O(n²) a

Parent note ko fluently padhne se pehle, tumhe ideas ki ek chhoti si toolbox chahiye. Yeh page har ek idea ko zero se build karta hai, usi order mein jis order mein woh ek doosre par depend karte hain. Yahan kuch bhi assume nahi kiya gaya hai — agar parent ne koi symbol use kiya, toh hum use pehle yahan define karte hain.


0. Woh picture jis par hum baar baar waapas aate hain

Is topic ki har cheez ek array ke upar hoti hai — boxes ki ek row, har box mein ek number, aur har box ke neeche ek index (uski position number) hota hai.

Figure — DP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

1. Subarray vs Subsequence — cross-out karne ka idea

Yahi THE distinction hai jis par poora topic tika hai.

Figure — DP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

Picture mein, [3, 4, 5] jo [3,1,4,1,5] se aaya hai, ek valid subsequence hai (humne do 1's cross out kiye) lekin subarray nahi hai (boxes ek doosre ke saath nahi hain).

Recall Length-

array mein kitni subsequences hoti hain? boxes mein se har ek independently "kept" ya "crossed out" hota hai. Yahi exactly reason hai ki brute force hai aur hume kuch zyada smart chahiye.


2. "Strictly increasing" — climbing ka matlab kya hai

Figure — DP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

3. Indices aur subscript notation

Definition mein use hota hai. Yahan do alag < signs chhupe hain — inhe confuse mat karo!


4. aur symbols (recurrence mein use hote hain)

Parent ki recurrence yeh hai: Unpack karne ke liye notation ke teen pieces hain.


5. "dp" ka actually matlab kya hai


6. Big-O: aur kya promise karte hain


7. lower_bound aur tails array — fast method ke tools

Figure — DP problems — Longest Increasing Subsequence (LIS) — O(n²) and O(n log n)

Prerequisite map

Array A and index counting from 0

Subsequence delete keep order

Index chain i1 lt i2 order

Strictly increasing lt

LIS length L to maximise

max union set-builder notation

O n squared recurrence

Dynamic Programming store subproblems

Big-O and log n halving

O n log n method

lower_bound and tails array

Parent LIS note

Seedha parent LIS note mein jaata hai aur aage Patience Sorting aur Greedy Algorithms se connect karta hai.


Equipment checklist

LIS acronym ka kya matlab hai?
Longest Increasing Subsequence.
Length-5 array diya ho toh uske last element ka index kya hai?
4 (indices 0 se tak jaate hain)
Empty array () ka LIS kya hai?
0 — climb karne ke liye koi subsequence nahi hai.
Kya [5, 3], [3,1,4,1,5] ki subsequence hai? Kyun ya kyun nahi?
Nahi — order lock hai; tum delete kar sakte ho lekin reorder nahi, aur original mein 5, 3 ke baad aata hai.
Length- array mein kitni subsequences hoti hain?
(har element kept ya crossed out).
Kya [2, 2, 3] strictly increasing hai?
Nahi — strict equal neighbours forbid karta hai; yeh sirf non-decreasing hai.
mein, kya hum positions compare kar rahe hain ya values?
Positions (indices) — yeh original order enforce karta hai, value comparison se alag.
dp table se overall LIS length kaise nikalte hain?
Saare cells par max lo: LIS = over of , kyunki LIS kisi bhi index par khatam ho sakti hai.
recurrence quadratic kyun hai?
Ek inner loop har earlier ko scan karta hai (up to ) boxes mein se har ek ke liye → steps.
Recurrence mein union kyun kiya jaata hai?
Taaki tab bhi defined ho jab koi earlier smaller element exist na kare, jisse length 1 mile (element akela).
kya count karta hai?
Kitni baar tum ko 1 tak pahunchne se pehle half kar sakte ho.
Fast method mein tails[k] kya store karta hai?
Ab tak dekhi gayi length ki kisi bhi increasing subsequence ki sabse chhoti possible last value.
Sorted array par lower_bound(x) kya return karta hai?
Pehle aise element ki position jo ho.