Find minimum number of iterations to reach a certain sum

前端 未结 2 1719
挽巷
挽巷 2021-01-12 11:10

I\'m trying to solve this problem since weeks, but couldn\'t arrive to a solution. You start with two numbers X and Y both equal to 1. Only valid options are X+Y

2条回答
  •  抹茶落季
    2021-01-12 11:23

    If the numbers are not that big (say, below 1000), you can use a breadth-first search.

    Consider a directed graph where each vertex is a pair of numbers (X,Y), and from each such vertex there are two edges to vertices (X+Y,Y) and (X,X+Y). Run a BFS on that graph from (0,0) until you reach any of the positions you need.

提交回复
热议问题