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
- 85 lines of Python from the credited upstream file split-and-merge-array-transformation.py.
- The implementation visibly relies on sequence storage.
- No explicit 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(object):6 def minSplitMerge(self, nums1, nums2):7 """8 :type nums1: List[int]9 :type nums2: List[int]10 :rtype: int11 """12 def bfs(start, target):13 def adj(arr):14 for l in xrange(len(arr)):15 for r in xrange(l, len(arr)):16 sub = arr[l:r+1]17 rem = arr[:l]+arr[r+1:]18 for i in xrange(len(rem)+1):19 if i == l:20 continue21 yield rem[:i]+sub+rem[i:]22 23 d = 024 if start == target:25 return d26 lookup = {start}27 q = [start]28 d += 129 while q:30 new_q = []31 for u in q:32 for v in adj(u):33 if v in lookup:34 continue35 if v == target:36 return d37 lookup.add(v)38 new_q.append(v)39 q = new_q40 d += 141 return -142 43 return bfs(tuple(nums1), tuple(nums2))44 45 46474849class Solution2(object):50 def minSplitMerge(self, nums1, nums2):51 """52 :type nums1: List[int]53 :type nums2: List[int]54 :rtype: int55 """56 def bfs(start, target):57 def adj(arr):58 for l in xrange(len(arr)):59 for r in xrange(l, len(arr)):60 sub = arr[l:r+1]61 rem = arr[:l]+arr[r+1:]62 for i in xrange(len(rem)+1):63 if i == l:64 continue65 yield rem[:i]+sub+rem[i:]66 67 d = 068 lookup = {start}69 q = [start]70 while q:71 new_q = []72 for u in q:73 if u == target:74 return d75 for v in adj(u):76 if v in lookup:77 continue78 lookup.add(v)79 new_q.append(v)80 q = new_q81 d += 182 return -183 84 return bfs(tuple(nums1), tuple(nums2))85