-
Notifications
You must be signed in to change notification settings - Fork 0
Product Of Array Except Self #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yuto729
wants to merge
1
commit into
main
Choose a base branch
from
product-of-array-except-self
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # 238. Product of Array Except Self | ||
|
|
||
| Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. | ||
|
|
||
| The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. | ||
|
|
||
| You must write an algorithm that runs in O(n) time and without using the division operation. | ||
|
|
||
| Example 1: | ||
| Input: nums = [1,2,3,4] | ||
| Output: [24,12,8,6] | ||
|
|
||
| Example 2: | ||
| Input: nums = [-1,1,0,-3,3] | ||
| Output: [0,0,9,0,0] | ||
|
|
||
| Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) | ||
|
|
||
| ## Step1 | ||
|
|
||
| 割り算を使ってはいけないので、掛け算だけで達成することを考える。 | ||
| O(n)で解きたいので、まず配列の要素を順番に舐めていく。現在の要素より前の要素の積を計算していけば一部は計算できる。残りの要素をかける必要があるが、配列を逆から舐めていって同じように今まで出てきた要素の積を取って先ほど計算した値にかければ、「自分以外の要素の積」が計算できそう。 | ||
| 時間計算量O(n)かつ空間計算量O(1)で解ける。 | ||
|
|
||
| 前からみるのと後ろからみるのを組み合わせる方法はよく見かける | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| ans = [1] * len(nums) | ||
| left = 1 | ||
| for i in range(len(nums)): | ||
| ans[i] = left | ||
| left *= nums[i] | ||
|
|
||
| right = 1 | ||
| for i in range(len(nums) - 1, -1, -1): | ||
| ans[i] = ans[i] * right | ||
| right *= nums[i] | ||
|
|
||
| return ans | ||
| ``` | ||
|
|
||
| test | ||
|
|
||
| - 0じゃないケースは問題なし | ||
| - 0が1つ入ってる時 -> left, rightが途中から0になる。0の位置の時だけleftかつrightが0ではない | ||
| - 0が2つ以上入ってる時 -> left もしくは rightが常に0 | ||
|
|
||
| ## Step2 | ||
|
|
||
| - https://github.com/kazuki-official/leetcode/pull/100 | ||
| 空間計算量がO(1)じゃないバージョン。意図がわかりやすい | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| prefix_product = [1] * len(nums) | ||
| for i in range(1, len(nums)): | ||
| prefix_product[i] = prefix_product[i - 1] * nums[i - 1] | ||
|
|
||
| suffix_product = [1] * len(nums) | ||
| for i in range(len(nums) - 2, -1, -1): | ||
| suffix_product[i] = suffix_product[i + 1] * nums[i + 1] | ||
|
|
||
| product_except_self = [None] * len(nums) | ||
| for i in range(len(nums)): | ||
| product_except_self[i] = prefix_product[i] * suffix_product[i] | ||
|
|
||
| return product_except_self | ||
| ``` | ||
|
|
||
| - https://github.com/tom4649/Coding/pull/121 | ||
| 以下の命名がわかりやすい | ||
| left -> prefix_product | ||
| right -> suffix_product | ||
|
|
||
| ## Step3 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
| product_except_self = [1] * len(nums) | ||
| prefix_product = 1 | ||
| for i in range(len(nums)): | ||
| product_except_self[i] = prefix_product | ||
| prefix_product *= nums[i] | ||
|
|
||
| suffix_product = 1 | ||
| for i in range(len(nums) - 1, -1, -1): | ||
| product_except_self[i] *= suffix_product | ||
| suffix_product *= nums[i] | ||
|
|
||
| return product_except_self | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらのコメントをご参照ください。
tom4649/Coding#147 (comment)
今回の場合は product_except_self はいかがでしょうか。関数名と被っている点に違和感はあるのですが…。