Problem solution · Python

Minimum Generations to Target Point

Minimum Generations to Target Point: a Python 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
111 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 Minimum Generations to Target Point, 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

  • 111 lines of Python from the credited upstream file minimum-generations-to-target-point.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.

Source

Code and credit

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

Full codeMinimum Generations to Target Point · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(7^6)# Space: O(7^3) # bfsclass Solution(object):    def minGenerations(self, points, target):        """        :type points: List[List[int]]        :type target: List[int]        :rtype: int        """        def encode(p):            return p[0]*7*7+p[1]*7+p[2]            lookup = [False]*(7**3)        k = total = 0        for p in points:            if lookup[encode(p)]:                continue            if p == target:                return k            lookup[encode(p)] = True        i = 0        while i < len(points):            if i == total:                total = len(points)                k += 1            for j in xrange(i):                p = [(points[i][0]+points[j][0])//2, (points[i][1]+points[j][1])//2, (points[i][2]+points[j][2])//2]                if lookup[encode(p)]:                    continue                if p == target:                    return k                lookup[encode(p)] = True                points.append(p)            i += 1        return -1  # Time:  O(7^6)# Space: O(7^3)# bfsclass Solution2(object):    def minGenerations(self, points, target):        """        :type points: List[List[int]]        :type target: List[int]        :rtype: int        """        def encode(p):            return p[0]*7*7+p[1]*7+p[2]            lookup = [False]*(7**3)        for p in points:            if lookup[encode(p)]:                continue            lookup[encode(p)] = True        i = k = 0        while i < len(points):            if lookup[encode(target)]:                return k            total = len(points)            while i < total:                for j in xrange(i):                    p = [(points[i][0]+points[j][0])//2, (points[i][1]+points[j][1])//2, (points[i][2]+points[j][2])//2]                    if lookup[encode(p)]:                        continue                    lookup[encode(p)] = True                    points.append(p)                i += 1            k += 1        return -1  # Time:  O(7^6)# Space: O(7^3)# bfsclass Solution3(object):    def minGenerations(self, points, target):        """        :type points: List[List[int]]        :type target: List[int]        :rtype: int        """        def encode(p):            return p[0]*7*7+p[1]*7+p[2]            q = []        lookup = [False]*(7**3)        for p in points:            if lookup[encode(p)]:                continue            lookup[encode(p)] = True            q.append(p)        k = 0        while q:            if lookup[encode(target)]:                return k            new_q = []            for i in xrange(len(points)-len(q), len(points)):                for j in xrange(i):                    p = [(points[i][0]+points[j][0])//2, (points[i][1]+points[j][1])//2, (points[i][2]+points[j][2])//2]                    if lookup[encode(p)]:                        continue                    lookup[encode(p)] = True                    new_q.append(p)            points.extend(new_q)            q = new_q            k += 1        return -1 

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 ↗