File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
BFS/3690.Split-and-Merge-Array-Transformation Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int minSplitMerge (vector<int >& nums1, vector<int >& nums2) {
4+ if (nums1==nums2) return 0 ;
5+ int n = nums1.size ();
6+ set<vector<int >>visited;
7+ visited.insert (nums1);
8+
9+ queue<vector<int >>q;
10+ q.push (nums1);
11+ int step = 0 ;
12+
13+ while (!q.empty ()) {
14+ int len = q.size ();
15+ step++;
16+ while (len--) {
17+ auto cur = q.front ();
18+ q.pop ();
19+
20+ for (int L = 0 ; L < n; L++)
21+ for (int R = L; R<n; R++) {
22+ vector<int >sub (cur.begin ()+L, cur.begin ()+R+1 );
23+ vector<int >rem;
24+ rem.insert (rem.end (), cur.begin (), cur.begin ()+L);
25+ rem.insert (rem.end (), cur.begin ()+R+1 , cur.end ());
26+
27+ for (int pos = 0 ; pos<=rem.size (); pos++) {
28+ vector<int >nxt;
29+ nxt.insert (nxt.end (), rem.begin (), rem.begin ()+pos);
30+ nxt.insert (nxt.end (), sub.begin (), sub.end ());
31+ nxt.insert (nxt.end (), rem.begin ()+pos, rem.end ());
32+
33+ if (visited.find (nxt)!=visited.end ()) continue ;
34+ visited.insert (nxt);
35+ if (nxt==nums2) return step;
36+ q.push (move (nxt));
37+ }
38+ }
39+ }
40+ }
41+
42+ return -1 ;
43+
44+ }
45+ };
You can’t perform that action at this time.
0 commit comments