Make Two Arrays Equal by Reversing Sub-arrays Solutions in C++
Number 1460
Difficulty Easy
Acceptance 73.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/// Author : Hao Chen// Date : 2020-10-02class Solution {public:// This problem could transfer to check two array has exactly same numbersbool canBeEqual(vector<int>& target, vector<int>& arr) {int len1 = target.size();int len2 = arr.size();if (len1 != len2) return false;if (len1 == 0) return true;unordered_map<int, int> stat;for (int i=0; i<len1; i++) {stat[target[i]]++;stat[arr[i]]--;}for (auto& s:stat) {if (s.second !=0 ) return false;}return true;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays//// Author : liuyubobobo/// Time : 2020-05-30#include <iostream>#include <vector>using namespace std;/// Sorting/// Time Complexity: O(nlogn)/// Space Complexity: O(1)class Solution {public:bool canBeEqual(vector<int>& target, vector<int>& arr) {sort(target.begin(), target.end());sort(arr.begin(), arr.end());return target == arr;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays//// Author : liuyubobobo/// Time : 2020-05-30#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Using Hash Map/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:bool canBeEqual(vector<int>& target, vector<int>& arr) {unordered_map<int, int> f1, f2;for(int i = 0; i < target.size(); i ++)f1[target[i]] ++, f2[arr[i]] ++;return f1 == f2;}};int main() {return 0;}