Problem solving/Algorithms

[LeetCode] 1646. Get Maximum in Generated Array (Python)

Young_A 2021. 1. 16. 03:47

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 1646. Get Maximum in Generated Array

    Problem Description

    You are given an integer n. An array nums of length n + 1 is generated in the following way:

     

    nums[0] = 0

    nums[1] = 1

    nums[2 * i] = nums[i] when 2 <= 2 * i <= n

    nums[2 * i + 1] = nums[i] + nums[i + 1] when 2 <= 2 * i + 1 <= n

    Return the maximum integer in the array numsโ€‹โ€‹โ€‹.

     

    Example 1:

    • Input: n = 7
    • Output: 3
    • Explanation: According to the given rules:
    •   nums[0] = 0
    •   nums[1] = 1
    •   nums[(1 * 2) = 2] = nums[1] = 1
    •   nums[(1 * 2) + 1 = 3] = nums[1] + nums[2] = 1 + 1 = 2
    •   nums[(2 * 2) = 4] = nums[2] = 1
    •   nums[(2 * 2) + 1 = 5] = nums[2] + nums[3] = 1 + 2 = 3
    •   nums[(3 * 2) = 6] = nums[3] = 2
    •   nums[(3 * 2) + 1 = 7] = nums[3] + nums[4] = 2 + 1 = 3
    • Hence, nums = [0,1,1,2,1,3,2,3], and the maximum is 3.

    Example 2:

    • Input: n = 2
    • Output: 1
    • Explanation: According to the given rules, the maximum between nums[0], nums[1], and nums[2] is 1.

    Example 3:

    • Input: n = 3
    • Output: 2
    • Explanation: According to the given rules, the maximum between nums[0], nums[1], nums[2], and nums[3] is 2.

    Constraints:

    • 0 <= n <= 100

    My Solution (Python)

    class Solution(object):
        def getMaximumGenerated(self, n):
            """
            :type n: int
            :rtype: int
            """
            if n == 0:
                return 0
            elif n == 1:
                return 1
            nums = []       # empty array nums
            nums.append(0)  # nums[0]
            nums.append(1)  # nums[1]
    
            index = 2           # index for nums
            while index <= n:
                quotient = int(index / 2)
                remainder = index % 2
                if remainder == 0:  # index is even number
                    nums.append(nums[quotient])
                else:               # index is odd number
                    nums.append(nums[quotient] + nums[quotient + 1])
                index += 1
            return max(nums)
    

    ์„ค๋ช…์ด ์กฐ๊ธˆ ๋ณต์žกํ•ด๋ณด์ด์ง€๋งŒ ๋ถ„์„ํ•˜๊ณ  ๋ณด๋ฉด ์‰ฝ๋‹ค.

    ํ”ผ๋ณด๋‚˜์น˜์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋’ค์— ์˜ฌ ์ˆซ์ž๋ฅผ ๊ณ„์‚ฐํ•˜๋ฉด ๋œ๋‹ค.

    ๋ฐฐ์—ด์˜ ๊ทœ์น™์€ ํฌ๊ฒŒ ๋‘๊ฐ€์ง€:

    1. index number๊ฐ€ ํ™€์ˆ˜์ผ ๊ฒฝ์šฐ, index number๊ฐ€ ๊ทธ ์ˆ˜๋ฅผ ๋‚˜๋ˆˆ ๋ชซ์ธ nums array์˜ ์š”์†Œ์™€, ๊ทธ ๋‹ค์Œ ์š”์†Œ๋ฅผ ๋”ํ•œ ๊ฐ’
    2. index number๊ฐ€ ์ง์ˆ˜์ผ ๊ฒฝ์šฐ,  index number๊ฐ€ ๊ทธ ์ˆ˜๋ฅผ ๋‚˜๋ˆˆ ๋ชซ์ธ nums array์˜ ์š”์†Œ์˜ ๊ฐ’

    ํ’€์ด๊ณผ์ •:

    1. nums[0]๊ณผ nums[1]์˜ ๊ฐ’์€ ์ด๋ฏธ ์ •ํ•ด์ ธ์žˆ์œผ๋‹ˆ n์ด 0 ํ˜น์€ 1์ธ์ง€ ํ™•์ธํ•œ ํ›„ ์ •ํ•ด์ง„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    2. 0์ด๋‚˜ 1์ด ์•„๋‹ ๊ฒฝ์šฐ nums array๋ฅผ ์œ„ํ•œ ๋นˆ ๋ฐฐ์—ด์„ ์ž‘์„ฑํ•œ๋‹ค. ์ •ํ•ด์ง„ nums[0]๊ณผ nums[1]์˜ ๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
    3. index๋Š” ํ˜„์žฌ ๋ฐฐ์—ด์— ์ง€์ •๋œ ๋งˆ์ง€๋ง‰ ๊ฐ’์˜ ๋‹ค์Œ ์š”์†Œ๋ฅผ ์œ„ํ•œ ์ธ๋ฑ์Šค ๋„˜๋ฒ„. 0๊ณผ 1์€ ์ด๋ฏธ ์ฑ„์› ์œผ๋‹ˆ 2๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
    4. ๋ฐ˜๋ณต๋ฌธ: index๊ฐ€ n๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ์ž‘์„๋•Œ๊นŒ์ง€
      1. index๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
      2. index๊ฐ€ ์ง์ˆ˜์ผ๊ฒฝ์šฐ nums[๋ชซ]์„ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค.
      3. index๊ฐ€ ํ™€์ˆ˜์ผ๊ฒฝ์šฐ nums[๋ชซ]๊ณผ nums[๋ชซ+1] ๊ฐ’์„ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค.
      4. ๋ฐ˜๋ณต์„ ๋๋งˆ์น˜๊ธฐ ์ „์— index ๋„˜๋ฒ„๋ฅผ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
    5. nums array์˜ ์ตœ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    'Problem solving > Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [LeetCode] 7. Reverse Integer (C#)  (0) 2021.01.23
    [LeetCode] 1. Two Sum (C#)  (0) 2021.01.21
    [LeetCode]Daily Challenge: Boats to Save People (Python)  (0) 2021.01.13
    [LeetCode] Daily Challenge: Add Two Numbers (Python)  (0) 2021.01.13
    [LeetCode] Daily Challenge Merge Sorted Array (Python)  (0) 2021.01.12