← Home

1460. Make Two Arrays Equal by Reversing Sub-arrays

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
greedyC++Markdown
146

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1460. Make Two Arrays Equal by Reversing Sub-arrays, the solution in this repository is mainly a greedy solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: greedy.

The notes already sitting in the source point us in the right direction:

  • counter
  • time: O(N)

Guide

When?

This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are canBeEqual.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(NlogN)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//counter
02//Runtime: 36 ms, faster than 75.00% of C++ online submissions for Make Two Arrays Equal by Reversing Sub-arrays.
03//Memory Usage: 15.5 MB, less than 100.00% of C++ online submissions for Make Two Arrays Equal by Reversing Sub-arrays.
04//time: O(N)
05class Solution {
06public:
07    bool canBeEqual(vector<int>& target, vector<int>& arr) {
08        map<int, int> counter;
09        
10        for(int e : target){
11            counter[e]+=1;
12        }
13        
14        for(int e : arr){
15            if(counter.find(e) == counter.end()){
16                return false;
17            }
18            counter[e]-=1;
19            if(counter[e] == 0){
20                counter.erase(e);
21            }
22        }
23        
24        return true;
25    }
26};
27
28//sorting
29//Runtime: 28 ms, faster than 75.00% of C++ online submissions for Make Two Arrays Equal by Reversing Sub-arrays.
30//Memory Usage: 13.8 MB, less than 100.00% of C++ online submissions for Make Two Arrays Equal by Reversing Sub-arrays.
31//time: O(NlogN)
32class Solution {
33public:
34    bool canBeEqual(vector<int>& target, vector<int>& arr) {
35        sort(target.begin(), target.end());
36        sort(arr.begin(), arr.end());
37        return target == arr;
38    }
39};

Cost

Complexity

Time
O(NlogN)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.