1 2 3 4 5 6 7 |
def solution(X, A): pos = {} for i in xrange(len(A)): pos.update({A[i]:i}) if len(pos)==X: return max(pos.values()) return -1 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
def solution(A): max = [-1000,-1000,-1000] min = [1000, 1000] for a in A: if a >= max[0]: max[2] = max[1] max[1] = max[0] max[0] = a elif a >= max[1]: max[2] = max[1] max[1] = a elif a > max[2]: max[2] = a if a <= min[0]: min[1] = min[0] min[0] = a elif a max[0]*min[0]*min[1]: return max[0]*max[1]*max[2] else: return max[0]*min[0]*min[1] |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
def solution(A): n=len(A) if n<3: return 0 peak=[0]*n pre_sum=[0]*(n+1) for i in range(1,n-1): if A[i]>A[i-1] and A[i]>A[i+1]: peak[i]=1 pre_sum[i+1]=pre_sum[i]+peak[i] pre_sum[n]=pre_sum[n-1] #print peak,pre_sum result=1 #check the possible plans to devide the array A plan=[] for i in range(1,n+1): if n%i==0: plan.append(i) for k in plan: checker=True m=0 while m<n/k and checker: if pre_sum[k*(m+1)]-pre_sum[k*m]<1: checker=False m+=1 if checker: return(int(n/k)) return 0 |

Updated by Sailingfish on Aug 19, 2017.

]]>