From dfda8954dbad6a7f8bd1828a4fbef4a7836f9b8f Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 8 Jun 2025 22:12:43 +0900 Subject: [PATCH 1/5] feat : #33 add the STEP1/STEP2 --- .../product-of-array-except-self/answer.md | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 blind75/array/product-of-array-except-self/answer.md diff --git a/blind75/array/product-of-array-except-self/answer.md b/blind75/array/product-of-array-except-self/answer.md new file mode 100644 index 0000000..680fa0f --- /dev/null +++ b/blind75/array/product-of-array-except-self/answer.md @@ -0,0 +1,133 @@ +# 238. Product of Array Except Self + +## STEP1 + +### 発想 + +### 想定されるユースケース + +### 何が分からなかったか? + +### 発想 + +* 自分の前までの積と自分の後ろ以降の積を掛け合わせる。 + +```javascript +const productExceptSelf = function(nums) { + const prefixProductForward = new Array(nums.length).fill(0) + const prefixProductReversed = new Array(nums.length).fill(0) + + let currentProduct = 1 + for (let i = 0; i < nums.length; i++) { + currentProduct *= nums[i] + prefixProductForward[i] = currentProduct + } + + currentProduct = 1 + for (let i = nums.length - 1; i >=0; i--) { + currentProduct *= nums[i] + prefixProductReversed[i] = currentProduct + } + + const result = [] + for (let i = 0; i < nums.length; i++) { + if (i === 0) { + result.push(prefixProductReversed[i + 1]) + continue + } + if (i === nums.length - 1) { + result.push(prefixProductForward[i - 1]) + continue + } + result.push(prefixProductForward[i - 1] * prefixProductReversed[i + 1]) + } + return result +}; +``` + +## STEP2 + +```javascript +const productExceptSelf = function(nums) { + const prefixProduct = new Array(nums.length) + const prefixProductReversed = new Array(nums.length) + + let currentProduct = 1 + for (let i = 0; i < nums.length; i++) { + currentProduct *= nums[i] + prefixProduct[i] = currentProduct + } + + let currentProductReversed = 1 + for (let i = nums.length - 1; 0 <= i; i--) { + currentProductReversed *= nums[i] + prefixProductReversed[i] = currentProductReversed + } + + const result = [] + result.push(prefixProductReversed[1]) + for (let i = 1; i < nums.length - 1; i++) { + result.push(prefixProduct[i - 1] * prefixProductReversed[i + 1]) + } + result.push(prefixProduct[nums.length - 2]) + return result +}; +``` + +## STEP3 + +```javascript +``` + +## 感想 + +### コメント集を読んで + +## 他の人のPRを読んで + +## その他の方法 + +* `*1` 番兵を使う方法 + +```javascript +const productExceptSelf = function(nums) { + const prefixProductForward = new Array(nums.length + 2) + const prefixProductReversed = new Array(nums.length + 2) + + prefixProductForward[0] = 1 + prefixProductForward[nums.length + 1] = 1 + prefixProductReversed[0] = 1 + prefixProductReversed[nums.length + 1] = 1 + + for (let i = 0; i < nums.length; i++) { + prefixProductForward[i + 1] = prefixProductForward[i] * nums[i] + } + + for (let i = nums.length - 1; i >=0; i--) { + prefixProductReversed[i + 1] = prefixProductReversed[i + 2] * nums[i] + } + + const result = [] + for (let i = 0; i < nums.length; i++) { + result.push(prefixProductForward[i] * prefixProductReversed[i + 2]) + } + return result +}; +``` + +### コードの良し悪し + +* `*0` + * 時間計算量: + * 空間計算量: + +* `*1` + * 時間計算量: + * 空間計算量: + +* `*2` + * 時間計算量: + * 空間計算量: + +## 調べたこと + From f1b56423bb39e47527c0649671b4d16dc9d9589d Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Jul 2025 11:19:07 +0900 Subject: [PATCH 2/5] feat : #33 add the STEP3g --- .../product-of-array-except-self/answer.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/blind75/array/product-of-array-except-self/answer.md b/blind75/array/product-of-array-except-self/answer.md index 680fa0f..c593c12 100644 --- a/blind75/array/product-of-array-except-self/answer.md +++ b/blind75/array/product-of-array-except-self/answer.md @@ -77,6 +77,36 @@ const productExceptSelf = function(nums) { ## STEP3 ```javascript +const productExceptSelf = function(nums) { + if (nums.length === 1) { + throw new Error("invalid nums length. length should be larger than 1.") + } + if (nums.length === 2) { + return [nums[1], nums[0]] + } + const prefixProduct = new Array(nums.length) + const prefixProductReversed = new Array(nums.length) + + let currentProduct = 1 + for (let i = 0; i < nums.length; i++) { + currentProduct *= nums[i] + prefixProduct[i] = currentProduct + } + let currentProductReversed = 1 + for (let i = nums.length - 1; 0 <= i; i--) { + currentProductReversed *= nums[i] + prefixProductReversed[i] = currentProductReversed + } + + const result = [] + result.push(prefixProductReversed[1]) + for (let i = 1; i < nums.length - 1; i++) { + const productExceptSelf = prefixProduct[i - 1] * prefixProductReversed[i + 1] + result.push(productExceptSelf) + } + result.push(prefixProduct[nums.length - 2]) + return result +}; ``` ## 感想 From 350df36a8235744c1dd33ce8cc87e3e04b0166b3 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Sun, 20 Jul 2025 17:38:30 +0900 Subject: [PATCH 3/5] feat : #33 add one more solution --- .../product-of-array-except-self/answer.md | 56 +++++++++++++++---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/blind75/array/product-of-array-except-self/answer.md b/blind75/array/product-of-array-except-self/answer.md index c593c12..68f1a10 100644 --- a/blind75/array/product-of-array-except-self/answer.md +++ b/blind75/array/product-of-array-except-self/answer.md @@ -4,13 +4,15 @@ ### 発想 +* 自分の前までの積と自分の後ろ以降の積を掛け合わせる。 + ### 想定されるユースケース ### 何が分からなかったか? -### 発想 +* 空間計算量をO(1)で行う方法が、思いつかなかった。 -* 自分の前までの積と自分の後ろ以降の積を掛け合わせる。 +### コード ```javascript const productExceptSelf = function(nums) { @@ -76,6 +78,8 @@ const productExceptSelf = function(nums) { ## STEP3 +* `*1` 累積積を用いた方法 + ```javascript const productExceptSelf = function(nums) { if (nums.length === 1) { @@ -115,9 +119,16 @@ const productExceptSelf = function(nums) { ## 他の人のPRを読んで +- https://github.com/Ryotaro25/leetcode_first60/pull/67 +- https://github.com/rihib/leetcode/pull/21 +- https://github.com/Exzrgs/LeetCode/pull/31 +- https://github.com/t-ooka/leetcode/pull/5 +- https://hayapenguin.com/notes/LeetCode/238/ProductOfArrayExceptSelf + + ## その他の方法 -* `*1` 番兵を使う方法 +* `*2` 累積積で番兵を用いた方法 ```javascript const productExceptSelf = function(nums) { @@ -145,19 +156,44 @@ const productExceptSelf = function(nums) { }; ``` +* `*3` 空間計算量をO(1)で行う方法 + +```javascript +const productExceptSelf = function(nums) { + if (nums.length <= 1) { + throw new Error("array lengths is invalid. nums size should be larger than 1.") + } + const result = new Array(nums.length).fill(1) + + let currentProduct = 1 + for (let i = 1; i < nums.length; i++) { + currentProduct *= nums[i - 1] + result[i] = currentProduct + } + let currentProductReversed = 1 + for (let i = nums.length - 2; 0 <= i; i--) { + currentProductReversed *= nums[i + 1] + result[i] *= currentProductReversed + } + return result +}; +``` + ### コードの良し悪し -* `*0` - * 時間計算量: - * 空間計算量: +numsの配列の長さをNとする。 * `*1` - * 時間計算量: - * 空間計算量: + * 時間計算量: O(N) + * 空間計算量: O(N) * `*2` - * 時間計算量: - * 空間計算量: + * 時間計算量: O(N) + * 空間計算量: O(N) + +* `*3` + * 時間計算量: O(N) + * 空間計算量: O(1) ## 調べたこと From ed199cf7f7bf10aca597f94df9f018ab6e9c0511 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 21 Jul 2025 22:26:54 +0900 Subject: [PATCH 4/5] feat : #33 add one more solution --- .../product-of-array-except-self/answer.md | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/blind75/array/product-of-array-except-self/answer.md b/blind75/array/product-of-array-except-self/answer.md index 68f1a10..d71caaa 100644 --- a/blind75/array/product-of-array-except-self/answer.md +++ b/blind75/array/product-of-array-except-self/answer.md @@ -4,7 +4,16 @@ ### 発想 -* 自分の前までの積と自分の後ろ以降の積を掛け合わせる。 +* 全ての要素に対して、自分以外の積を求める。 +時間計算量O(N^2) +空間計算量はO(N) + +* 最初に全ての要素の積を求めて、各要素に対して割り算を行う。 +問題の条件で割り算をしてはいけないという記載があるため、不可。 +また、要素には0が含まれてうるため、解法としても不可。 + +* 配列の各要素に対して、 +その要素の直前までの積とその要素の後ろ以降の積を掛け合わせる。 ### 想定されるユースケース @@ -179,6 +188,25 @@ const productExceptSelf = function(nums) { }; ``` +- `*4` ナイーブに求める自分以外の席をFor文で計算する方法 + +```javascript +const productExceptSelf = function(nums) { + const result = [] + for (let i = 0; i < nums.length; i++) { + let product = 1 + for (let j = 0; j < nums.length; j++) { + if (i === j) { + continue + } + product *= nums[j] + } + result.push(product) + } + return result +}; +``` + ### コードの良し悪し numsの配列の長さをNとする。 @@ -195,5 +223,9 @@ numsの配列の長さをNとする。 * 時間計算量: O(N) * 空間計算量: O(1) +* `*4` + * 時間計算量: O(N^2) + * 空間計算量: O(1) + ## 調べたこと From d16827b4b1f34185f54f2a7c56a5bed20cbf53e9 Mon Sep 17 00:00:00 2001 From: shintaroyoshida20 Date: Mon, 11 Aug 2025 12:54:10 +0900 Subject: [PATCH 5/5] feat : #33 add the comment --- blind75/array/product-of-array-except-self/answer.md | 1 + 1 file changed, 1 insertion(+) diff --git a/blind75/array/product-of-array-except-self/answer.md b/blind75/array/product-of-array-except-self/answer.md index d71caaa..91e89ee 100644 --- a/blind75/array/product-of-array-except-self/answer.md +++ b/blind75/array/product-of-array-except-self/answer.md @@ -189,6 +189,7 @@ const productExceptSelf = function(nums) { ``` - `*4` ナイーブに求める自分以外の席をFor文で計算する方法 + - Time Limmit Exceeded エラーが発生する。 ```javascript const productExceptSelf = function(nums) {