A=[0,1,0,3,2,3] ke liye LIS length kya hai aur ek aise subsequence ka example?
Length 4: [0,1,2,3]. tails se check karo: [0]→[0,1]→ (0 replace karo)[0,1]→[0,1,3]→(3 ko 2 se replace karo)[0,1,2]→[0,1,2,3]. ✓
What is the difference between a subsequence and a subarray?
Subarray contiguous hota hai; subsequence elements ko reorder kiye bina delete karke banta hai (gaps ho sakte hain).
Define the DP state used in the O(n²) LIS solution.
dp[i] = longest increasing subsequence ki length jo exactly index i par end hoti hai.
State the O(n²) LIS recurrence.
dp[i] = 1 + max(0, {dp[j] : j<i and A[j]<A[i]}); answer = max over all dp[i].
Why is the final answer max(dp) and not dp[n-1]?
Kyunki LIS kisi bhi index par end ho sakti hai, zaroor nahi ki last wale par.
In the O(n log n) method, what does tails[k] store?
Length k+1 ki kisi bhi increasing subsequence ki sabse chhoti possible tail value.
Why is the tails array always strictly increasing?
Ek lambi chain ko kaat dene par ek chhoti tail wali chhoti chain milti hai, aur tails[k] woh sabse chhoti tail hai, isliye values length ke saath badhni chahiye.
For each new x in patience sorting, what do you do?
lower_bound se pehla tails[k] >= x dhundho; replace karo, ya x ko append karo agar x saari tails se bada ho.
Why use lower_bound (>=) instead of upper_bound (>) for strict LIS?
Taaki equal values ek fake lambi increasing chain na banayein; >= ise strictly increasing rakhta hai.
Does len(tails) equal the LIS length? Is tails the actual LIS?
len(tails) = sahi LIS length; lekin tails khud ek valid subsequence NAHI hai (ise recover karne ke liye predecessor pointers chahiye).
What are the time complexities of the two LIS methods?
O(n²) DP, aur O(n log n) tails par binary search ke saath.
Recall Feynman: ek 12-saal ke bachche ko samjhao
Socho cards ko face-up piles mein left se right rakh rahe ho. Rule: ek card sirf us pile par ja sakta hai agar woh us pile ke top card se bada ho, aur tum hamesha use sabse left wali pile par rakhte ho jahan woh fit ho — agar kahin fit nahi hota, toh right mein ek bilkul naya pile shuru karo. Jitne piles bante hain woh cards ki sabse lambi "hamesha barhti hui" line ki length hai jo tum chun sakte ho! Har pile ka top jitna ho sake utna chhota rakha jaata hai taaki baad mein zyada cards stack ho sakein. Woh "leftmost pile" choice hi binary search hai.