Can any one help me to improve the performance of my solution?


  • 0
    M

    `

    class Solution:
        # @param A, a list of integers
        # @return a boolean
        def canJump(self, A):
            #tag: greedy
            #thought: when index is i, we need to find the maxJump between i+1 and i+A[i],
            #then update the index i to the maxjump's index
            #then we need to assert the result, and iterate it 
            i = 0
            flag = True
            if len(A) == 1:
                return True
            while i < len(A):
                #temp = self.findmax(A,i)
                maxJ = A[i+1]
                index = i + 1
                lastindex = i + A[i] + 1 if i + A[i] + 1 < len(A) else len(A)
                for j in range(i+1, lastindex):
                    if maxJ <= A[j]:
                        index = j
                        maxJ = A[j]
                i = index
                if i == i + A[i] and i < len(A)-1:
                    flag = False
                    break
                if i == i + A[i] and i == len(A)-1:
                    flag = True
                    break
                if i + A[i] >= len(A)-1:
                    flag = True
                    break
    
            return flag
    

    `


Log in to reply
 

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