Skip to content

Commit b335fa4

Browse files
committed
solutions: 1460 - Make Two Arrays Equal by Reversing Subarrays (Easy)
1 parent 6664453 commit b335fa4

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
description: "Author: @wingkwong | https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/"
3+
tags: [Array, Hash Table, Sorting]
4+
---
5+
6+
# 1460 - Make Two Arrays Equal by Reversing Subarrays (Easy)
7+
8+
## Problem Link
9+
10+
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/
11+
12+
## Problem Statement
13+
14+
You are given two integer arrays of equal length `target` and `arr`. In one step, you can select any **non-empty subarray** of `arr` and reverse it. You are allowed to make any number of steps.
15+
16+
Return `true` _if you can make_`arr`_equal to_`target`_or_`false`_otherwise_.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: target = [1,2,3,4], arr = [2,4,1,3]
22+
Output: true
23+
Explanation: You can follow the next steps to convert arr to target:
24+
1- Reverse subarray [2,4,1], arr becomes [1,4,2,3]
25+
2- Reverse subarray [4,2], arr becomes [1,2,4,3]
26+
3- Reverse subarray [4,3], arr becomes [1,2,3,4]
27+
There are multiple ways to convert arr to target, this is not the only way to do so.
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: target = [7], arr = [7]
34+
Output: true
35+
Explanation: arr is equal to target without any reverses.
36+
```
37+
38+
**Example 3:**
39+
40+
```
41+
Input: target = [3,7,9], arr = [3,7,11]
42+
Output: false
43+
Explanation: arr does not have value 9 and it can never be converted to target.
44+
```
45+
46+
**Constraints:**
47+
48+
- `target.length == arr.length`
49+
- `1 <= target.length <= 1000`
50+
- `1 <= target[i] <= 1000`
51+
- `1 <= arr[i] <= 1000`
52+
53+
## Approach 1: Sorting
54+
55+
- Time Complexity: $O(n log n)$
56+
- Space Complexity: $O(log n)$
57+
58+
<Tabs>
59+
<TabItem value="py" label="Python">
60+
<SolutionAuthor name="@wingkwong"/>
61+
62+
```cpp
63+
class Solution:
64+
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
65+
return sorted(target) == sorted(arr)
66+
```
67+
68+
</TabItem>
69+
</Tabs>

0 commit comments

Comments
 (0)