Problem solution · C++

Split and Merge Array Transformation

Split and Merge Array Transformation: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
114 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Split and Merge Array Transformation, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 114 lines of C++ from the credited upstream file split-and-merge-array-transformation.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 12 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeSplit and Merge Array Transformation · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n^4 * n!)// Space: O(n * n!) // bfsclass Solution {public:    int minSplitMerge(vector<int>& nums1, vector<int>& nums2) {        const auto& bfs = [](const auto& start, const auto& target) {            const auto& join = [](const auto& vals, const char delim = ',') {                string result;                for (const auto& x : vals) {                    result += to_string(x);                    result.push_back(delim);                }                return result;            };             int d = 0;            if (start == target) {                return d;            }            unordered_set<string> lookup = {join(start)};            vector<vector<int>> q = {start};            ++d;            for (; !empty(q); ++d) {                vector<vector<int>> new_q;                for (const auto& u : q) {                    for (int l = 0; l < size(u); ++l) {                        for (int r = l; r < size(u); ++r) {                            vector<int> sub(cbegin(u) + l, cbegin(u) + (r + 1));                            vector<int> rem(cbegin(u) , cbegin(u) + l);                            rem.insert(end(rem), cbegin(u) + (r + 1), cend(u));                            for (int i = 0; i <= size(rem); ++i) {                                if (i == l) {                                    continue;                                }                                vector<int> v = rem;                                v.insert(begin(v) + i, cbegin(sub), cend(sub));                                const auto& key = join(v);                                if (lookup.count(key)) {                                    continue;                                }                                if (v == target) {                                    return d;                                }                                lookup.emplace(key);                                new_q.emplace_back(v);                            }                        }                    }                }                q = move(new_q);            }            return -1;        };         return bfs(nums1, nums2);    }}; // Time:  O(n^4 * n!)// Space: O(n * n!)// bfsclass Solution2 {public:    int minSplitMerge(vector<int>& nums1, vector<int>& nums2) {        const auto& bfs = [](const auto& start, const auto& target) {            const auto& join = [](const vector<int>& vals, const char delim = ',') {                string result;                for (const auto& x : vals) {                    result += to_string(x);                    result.push_back(delim);                }                return result;            };             unordered_set<string> lookup = {join(start)};            vector<vector<int>> q = {start};            for (int d = 0; !empty(q); ++d) {                vector<vector<int>> new_q;                for (const auto& u : q) {                    if (u == target) {                        return d;                    }                    for (int l = 0; l < size(u); ++l) {                        for (int r = l; r < size(u); ++r) {                            vector<int> sub(cbegin(u) + l, cbegin(u) + (r + 1));                            vector<int> rem(cbegin(u) , cbegin(u) + l);                            rem.insert(end(rem), cbegin(u) + (r + 1), cend(u));                            for (int i = 0; i <= size(rem); ++i) {                                if (i == l) {                                    continue;                                }                                vector<int> v = rem;                                v.insert(begin(v) + i, cbegin(sub), cend(sub));                                const auto& key = join(v);                                if (lookup.count(key)) {                                    continue;                                }                                lookup.emplace(key);                                new_q.emplace_back(v);                            }                        }                    }                }                q = move(new_q);            }            return -1;        };         return bfs(nums1, nums2);    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗