코딩 공부/Leetcode

[Python] 2657. Find the Prefix Common Array of Two Arrays

일하는 공학도 2025. 1. 14. 11:37
728x90

난이도 : Medium

길이가 n으로 같은 행렬 2개 A,B가 있는데, C[i]는 A[0:i]와 B[0:i]에서 어떤 요소가 같은지 찾으라는 문제

 

class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        n = len(A)
        C = []
        for i in range(n):
            a = list(set(A[0:i+1]) & set(B[0:i+1]))
            C.append(len(a))
        return C

 

리스트 A와 B를 교집합한 값을 C 행렬 뒤에다가 하나씩 붙여넣기했다.

 

Runtime : 52ms (16.41%)

Memory : 17.58MB (49.74%)

728x90