retry__'s blog

By retry__, history, 5 years ago, In English

given a number x, we have to reach from 1 to x on number line and at each position i we can move to i+1 or to reverse of number i(ignoring the leading zeroes, say from 23 to 32). Find the minimum number of steps to reach x. x<=1e14

  • Vote: I like it
  • -17
  • Vote: I do not like it

»
5 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it
  • Hint $$$1$$$ The answer is $$$\lt 200$$$
Why ?
  • Hint $$$2$$$ BFS
How ?
  • Hint $$$3$$$ Greedy
How ?

P.S. — How do I did spoiler tags in my comment ?

»
5 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

let's start at x and move to 1 ,the best strategy will be always to move x%10 moves while you are greater than 10 then reverse ,as if you are dividing by 10 in x%10 moves but reversing the digits each time. let x=987, then you move 7 moves then reverse,now x=89 then move 9 moves then reverse , x=8 since you are smaller than 10 the best strategy is to move normally to 1 so the answer will be the sum of digits of x — 1 ,the minus 1 because we start from 1 not 0

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it +11 Vote: I do not like it

    This is incorrect, because you can move from $$$980$$$ to $$$89$$$, but questions asks you to start from $$$1$$$ and go to $$$x$$$. You won't be able to move from $$$89$$$ to $$$980$$$, so this path is not reversible.