当前位置:网站首页>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 <= 100
sum(pieces[i].length) == arr.length
1 <= pieces[i].length <= arr.length
1 <= arr[i], pieces[i][j] <= 100
arr
The integer Different from each otherpieces
The integer Different from each other ( in other words , If you willpieces
Flatten 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).
边栏推荐
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- 软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
- Summary of game theory
- Bisphenol based CE Resin Industry Research Report - market status analysis and development prospect forecast
- Hbuilder x format shortcut key settings
- Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
- Cmake error: could not create named generator visual studio 16 2019 solution
- The concept of spark independent cluster worker and executor
- Spark独立集群动态上线下线Worker节点
- LeetCode 1552. Magnetic force between two balls
猜你喜欢
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
(lightoj - 1369) answering queries (thinking)
Chapter 6 rebalance details
顺丰科技智慧物流校园技术挑战赛(无t4)
Audio and video development interview questions
第7章 __consumer_offsets topic
(lightoj - 1323) billiard balls (thinking)
Chapter 6 datanode
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
LeetCode 1560. The sector with the most passes on the circular track
随机推荐
(lightoj - 1354) IP checking (Analog)
Acwing: the 56th weekly match
Chapter 5 namenode and secondarynamenode
Summary of game theory
Simply try the new amp model of deepfacelab (deepfake)
第五章 Yarn资源调度器
Research Report on market supply and demand and strategy of Chinese table lamp industry
FLV格式详解
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
Two weeks' experience of intermediate software designer in the crash soft exam
Codeforces Round #803 (Div. 2)A~C
腾讯面试算法题
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
本地可视化工具连接阿里云centOS服务器的redis
Kubernetes cluster deployment
Codeforces Round #801 (Div. 2)A~C
解决Intel12代酷睿CPU单线程只给小核运行的问题
Hbuilder X格式化快捷键设置
Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)