Chapter 4 - Dynamic Programming
Dynamic programming can be understood as optimizing sub-problem by decomposing a big problem
Greatest Common Subsequence
function leng(i,j):
if i or j is 0: //going backward, if i or j reaches the first element, then that means we r done
return 0
if A[i] == B[j]: //if they are the same, then just decrement 1 to search for new elements
1+leng(i-1,j-1)
else: //if they are different, then take the maximum of i-1 or j-1
max(leng(i,j-1), leng(i-1,j))def leng(i,j):
if i == 0 or j == 0:
return 0
if a[i] == a[j]:
temp = 1 + leng(i-1,j-1)
else:
temp = leng(i-1,j)
temp2 = leng(i,j-1)
temp = max(temp, temp2)
return tempWood Cutting Problem
Last updated