Solving the Problem Backwards: Leetcode 780 Reaching points

The naive solution got TLE

Solve it backward

Since our begin point can only be a number greater than 1, we can solve the problem backward. Detail can be seen from the following figure. A naive way to solve the problem backward, we still get TLE, although we can pass more test cases.

Image is from HERE

This solution gets AC by introducing the mod operation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store