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


  • 0
    X

    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 {
    public:
        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;
                else
                    seq_trap_count++;
    
            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.