Posted on

Description

Submission

class Solution {
public:
    bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
        map<int, int> Map;
        for(int i = 0; i < arr.size(); ++i) {
            Map[arr[i]] = i;
        }
        
        vector<int> arr2;
        
        for(auto& piece : pieces) {
            for(auto& p : piece) {
                arr2.push_back(p);
            }
        }
        
        if(arr.size() != arr2.size()) return false;
        
        sort(arr.begin(), arr.end());
        sort(arr2.begin(), arr2.end());
        
        for(int i = 0; i < arr.size(); ++i) {
            if(arr[i] != arr2[i]) return false;
        }
        
        for(auto& piece : pieces) {
            if(piece.size() == 1) continue;
            for(int i = 1; i < piece.size(); ++i) {
                if(Map[piece[i]] - Map[piece[i-1]] != 1) return false;
            }
        }
        
        return true;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *