래원

[LeetCode] 2657. Find the Prefix Common Array of Two Arrays (Python) 본문

알고리즘/LeetCode

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

Laewon Jeong 2025. 1. 14. 17:32

 

난이도: Medium

 

문제 설명


You are given two 0-indexed integer permutations A and B of length n.

 

prefix common array of A and B is an array C such that C[i] is equal to the count of numbers that are present at or before the index i in both A and B.

 

Return the prefix common array of A and B.

 

A sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.

 

문제 예제


Example 1:

Input: A = [1,3,2,4], B = [3,1,2,4]
Output: [0,2,3,4]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.

Example 2:

Input: A = [2,3,1], B = [3,1,2]
Output: [0,1,3]
Explanation: At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.

 

제한 사항

  • 1 <= A.length == B.length == n <= 50
  • 1 <= A[i], B[i] <= n
  • It is guaranteed that A and B are both a permutation of n integers.

 

✏️ Solution(솔루션) 1

class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        max_a = max(A)
        max_b = max(B)
        check = [0 for _ in range(max(max_a, max_b)+1)]

        answer = []
        temp = 0
        for num_a, num_b in zip(A, B):
            check[num_a] += 1
            if check[num_a] == 2:
                temp += 1
                
            check[num_b] += 1
            if check[num_b] == 2:
                temp += 1
            answer.append(temp)
            
        return answer

 

이것 저것 해보다가 A의 숫자와 B의 숫자에 대해 순서대로 +1 해주면서 그 숫자가 2가 되면 조건을 만족하는 것을 확인했다.

따라서 만약 그 숫자가 2가 된다고 하면 temp를 +1 해주었고, 이를 answer 리스트에 저장했다.

 

마지막에 이를 return하여 정답을 맞출 수 있었다.

 

아래 솔루션들은 시간은 좀 걸리지만 다른 방식으로 풀어본 코드이다.

✏️ Solution(솔루션) 2

class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        max_a = max(A)
        max_b = max(B)
        check = [0 for _ in range(max(max_a, max_b)+1)]

        answer = []
        for num_a, num_b in zip(A, B):
            check[num_a] += 1
            check[num_b] += 1
            answer.append(check.count(2))
            
        return answer

 

✏️ Solution(솔루션) 3

class Solution:
    def findThePrefixCommonArray(self, A: List[int], B: List[int]) -> List[int]:
        check_a = Counter()
        check_b = Counter()

        answer = []

        for num_a, num_b in zip(A, B):
            check_a[num_a] += 1
            check_b[num_b] += 1

            answer.append(list((check_a + check_b).values()).count(2))

        return answer

문제: 2657. Find the Prefix Common Array of Two Arrays

 

깃허브: github

 

algorithmPractice/LeetCode/2766-find-the-prefix-common-array-of-two-arrays at main · laewonJeong/algorithmPractice

하루 한 문제 챌린지. Contribute to laewonJeong/algorithmPractice development by creating an account on GitHub.

github.com