Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! π»π₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! π
100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney
Problem:
https://www.geeksforgeeks.org/problems/longest-common-increasing-subsequence1437/1
Longest Common Increasing Subsequence
Difficulty: Medium Accuracy: 37.37%
Given two arrays, a[] and b[], find the length of the longest common increasing subsequence(LCIS).
Note: LCIS refers to a subsequence that is present in both arrays and strictly increases.
Examples:
Input: a[] = [3, 4, 9, 1], b[] = [5, 3, 8, 9, 10, 2, 1]
Output: 2
Explanation: The longest increasing subsequence that is common is [3, 9] and its length is 2.
Input: a[] = [1, 1, 4, 3], b[] = [1, 1, 3, 4]
Output: 2
Explanation: There are two common subsequences [1, 4] and [1, 3] both of length 2.
Constraints:
1 β€ a.size(), b.size() β€ 103
1 β€ a[i], b[i] β€ 104
Solution:
class Solution:
def LCIS(self, a, b):
n, m = len(a), len(b)
dp = [0]*m
for i in range(n):
current_max = 0
for j in range(m):
if a[i] == b[j]:
dp[j] = current_max + 1
elif b[j] < a[i]:
current_max = max(current_max, dp[j])
return max(dp)
Top comments (0)