小米,算法笔试,秋招
第一题: 连续数组最大和
if __name__ == "__main__": num = [int(x) for x in input().strip().split(' ')] res = num[0] s = num[0] for i in range(1, len(num)): if s > 0: s += num[i] res = max(res, s) else: s = num[i] res = max(res, s) print(res)
第二题:最大利润
def maxProfit(p): n = len(p) if n == 0: return 0 f = [0]*n minv = float('inf') for i in range(n): if i != 0: f[i] = f[i-1] if p[i] > minv: f[i] = max(f[i], p[i]-minv) minv = min(minv, p[i]) res = f[n-1] maxv = -float('inf') for i in range(n-1, 0, -1): if p[i] < maxv: res = max(res, maxv-p[i]+f[i-1]) maxv = max(maxv, p[i]) return res if __name__ == "__main__": p = [int(x) for x in input().strip().split(' ')] print(maxProfit(p))题目简单的让人觉得就是走走形式,跟本不招人