Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
We don't neet to perform a BFS. We just looking for connection. If previous containt next.

lejafilip

Dec 11, 2024

Code:

    bool sequenceReconstruction(const vector<int>& nums, const vector<vector<int>>& sequences) {         std::unordered_map<int, std::vector<int>> adjList;         for(auto i = 0; i < sequences.size(); ++i)         {             for(auto j = 0; j < sequences[i].size() - 1; ++j)             {                 adjList[sequences[i][j]].push_back(sequences[i][j + 1]);             }         }         for(auto i = 0; i < nums.size() - 1; ++i)         {             if(!adjList.contains(nums[i]) || std::ranges::find(adjList[nums[i]], nums[i + 1]) == std::ranges::end(adjList[nums[i]]))                 return false;         }         return true;     }

0

0

Comments
Comments

On this page