-
Notifications
You must be signed in to change notification settings - Fork 126
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
[나리] WEEK 03 Solutions #386
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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,74 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
|
||
/** | ||
* Runtime: 124ms, Memory: 65.06MB | ||
* Time complexity: O(n^2) | ||
* Space complexity: O(n) | ||
* -> answer 배열에 O(n) | ||
* -> splicedArr 배열에 O(n) | ||
*/ | ||
|
||
var productExceptSelf = function (nums) { | ||
const totalProduct = nums.reduce((total, current) => total * current, 1); | ||
const answer = Array(nums.length).fill(0); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
answer[i] = totalProduct / nums[i]; | ||
if (nums[i] === 0) { | ||
const splicedArr = nums.filter(function (_, index) { | ||
return index !== i; | ||
}); | ||
answer[i] = splicedArr.reduce((total, current) => total * current, 1); | ||
} | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
|
||
/** | ||
* 위의 풀이는 0일 때마다 순회를 해야 하기 때문에 해당 부분을 0의 개수를 세어 처리하도록 변경한 풀이 | ||
|
||
* Runtime: 109ms, Memory: 63.45MB | ||
* Time complexity: O(n) | ||
* Space complexity: O(n) | ||
|
||
*/ | ||
var productExceptSelf = function (nums) { | ||
let totalProduct = 1; | ||
let zeroCount = 0; | ||
const answer = Array(nums.length).fill(0); | ||
|
||
for (let numIndex = 0; numIndex < nums.length; numIndex++) { | ||
if (nums[numIndex] === 0) { | ||
zeroCount += 1; | ||
} else { | ||
totalProduct *= nums[numIndex]; | ||
} | ||
} | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (nums[i] === 0) { | ||
if (zeroCount - 1 > 0) { | ||
answer[i] = 0; | ||
} else { | ||
answer[i] = totalProduct; | ||
} | ||
} else { | ||
if (zeroCount > 0) { | ||
answer[i] = 0; | ||
} else { | ||
answer[i] = totalProduct / nums[i]; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
}; |
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.
안녕하세요 나리님, 나누기 연산자를 사용하면 안 된다고 문제에 적혀 있습니다.
You must write an algorithm that runs in O(n) time and without using the division operation.
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.
앗 해당 문장을 못봣네요 ㅜㅜ 😨