当前位置:网站首页>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).
边栏推荐
- Summary of FTP function implemented by qnetworkaccessmanager
- Story of [Kun Jintong]: talk about Chinese character coding and common character sets
- 视频压缩编码和音频压缩编码基本原理
- Spark独立集群Worker和Executor的概念
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
- Codeforces Round #803 (Div. 2)A~C
- 解决Intel12代酷睿CPU单线程只给小核运行的问题
- Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
- Codeforces Round #801 (Div. 2)A~C
猜你喜欢

使用jq实现全选 反选 和全不选-冯浩的博客

Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
![Story of [Kun Jintong]: talk about Chinese character coding and common character sets](/img/d5/9a9e3a0ba57328749d80ec71cb9467.png)
Story of [Kun Jintong]: talk about Chinese character coding and common character sets

Installation and configuration of MariaDB

Cmake Express

MariaDB的安装与配置

Raspberry pie 4B installation opencv3.4.0

Audio and video development interview questions

Basic principles of video compression coding and audio compression coding

Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
随机推荐
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
JS time function Daquan detailed explanation ----- AHAO blog
FLV格式详解
Useeffect, triggered when function components are mounted and unloaded
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
Chapter 5 yarn resource scheduler
Codeforces round 797 (Div. 3) no f
Spark的RDD(弹性分布式数据集)返回大结果集
去掉input聚焦时的边框
Educational Codeforces Round 130 (Rated for Div. 2)A~C
MP4格式详解
Double specific tyrosine phosphorylation regulated kinase 1A Industry Research Report - market status analysis and development prospect prediction
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Acwing: the 56th weekly match
js时间函数大全 详细的讲解 -----阿浩博客
第一章 MapReduce概述
Cmake Express
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
图像处理一百题(1-10)
Summary of game theory