Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- leetcode
- BFS
- 아파치 하둡
- 이진탐색
- 아파치 스파크
- 도커
- 오블완
- String
- heapq
- 아파치 카프카
- apache kafka
- 리트코드
- 알고리즘
- Python
- 카프카
- 우선순위큐
- 분산처리
- 문자열
- docker
- 그래프
- 코딩테스트
- 티스토리챌린지
- Apache Spark
- 하둡
- programmers
- Apache Hadoop
- 분산
- DP
- 파이썬
- 프로그래머스
Archives
- Today
- Total
래원
[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.
A 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
'알고리즘 > LeetCode' 카테고리의 다른 글
[LeetCode] 2425. Bitwise XOR of All Pairings (Python) (0) | 2025.01.16 |
---|---|
[LeetCode] 2429. Minimize XOR (Python) (0) | 2025.01.15 |
[LeetCode] 3223. Minimum Length of String After Operations (Python) (0) | 2025.01.13 |
[LeetCode] 1400. Construct K Palindrome Strings (Python) (0) | 2025.01.11 |
[LeetCode] 916. Word Subsets (Python) (0) | 2025.01.10 |