Make Two Arrays Equal by Reversing Subarrays — LeetCode 1460 Python Solution
EasyArrayHash TableSorting
- Problem
- #1460
- Pattern
- Sorting
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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.
Example
- 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:
Python solution
Python
class Solution:
def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
return sorted(target) == sorted(arr)Complexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n) |
| Space | O(\log n), where n is the length of the array arr auxiliary |
Pattern: Sorting
Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.
The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays?
- LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays?
- The Python solution on this page runs in O(n \times \log n).
- What is the space complexity of LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays?
- The Python solution on this page uses O(\log n), where n is the length of the array arr auxiliary space.
- What topics does LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays cover?
- LeetCode 1460. Make Two Arrays Equal by Reversing Subarrays is tagged Array, Hash Table and Sorting on LeetCode.