classSolution: defgetMaximumGenerated(self, n: int) -> int: if n == 0: return0 if n == 1: return1 maximum = 1 nums = [0for _ in range(n+1)] nums[1] = 1 for k in range(2, n + 1): i = k // 2 if k % 2 == 0: nums[k] = nums[i] else: nums[k] = nums[i] + nums[i+1] maximum = max(maximum, nums[k]) return maximum
Version 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution: defgetMaximumGenerated(self, n: int) -> int: if n == 0: return0 if n == 1: return1 maximum = 1 nums = [0, 1] for k in range(2, n + 1): i = k // 2 if k % 2 == 0: current = nums[i] else: current = nums[i] + nums[i+1] nums.append(current) maximum = max(maximum, nums[k]) return maximum