当前位置:网站首页>LeetCode 1640. Can I connect to form an array
LeetCode 1640. Can I connect to form an array
2022-07-06 16:43:00 【Daylight629】
1640. Can you join to form an array
Give you an array of integers arr , Every integer in the array Different from each other . There's another array of integers pieces, The integers are also Different from each other . Please use In any order Connect pieces In order to form arr . however , Don't allow For each array pieces[i] Reorder integers in .
If you can connect pieces The array in the form of arr , return true ; otherwise , return false .
Example 1:
Input :arr = [85], pieces = [[85]]
Output :true
Example 2:
Input :arr = [15,88], pieces = [[88],[15]]
Output :true
explain : Connect in turn [15] and [88]
Example 3:
Input :arr = [49,18,16], pieces = [[16,18,49]]
Output :false
explain : Even if the numbers match , You can't rearrange pieces[0]
Example 4:
Input :arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
Output :true
explain : Connect in turn [91]、[4,64] and [78]
Example 5:
Input :arr = [1,3,5,7], pieces = [[2,4,6,8]]
Output :false
Tips :
1 <= pieces.length <= arr.length <= 100sum(pieces[i].length) == arr.length1 <= pieces[i].length <= arr.length1 <= arr[i], pieces[i][j] <= 100arrThe integer Different from each otherpiecesThe integer Different from each other ( in other words , If you willpiecesFlatten it into a one-dimensional array , All integers in an array are different from each other )
Two 、 Method 1
Hash
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
Map<Integer, int[]> map = new HashMap<>();
for (int[] piece : pieces) {
map.put(piece[0], piece);
}
for (int i = 0; i < arr.length;) {
int cur = arr[i];
if (map.containsKey(cur)) {
int[] pos = map.get(cur);
for (int x : pos) {
if (arr[i] == x) {
i++;
} else {
return false;
}
}
} else {
return false;
}
}
return true;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- MP4格式详解
- (lightoj - 1349) Aladdin and the optimal invitation (greed)
- 软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- Cmake error: could not create named generator visual studio 16 2019 solution
- Codeforces round 797 (Div. 3) no f
- Summary of game theory
- Chapter 2 shell operation of hfds
- One hundred questions of image processing (11-20)
- SQL quick start
猜你喜欢

QT simulates mouse events and realizes clicking, double clicking, moving and dragging

Kubernetes集群部署

业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事

Submit several problem records of spark application (sparklauncher with cluster deploy mode)

CMake速成

软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客

Chapter 2 shell operation of hfds

Chapter 6 datanode

Gridhome, a static site generator that novices must know

Li Kou - 298th weekly match
随机推荐
我在字节跳动「修电影」
Tree of life (tree DP)
QT implementation fillet window
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
第7章 __consumer_offsets topic
第5章 NameNode和SecondaryNameNode
业务系统从Oracle迁移到openGauss数据库的简单记录
Market trend report, technical innovation and market forecast of China's desktop capacitance meter
Educational Codeforces Round 122 (Rated for Div. 2)
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Generate random password / verification code
Kubernetes集群部署
MariaDB的安装与配置
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Simple records of business system migration from Oracle to opengauss database
Li Kou - 298th weekly match
Codeforces Round #771 (Div. 2)
LeetCode 1552. Magnetic force between two balls