File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ import math
2
+
3
+ class Solution :
4
+ """
5
+ 1. ์ธ๋ฑ์ค ์ฌ๋ผ์ด์ฑ์ ํ์ฉํ ํ์ด
6
+ - ์ธ๋ฑ์ค ์ฌ๋ผ์ด์ฑ์ ํ์ฉํ์ฌ ํด๋น ์ธ๋ฑ์ค๋ฅผ ์ ์ธํ ๋๋จธ์ง ์์๋ค์ ๊ณฒ์ math.prod()๋ฅผ ์ด์ฉํ์ฌ ๋ฐฐ์ด์ ์ฝ์
7
+ - ์๊ฐ์ด๊ณผ O(n^2)
8
+ 2. DP Bottom Up ๋ฐฉ์์ผ๋ก ํ์ด
9
+ - ์ธ๋ฑ์ค i์ ๋ํ ์ผ์ชฝ ๋ถ๋ถ๊ณผ ์ค๋ฅธ์ชฝ ๋ถ๋ถ์ ๊ณฑ์ ๊ณ์ฐํ๋ ๋ฐฉ์
10
+ - ์๊ฐ ๋ณต์ก๋ O(n)
11
+ - ๊ณต๊ฐ ๋ณต์ก๋ O(1)
12
+ """
13
+ # 1๋ฒ ํ์ด
14
+ # def productExceptSelf(self, nums: List[int]) -> List[int]:
15
+ # answer = [1] * len(n)
16
+
17
+ # for i in range(len(nums)):
18
+ # answer.append(math.prod(nums[:i] + nums[i+1:]))
19
+
20
+ # return answer
21
+
22
+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
23
+ dp = [1 ] * len (nums )
24
+
25
+ left = 1
26
+ for i in range (len (nums )):
27
+ dp [i ] = left
28
+ left *= nums [i ]
29
+
30
+ right = 1
31
+ for i in range (len (nums ) - 1 , - 1 , - 1 ):
32
+ dp [i ] *= right
33
+ right *= nums [i ]
34
+ return dp
You canโt perform that action at this time.
0 commit comments