Given two stacks of pizzas such that each contains 10 pizzas of varying radii. Geek can only eat a pizza from a stack if it is also present in the other stack, in the same order. The pizzas that he selects need not be continuous. Find the number of pizzas Geek can eat.
 
Example 1:
Input:
Stack1 = {891 424 945 741 897 514 692 221 678 168}
Stack2 = {702 952 221 614 69 753 821 971 318 364}
Output: 1
Explanation: There is only one common element.
 
Example 2:
Input:
Stack1 = {476 241 866 571 827 599 949 948 579 726}
Stack2 = {371 359 610 216 885 876 92 10 171 724}
Output: 0
Explanation: There is no common element.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function getCommon() which takes two arrays stack1[] and stack2[], each of fixed size 10 as input parameters and returns the number of pizzas Geek can eat.
 
Expected Time Complexity : O(N^{2})
Expected Auxiliary Space : O(N^{2}) Where N is the stack length = 10.
 
Constraints:
Stack_{1 }= Stack_{2} = 10
1 <= radii <= 10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def getCommon(self, stack1, stack2):
    

Solutions:
class Solution:

	def getCommon(self, stack1, stack2):
		(m, n) = (len(stack1), len(stack2))
		dp = [[0] * (n + 1) for _ in range(m + 1)]
		for i in range(1, m + 1):
			for j in range(1, n + 1):
				if stack1[i - 1] == stack2[j - 1]:
					dp[i][j] = dp[i - 1][j - 1] + 1
				else:
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
		return dp[m][n]


class Solution:

	def getCommon(self, stack1, stack2):
		lcslen = [[0 for i in range(12)] for j in range(12)]
		for i in range(0, 10):
			for j in range(0, 10):
				if stack1[i] == stack2[j]:
					lcslen[i + 1][j + 1] = 1 + lcslen[i][j]
				else:
					lcslen[i + 1][j + 1] = max(lcslen[i + 1][j], lcslen[i][j + 1])
		return lcslen[10][10]


class Solution:

	def getCommon(self, a, b):
		N = len(a)
		M = len(b)
		if M == 0 or N == 0:
			return 0
		max = 0
		for i in range(N):
			x = 0
			if a[i] in b:
				x = 1 + self.getCommon(a[i + 1:], b[b.index(a[i]) + 1:])
			if x > max:
				max = x
		return max


class Solution:

	def getCommon(self, a, b):
		N = len(a)
		M = len(b)
		if M == 0 or N == 0:
			return 0
		dp = [[0 for _ in range(11)] for _ in range(11)]
		for i in range(1, 11):
			for j in range(1, 11):
				if a[i - 1] == b[j - 1]:
					dp[i][j] = 1 + dp[i - 1][j - 1]
				else:
					dp[i][j] = max(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j])
		return dp[-1][-1]


class Solution:

	def getCommon(self, stack1, stack2):
		dp = {}

		def rec(i, j):
			if i < 0 or j < 0:
				return 0
			if (i, j) in dp:
				return dp[i, j]
			if stack1[i] == stack2[j]:
				dp[i, j] = 1 + rec(i - 1, j - 1)
			else:
				dp[i, j] = max(rec(i, j - 1), rec(i - 1, j))
			return dp[i, j]
		return rec(9, 9)