We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5a5bbab commit 225d19eCopy full SHA for 225d19e
product-of-array-except-self/mike2ox.ts
@@ -0,0 +1,25 @@
1
+/**
2
+ * source: https://leetcode.com/problems/product-of-array-except-self/
3
+ * 풀이방법: 왼쪽부터의 누적 곱과 오른쪽부터의 누적 곱을 이용하여 결과 계산
4
+ * 시간복잡도: O(n) (n: nums의 길이)
5
+ * 공간복잡도: O(1) (상수 공간만 사용)
6
+ */
7
+function productExceptSelf(nums: number[]): number[] {
8
+ const n = nums.length;
9
+ const result = new Array(n);
10
+
11
+ // 왼쪽부터의 누적 곱 계산
12
+ result[0] = 1;
13
+ for (let i = 1; i < n; i++) {
14
+ result[i] = result[i - 1] * nums[i - 1];
15
+ }
16
17
+ // 오른쪽부터의 누적 곱을 곱하면서 결과 계산
18
+ let right = 1;
19
+ for (let i = n - 1; i >= 0; i--) {
20
+ result[i] = result[i] * right;
21
+ right *= nums[i];
22
23
24
+ return result;
25
+}
0 commit comments