Can’t Understand the recursive approach!
K-Ordered LCS by DP
@Codemaniac_Aditya think this way, let’s say you are at index i and j of array1 and array2 respetively along with K allowed changes in array1, now there are two possibilities:
- arr1[i]==arr2[j], here you simply return 1+K_lcs(i+1,j+1,K)
- arr1[i]!=arr2[j], now you can move either of index , also if K>0 then you can also make arr[i] equal to arr[j], hence you do it as:
return max(K_lcs(i+1,j,K), K_lcs(i,j+1,K), K_lcs(i+1,j+1,K-1)
return max(K_lcs(i+1,j,K), K_lcs(i,j+1,K), 1 + K_lcs(i+1,j+1,K-1) is correct for 2.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.