Easy to understand C++ solution based on # of consecutive "traps"

  • 0

    I also post my DP solution, which yields TLE for very long input but gives me the insight to come up w/ the solution below.

    class Solution {
        bool canJump(int A[], int n) {
            // # of sequential/consecutive elements unable to reach last
            int seq_trap_count = 0;
            for (int i = n - 2; i >= 0; i--)
                // have to jump more than seq_trap_count
                if (A[i] > seq_trap_count)
                    seq_trap_count = 0;
            return (0 == seq_trap_count);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.