小米笔试编程题
昨天做了小米笔试,编程题真挺简单的,基本都做过
第一题:连续最大子序列和
s=list(map(int,input().split())) def fun(s): length=len(s) dp=[0]*length dp[0]=max(0,s[0]) for i in range(1,length): if dp[i-1]<0: dp[i]=s[i] else: dp[i]=dp[i-1]+s[i] print(max(dp)) fun(s)第二题:股票交易的最大收益(最多只允许交易两次)
s=list(map(int,input().split())) def fun(s): T_i10,T_i11=0,-1000000 T_i20,T_i21=0,-1000000 for i in s: T_i20=max(T_i20,T_i21+i) T_i21=max(T_i21,T_i10-i) T_i10=max(T_i10,T_i11+i) T_i11=max(T_i11,-i) print(T_i20) fun(s)