//java-MOST OPTIMAL // tc=O(n) // sc=O(n) class Solution { public int[] findThePrefixCommonArray(int[] A, int[] B) { int n=A.length; int commonEleCount=0; int[] ans=new int[n]; Set sett=new HashSet(n); for(int i=0;i
@darshankumar554613 күн бұрын
Python documentaion for set: docs.python.org/2/library/sets.html
@ollieorion36913 күн бұрын
Great Video 😌 Sir
@darshankumar554612 күн бұрын
Thanks 🙂
@darshankumar554613 күн бұрын
#python3-MOST OPTIMAL # tc=O(n) # sc=O(n) class Solution: def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: ans=[] commonEleCount=0 n=len(A) sett=set() for i in range(n): if(A[i] in sett): commonEleCount+=1 else: sett.add(A[i]) if(B[i] in sett): commonEleCount+=1 else: sett.add(B[i]) ans.append(commonEleCount) return ans #i=0 commonEleCount=4 ans=[0,2,3,4] #set={1,3,2,4}
@darshankumar554613 күн бұрын
# python - not optimal class Solution: def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]: ans=[] n=len(A) setA=set() setB=set() for i in range(n): setA.add(A[i]) setB.add(B[i]) if(A[i]==B[i] and i>0): ans.append(ans[-1]+1) else: ans.append(len(setA&setB)) return ans