Dynamic Programming 2
Dynamic Programming 2
return dp[m][n]
Time Complexity:
● O(m * n), where m is the length of text1 and n is the length of text2. We fill up a 2D
table of size (m+1) x (n+1) with each cell being filled in constant time.
Space Complexity:
● O(m * n). We use a 2D array of size (m+1) x (n+1) to store the intermediate results.
Solution:
def lengthOfLIS(nums):
if not nums:
return 0
n = len(nums)
dp = [1] * n
return max(dp)
Time Complexity:
● O(n^2) where n is the length of the array nums. This is because for each element i, we
iterate over all previous elements j (where j < i).
Space Complexity: