3.7.11 · D3 · HinglishAlgorithm Paradigms

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

3,114 words14 min read↑ Read in English

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

Shuru karne se pehle, dono tools ki ek reminder plain words mein:

Recall Dono machines ek hi saanp mein

DP machine: dp[i] = sabse lambi strictly-increasing chain ki length jo exactly index i pe khatam hoti hai. Ise left to right bharo; answer dp mein kahi bhi sabse bada value hai. Tails machine: tails[k] = sabse chhhota value jo length k+1 ki chain ko khatam kar sakta hai. Har naye x ke liye, sabse pehla tails[k] >= x dhoondo aur overwrite karo; agar koi nahi, toh append karo. Answer = len(tails).


The scenario matrix

Har array jo tum kabhi bhi LIS ko de sakte ho, woh in cells mein se ek mein aata hai. Agar tum sab kar sako, toh tum ne sab kuch dekh liya.

Cell Input shape Kya tricky hai Example ex.
C1 Already sorted (strictly increasing) LIS = poora array; har baar append, koi replace nahi Ex 1
C2 Reverse sorted (strictly decreasing) LIS = 1; har element tails[0] ko replace karta hai Ex 2
C3 All equal strict LIS = 1; ties extend NAHI hone chahiye Ex 3
C4 Degenerate size — empty / one element length 0 aur 1; loops ki boundary Ex 4
C5 Ties in the middle (real growth ke saath mixed) >= vs > yahan matter karta hai Ex 5
C6 General zig-zag ("normal" case) dono machines agree karni chahiye Ex 6
C7 Negative numbers + plateau signs matter nahi karte, sirf order karta hai Ex 7
C8 Word problem (real-world) ek story ko array mein convert karna Ex 8
C9 Exam twistnon-decreasing LIS lower_boundupper_bound swap karo Ex 9
C10 Actual sequence reconstruct karo tails akela nahi kar sakta; parents chahiye Ex 10

Ab hum har cell cover karte hain.


C1 — Already sorted (Ex 1)


C2 — Reverse sorted (Ex 2)


C3 — All equal (Ex 3)


C4 — Degenerate sizes (Ex 4)


C5 — Ties in the middle (Ex 5)


C6 — General zig-zag (Ex 6)


C7 — Negatives with a plateau (Ex 7)


C8 — Word problem (Ex 8)


C9 — Exam twist: non-decreasing LIS (Ex 9)


C10 — Actual subsequence reconstruct karo (Ex 10)


Coverage check

Recall Kya humne matrix ki har cell hit ki?

C1 sorted (Ex 1) ✓ · C2 reverse (Ex 2) ✓ · C3 all-equal (Ex 3) ✓ · C4 empty/singleton (Ex 4) ✓ · C5 mid ties (Ex 5) ✓ · C6 zig-zag (Ex 6) ✓ · C7 negatives+plateau (Ex 7) ✓ · C8 word problem (Ex 8) ✓ · C9 non-decreasing twist (Ex 9) ✓ · C10 reconstruction (Ex 10) ✓. Har woh scenario jo yeh topic throw kar sakta hai, ab kaam kiya ja chuka hai.


Connections

  • Dynamic Programming — har C-cell dp[i] ends-at-i state se solve hua.
  • Binary Searchlower_bound (C1–C8) vs upper_bound (C9) poora strict/non-decreasing switch hai.
  • Patience Sorting — upar wale tails traces literal card-pile runs hain.
  • Greedy Algorithms — "sabse chhhota tail rakho" ne har replace step justify kiya.
  • Russian Doll Envelopes — C10 ki reconstruction woh skill hai jo tum wahan reuse karte ho.
  • Longest Common Subsequence (LCS) — non-decreasing LIS (C9) sorted-with-duplicates ke saath LCS ke barabar hai.