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.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- 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.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int minSplitMerge(vector<int>& nums1, vector<int>& nums2) {8 const auto& bfs = [](const auto& start, const auto& target) {9 const auto& join = [](const auto& vals, const char delim = ',') {10 string result;11 for (const auto& x : vals) {12 result += to_string(x);13 result.push_back(delim);14 }15 return result;16 };17 18 int d = 0;19 if (start == target) {20 return d;21 }22 unordered_set<string> lookup = {join(start)};23 vector<vector<int>> q = {start};24 ++d;25 for (; !empty(q); ++d) {26 vector<vector<int>> new_q;27 for (const auto& u : q) {28 for (int l = 0; l < size(u); ++l) {29 for (int r = l; r < size(u); ++r) {30 vector<int> sub(cbegin(u) + l, cbegin(u) + (r + 1));31 vector<int> rem(cbegin(u) , cbegin(u) + l);32 rem.insert(end(rem), cbegin(u) + (r + 1), cend(u));33 for (int i = 0; i <= size(rem); ++i) {34 if (i == l) {35 continue;36 }37 vector<int> v = rem;38 v.insert(begin(v) + i, cbegin(sub), cend(sub));39 const auto& key = join(v);40 if (lookup.count(key)) {41 continue;42 }43 if (v == target) {44 return d;45 }46 lookup.emplace(key);47 new_q.emplace_back(v);48 }49 }50 }51 }52 q = move(new_q);53 }54 return -1;55 };56 57 return bfs(nums1, nums2);58 }59};60 61626364class Solution2 {65public:66 int minSplitMerge(vector<int>& nums1, vector<int>& nums2) {67 const auto& bfs = [](const auto& start, const auto& target) {68 const auto& join = [](const vector<int>& vals, const char delim = ',') {69 string result;70 for (const auto& x : vals) {71 result += to_string(x);72 result.push_back(delim);73 }74 return result;75 };76 77 unordered_set<string> lookup = {join(start)};78 vector<vector<int>> q = {start};79 for (int d = 0; !empty(q); ++d) {80 vector<vector<int>> new_q;81 for (const auto& u : q) {82 if (u == target) {83 return d;84 }85 for (int l = 0; l < size(u); ++l) {86 for (int r = l; r < size(u); ++r) {87 vector<int> sub(cbegin(u) + l, cbegin(u) + (r + 1));88 vector<int> rem(cbegin(u) , cbegin(u) + l);89 rem.insert(end(rem), cbegin(u) + (r + 1), cend(u));90 for (int i = 0; i <= size(rem); ++i) {91 if (i == l) {92 continue;93 }94 vector<int> v = rem;95 v.insert(begin(v) + i, cbegin(sub), cend(sub));96 const auto& key = join(v);97 if (lookup.count(key)) {98 continue;99 }100 lookup.emplace(key);101 new_q.emplace_back(v);102 }103 }104 }105 }106 q = move(new_q);107 }108 return -1;109 };110 111 return bfs(nums1, nums2);112 }113};114