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 c497f74 commit 7278a49Copy full SHA for 7278a49
โproduct-of-array-except-self/uraflower.jsโ
@@ -0,0 +1,19 @@
1
+/**
2
+ * ์ฃผ์ด์ง ๋ฐฐ์ด์์ ์๊ธฐ ์์ ์ ์ ์ธํ๊ณ ๋๋จธ์ง ์์๋ฅผ ๊ณฑํ ๊ฐ์ผ๋ก ๊ตฌ์ฑ๋ ๋ฐฐ์ด์ ๋ฐํํ๋ ํจ์
3
+ * @param {number[]} nums
4
+ * @return {number[]}
5
+ */
6
+const productExceptSelf = function(nums) {
7
+ const dp1 = []; // left to right
8
+ const dp2 = []; // right to left
9
+
10
+ for (let i = 0, j = nums.length - 1; i < nums.length; i++, j--) {
11
+ dp1[i] = (dp1[i-1] ?? 1) * nums[i];
12
+ dp2[j] = (dp2[j+1] ?? 1) * nums[j];
13
+ }
14
15
+ return nums.map((_, i) => (dp1[i-1] ?? 1) * (dp2[i+1] ?? 1));
16
+};
17
18
+// ์๊ฐ๋ณต์ก๋: O(n)
19
+// ๊ณต๊ฐ๋ณต์ก๋: O(n) --> dp ๋ฐฐ์ด์ ํ๋๋ง ์ฐ๋ ๋ฐฉ์์ผ๋ก ๊ฐ์ ํ ์ ์์ ๋ฏ
0 commit comments