File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ Approach
3+ - ๋ฐฐ์ด์ 0์ด 2๊ฐ ์ด์ ์๋ ๊ฒฝ์ฐ, ๋ชจ๋ ์์๋ 0์ด ๋๋ฏ๋ก ๋ฐ๋ก ๋ต์ ๋ฐํ
4+ - "self(์ ์ฒด ๊ณฑ์์ ์ ์ธ๋ ๊ฐ)"์ ์ธ๋ฑ์ค๋ฅผ ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ(before)๊ณผ ์ค๋ฅธ์ชฝ(after)๊ณฑ์ ๋ฏธ๋ฆฌ ๊ณ์ฐํด์ ๋ฐฐ์ด์ ์ ์ฅ
5+ - ์ต์ข
์ ์ผ๋ก before์ after ๋ฐฐ์ด์ ๊ฐ์ ์ธ๋ฑ์ค ๊ฐ์ ๊ณฑํด์ ๋ฐํ
6+
7+ Time Complexity: O(N)
8+ - after, before ๋ฐฐ์ด์ ๊ฐ๊ฐ ํ ๋ฒ์ฉ ์ํํ๋ฉฐ ๊ณ์ฐ
9+ - ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ ๋ ๋ ๋ฐฐ์ด์ ๊ณฑ์ ๊ณ์ฐ
10+ Space Complexity: O(N)
11+ - N์ num์ ๊ธธ์ด์ด๋ฉฐ Nํฌ๊ธฐ์ ๋น๋กํ๋ before, after ๋ฐฐ์ด ์์ฑ ๊ณต๊ฐ
12+ '''
13+ from typing import List
14+
15+ class Solution :
16+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
17+ if nums .count (0 ) > 1 :
18+ return [0 ] * len (nums )
19+
20+ before = [1 ] * len (nums )
21+ for idx in range (len (nums )- 1 ):
22+ before [idx + 1 ] = before [idx ] * nums [idx ]
23+
24+ after = [1 ] * len (nums )
25+ for jdx in range (len (nums )- 1 , 0 , - 1 ):
26+ after [jdx - 1 ] = after [jdx ] * nums [jdx ]
27+
28+ return [x * y for x , y in zip (before , after )]
You canโt perform that action at this time.
0 commit comments