当前位置: 代码迷 >> 综合 >> Leetcode 1646. Get Maximum in Generated Array
  详细解决方案

Leetcode 1646. Get Maximum in Generated Array

热度:89   发布时间:2023-12-12 21:09:10.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Get Maximum in Generated Array

2. Solution

  • Version 1
class Solution:def getMaximumGenerated(self, n: int) -> int:if n == 0:return 0if n == 1:return 1maximum = 1nums = [0 for _ in range(n+1)]nums[1] = 1for 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
class Solution:def getMaximumGenerated(self, n: int) -> int:if n == 0:return 0if n == 1:return 1maximum = 1nums = [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

Reference

  1. https://leetcode.com/problems/get-maximum-in-generated-array/
  相关解决方案