Leetcode 1646. Get Maximum in Generated Array

2021-07-01 10:30:36 浏览数 (1)

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

1. Description

2. Solution

  • Version 1
代码语言:javascript复制
class Solution:
    def getMaximumGenerated(self, n: int) -> int:
        if n == 0:
            return 0
        if n == 1:
            return 1
        maximum = 1
        nums = [0 for _ 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
代码语言:javascript复制
class Solution:
    def getMaximumGenerated(self, n: int) -> int:
        if n == 0:
            return 0
        if n == 1:
            return 1
        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

Reference

  1. https://leetcode.com/problems/get-maximum-in-generated-array/

0 人点赞