Dynamic programming Flashcards
1
Q
Longest increasing subsequence
A
- each number is longest increasing subsequence of value 1.
- So for each index, iterate from the start and keep on check whether any value lesser than the current value
- If so, max(dp[prev], maxLen)
- Finally update dp[i] = maxLen + 1
- maxAnswer = max(maxAnswer, dp[i])
2
Q
Perfect square
A
- Find the square required by doing sqrt(n)
- For each index, iterate over each sqr and check if its lesser and equal to the index
- If so dp[i] = min(dp[i], dp[i - j*j] + 1)