문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
서로 다른 정수로 이루어진 배열 arr과 각 정수가 서로 다른 정수 배열들의 배열 pieces가 주어진다. 목표는 pieces 배열에 어떤 순서로든 연결하여 배열 arr을 만드는 것이다. 단, 각 배열 pieces[i]의 순서는 변경할 수 없다.
배열 pieces의 요소들을 연결하여 배열 arr을 만들 수 있다면 true, 그렇지 않다면 false를 반환해라.
#1
Input: arr = [15, 88], pieces = [[88], [15]]
Output: true
Explanation: [15]와 [88]을 연결한다.
#2
Input: arr = [49, 18, 16], pieces = [[16, 18, 49]]
Output: false
Explanation: 숫자는 일지하지만, pieces[0]을 재정렬할 수 없다.
#3
Input: arr = [91, 4, 64, 78], pices = [[78], [4, 64], [91]]
Output: true
Explanation: [91] 다음 [4, 64] 다음 [78]을 연결하면 된다.
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
int[][] map = new int[101][];
for (int[] p : pieces) {
map[p[0]] = p;
}
for (int i = 0; i < arr.length;) {
int e = arr[i];
if (map[e] == null) {
return false;
}
int[] part = map[e];
for (int j = 1; j < part.length; j++) {
if (arr[i + j] != part[j]) {
return false;
}
}
i += part.length;
}
return true;
}
}