(진행중) [leetcode] Product of Array Except Self

데린이·2022년 5월 30일
0

자기 자신을 제외한 모든 원소들의 곱들을 산출하시오.
https://leetcode.com/problems/product-of-array-except-self/

  • 단, 나눗셈을 하지 않고 O(n)에 풀이하시오.

22-05-30

import numpy as np

class Solution(object):
    def productExceptSelf(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        answer = np.array([1]*len(nums))
        for i,n in enumerate(nums):
            multiplication = answer*n - answer
            multiplication[i] = 0
            answer += multiplication
        
        return answer
        

array로 각 원소의 곱만큼 더하기 방식으로 진행했다.
단, 해당 원소는 0을 더함으로 조건을 만족하였다.
하지만, Time Out. ㅎㅎ
array 자체로 계산하는 것이 O(n)...

Next to do.
1. 왼쪽 곱셈에 결과에 오른쪽 값을 차례대로 곱셈하는 방식으로 풀자

profile
취뽀를 기원하는 취준생입니다!

0개의 댓글