Bunker

[medium] 238. Product of Array Except Self 본문

Algorithms/Leetcode

[medium] 238. Product of Array Except Self

_Ishmael 2022. 7. 25. 21:23

문제 *7/24 풀이본

https://leetcode.com/problems/product-of-array-except-self/

 

Product of Array Except Self - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

특이사항

Product of - '곱셈'이라는 뜻을 상기함.

Division을 사용하지 말라는 요구사항을 제대로 읽지 못함. 요구사항을 제대로 읽을 것.

returnSize를 넣어주지 않으면 Caller가 Expected Output을 제대로 출력할 수 없음. 유념할 것

 

풀이

앞으로부터의 누적곱 (=acc1)

뒤에서부터의 누적곱 (=acc2)

이 두 개를 조합해서 자신을 제외하는 방식으로 작성했는데, extra space를 사용하고도 O(n)알고리즘임.

O(1) 알고리즘으로 어떻게 구현해야 할지 모르겠음

 

코드 (*#if 안쪽은 division을 이용한 알고리즘임)

https://github.com/suy-lee/algorithm/blob/master/LeetCode_FirstMissingPositive.c

'Algorithms > Leetcode' 카테고리의 다른 글

[easy] 771. Jewels and Stones  (0) 2022.08.03
[medium] 54. Spiral Matrix  (0) 2022.08.02
[hard] 41. First Missing Positive  (0) 2022.07.25
Youtube Searching Script (Javascript)  (0) 2018.05.07