1460. Make Two Arrays Equal by Reversing Subarrays
Description
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.
Return true
if you can make arr
equal to target
or false
otherwise.
Example 1:
Input: target = [1,2,3,4], arr = [2,4,1,3] Output: true Explanation: You can follow the next steps to convert arr to target: 1- Reverse subarray [2,4,1], arr becomes [1,4,2,3] 2- Reverse subarray [4,2], arr becomes [1,2,4,3] 3- Reverse subarray [4,3], arr becomes [1,2,3,4] There are multiple ways to convert arr to target, this is not the only way to do so.
Example 2:
Input: target = [7], arr = [7] Output: true Explanation: arr is equal to target without any reverses.
Example 3:
Input: target = [3,7,9], arr = [3,7,11] Output: false Explanation: arr does not have value 9 and it can never be converted to target.
Constraints:
target.length == arr.length
1 <= target.length <= 1000
1 <= target[i] <= 1000
1 <= arr[i] <= 1000
Solutions
Solution 1: Sorting
If two arrays are equal after sorting, then they can be made equal by reversing sub-arrays.
Therefore, we only need to sort the two arrays and then check if the sorted arrays are equal.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$, where $n$ is the length of the array $arr$.
Python3
class Solution:
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
return sorted(target) == sorted(arr)
Java
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
Arrays.sort(target);
Arrays.sort(arr);
return Arrays.equals(target, arr);
}
}
C++
class Solution {
public:
bool canBeEqual(vector<int>& target, vector<int>& arr) {
sort(target.begin(), target.end());
sort(arr.begin(), arr.end());
return target == arr;
}
};
Go
func canBeEqual(target []int, arr []int) bool {
sort.Ints(target)
sort.Ints(arr)
return reflect.DeepEqual(target, arr)
}
TypeScript
function canBeEqual(target: number[], arr: number[]): boolean {
target.sort();
arr.sort();
return target.every((x, i) => x === arr[i]);
}
JavaScript
function canBeEqual(target, arr) {
target.sort();
arr.sort();
return target.every((x, i) => x === arr[i]);
}
Rust
impl Solution {
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
target.sort();
arr.sort();
target == arr
}
}
PHP
class Solution {
/**
* @param Integer[] $target
* @param Integer[] $arr
* @return Boolean
*/
function canBeEqual($target, $arr) {
sort($target);
sort($arr);
return $target === $arr;
}
}
C
int compare(const void* a, const void* b) {
return (*(int*) a - *(int*) b);
}
bool canBeEqual(int* target, int targetSize, int* arr, int arrSize) {
qsort(target, targetSize, sizeof(int), compare);
qsort(arr, arrSize, sizeof(int), compare);
for (int i = 0; i < targetSize; ++i) {
if (target[i] != arr[i]) {
return false;
}
}
return true;
}
Solution 2: Counting
We note that the range of the array elements given in the problem is $1 \sim 1000$. Therefore, we can use two arrays cnt1
and cnt2
of length $1001$ to record the number of times each element appears in the arrays target
and arr
respectively. Finally, we just need to check if the two arrays are equal.
We can also use only one array cnt
. We traverse the arrays target
and arr
. For target[i]
, we increment cnt[target[i]]
, and for arr[i]
, we decrement cnt[arr[i]]
. In the end, we check if all elements in the array cnt
are $0$.
The time complexity is $O(n + M)$, and the space complexity is $O(M)$. Here, $n$ is the length of the array arr
, and $M$ is the range of the array elements. In this problem, $M = 1001$.
Python3
class Solution:
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
return Counter(target) == Counter(arr)
Java
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
int[] cnt1 = new int[1001];
int[] cnt2 = new int[1001];
for (int v : target) {
++cnt1[v];
}
for (int v : arr) {
++cnt2[v];
}
return Arrays.equals(cnt1, cnt2);
}
}
C++
class Solution {
public:
bool canBeEqual(vector<int>& target, vector<int>& arr) {
vector<int> cnt1(1001);
vector<int> cnt2(1001);
for (int& v : target) {
++cnt1[v];
}
for (int& v : arr) {
++cnt2[v];
}
return cnt1 == cnt2;
}
};
Go
func canBeEqual(target []int, arr []int) bool {
cnt1 := make([]int, 1001)
cnt2 := make([]int, 1001)
for _, v := range target {
cnt1[v]++
}
for _, v := range arr {
cnt2[v]++
}
return reflect.DeepEqual(cnt1, cnt2)
}
TypeScript
function canBeEqual(target: number[], arr: number[]): boolean {
const n = target.length;
const cnt = Array(1001).fill(0);
for (let i = 0; i < n; i++) {
cnt[target[i]]++;
cnt[arr[i]]--;
}
return cnt.every(v => !v);
}
JavaScript
function canBeEqual(target, arr) {
const n = target.length;
const cnt = Array(1001).fill(0);
for (let i = 0; i < n; i++) {
cnt[target[i]]++;
cnt[arr[i]]--;
}
return cnt.every(v => !v);
}
Rust
impl Solution {
pub fn can_be_equal(mut target: Vec<i32>, mut arr: Vec<i32>) -> bool {
let n = target.len();
let mut cnt = [0; 1001];
for i in 0..n {
cnt[target[i] as usize] += 1;
cnt[arr[i] as usize] -= 1;
}
cnt.iter().all(|v| *v == 0)
}
}