class Solution(object): def oddEvenJumps(self, A): l = len(A) odd_jump = [-1]*l #odd_jump[i], when odd jump from index i, what index it is going to land even_jump = [-1]*l #even_jump[i], when even jump from index i, what index it is going to land odd = [False]*l #odd[i], can index i, starting by odd jump, go to the end even = [False]*l #even[i], can index i, starting by even jump, go to the end odd[-1] = True even[-1] = True #construct odd_jump stack = [] for n, i in sorted((n, i) for i, n in enumerate(A)): while stack and stack[-1]=A[start] and valn: n = val curr = i return curr opt = set() not_opt = set() end = len(A)-1 for i in xrange(len(A)): if i in opt or i in not_opt: continue odd = True curr = i temp = set() visited = set() temp.add(i) while True: print curr visited.add(i) if curr==None: not_opt.update(temp) break if curr==end: opt.update(temp) break curr = jump(curr, odd) odd = not odd return len(opt)