Skip to content

Commit 8f0f654

Browse files
committed
solved : proudct-of-array-except-self
1 parent 9e6582b commit 8f0f654

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ์ •์ˆ˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ์œ„์น˜ i์— ๋Œ€ํ•ด
2+
# nums[i]๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ์š”์†Œ์˜ ๊ณฑ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ
3+
4+
'''
5+
nums = [1, 2, 3, 4]
6+
answers = [24, 12, 8, 6]
7+
answers[0] = 2 * 3 * 4
8+
answers[1] = 1 * 3 * 4
9+
answers[2] = 1 * 2 * 4
10+
answers[3] = 1 * 2 * 3
11+
12+
์กฐ๊ฑด : O(n) ์‹œ๊ฐ„ ๋ณต์žก๋„
13+
14+
์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ˆ„์  ๊ณฑ ๊ณ„์‚ฐ
15+
์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ, ์˜ค๋ฅธ์ชฝ ๋ˆ„์  ๊ณฑ์„ ๊ณ„์‚ฐ
16+
์™ผ์ชฝ ๋ˆ„์  ๊ณฑ๊ณผ ๊ณฑํ•˜์—ฌ ์ตœ์ข… ๊ฒฐ๊ณผ ์—…๋ฐ์ดํŠธ
17+
18+
'''
19+
def productExceptSelf(self, nums: List[int]) -> List[int]:
20+
n = len(nums)
21+
result = [1] * n
22+
left = 1
23+
right = 1
24+
# ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ˆ„์  ๊ณฑ ๊ณ„์‚ฐ
25+
for i in range(n):
26+
result[i] = left
27+
left *= nums[i]
28+
# ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ๋ˆ„์  ๊ณฑ ๊ณ„์‚ฐ
29+
# ์—ญ์ˆœ์œผ๋กœ ๋ฐ˜๋ณตํ•˜๋Š” ๊ตฌ๋ฌธ, ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒ
30+
# range(start, stop, step) => ๋ฐ˜๋ณต์„ ์‹œ์ž‘ํ•  ๊ฐ’, ๋ฐ˜๋ณต์„ ๋ฉˆ์ถœ ๊ฐ’, ๋ฐ˜๋ณต์˜ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ ๋‹จ์œ„
31+
for i in range(n - 1, -1, -1):
32+
result[i] *= right
33+
right *= nums[i]
34+
return result

0 commit comments

Comments
ย (0)