-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Helena] Week 14 solutions #215
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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,64 @@ | ||
// Time Complexity: O(rows * cols) | ||
// Space Complexity: O(1) | ||
|
||
var setZeroes = function (matrix) { | ||
// number of rows in the matrix | ||
const rows = matrix.length; | ||
// number of cols in the matrix | ||
const cols = matrix[0].length; | ||
// to check if the first row has any zeros | ||
let rowZero = false; | ||
// to check if the first col has any zeros | ||
let colZero = false; | ||
|
||
// check if the first row has any zeros | ||
for (let c = 0; c < cols; c++) { | ||
if (matrix[0][c] === 0) { | ||
rowZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// check if the first col has any zeros | ||
for (let r = 0; r < rows; r++) { | ||
if (matrix[r][0] === 0) { | ||
colZero = true; | ||
break; | ||
} | ||
} | ||
|
||
// use the first row and col to mark zeros | ||
for (let r = 1; r < rows; r++) { | ||
for (let c = 1; c < cols; c++) { | ||
if (matrix[r][c] === 0) { | ||
// mark corresponding col in first row | ||
matrix[0][c] = 0; | ||
// mark corresponding row in first col | ||
matrix[r][0] = 0; | ||
} | ||
} | ||
} | ||
|
||
// set matrix elements to zero based on markers in the first row and col | ||
for (let r = 1; r < rows; r++) { | ||
for (let c = 1; c < cols; c++) { | ||
if (matrix[0][c] === 0 || matrix[r][0] === 0) { | ||
matrix[r][c] = 0; | ||
} | ||
} | ||
} | ||
|
||
// handle the first row if there was any zero | ||
if (rowZero) { | ||
for (let c = 0; c < cols; c++) { | ||
matrix[0][c] = 0; | ||
} | ||
} | ||
|
||
// handle the first col if there was any zero | ||
if (colZero) { | ||
for (let r = 0; r < rows; r++) { | ||
matrix[r][0] = 0; | ||
} | ||
} | ||
}; |
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,33 @@ | ||
// Time Complexity: O(m*n) m = number of rows, n = number of cols | ||
// Space Complexity: O(m*n) | ||
|
||
var spiralOrder = function (matrix) { | ||
let result = []; | ||
|
||
while (matrix.length > 0) { | ||
// add the first row to the result | ||
result = result.concat(matrix.shift()); | ||
|
||
// add the last element of each remaining row to the result | ||
for (let i = 0; i < matrix.length; i++) { | ||
if (matrix[i].length > 0) { | ||
result.push(matrix[i].pop()); | ||
} | ||
} | ||
|
||
// add the last row in reverse order to the result, if any rows are left | ||
if (matrix.length > 0) { | ||
result = result.concat(matrix.pop().reverse()); | ||
} | ||
|
||
// add the first element of each remaining row to the result in reverse order | ||
for (let i = matrix.length - 1; i >= 0; i--) { | ||
if (matrix[i].length > 0) { | ||
result.push(matrix[i].shift()); | ||
} | ||
} | ||
} | ||
|
||
// return the result array containing the elements in spiral order | ||
return result; | ||
}; |
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,16 @@ | ||
// Time Complexity: O(log max(a, b)) | ||
// Space Complexity: O(log max(a, b)) | ||
|
||
var getSum = function (a, b) { | ||
// if there is no carry, return a as the result | ||
if (b === 0) return a; | ||
|
||
// calculate the sum without carry using XOR | ||
let sum = a ^ b; | ||
|
||
// calculate the carry using AND and left shift | ||
let carry = (a & b) << 1; | ||
|
||
// recursively call getSum with sum and carry | ||
return getSum(sum, carry); | ||
}; |
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.
shift()
함수의 시간 복잡도를O(n)
으로 알고 있어요. 맨 앞에 있는 값을 제거한 후에 기존에 배열에 있던 모든 요소를 한 자리씩 왼쪽으로 이동시켜야 하기 때문이죠. 시간 복잡도 분석에 대한 재고가 필요할 것 같습니다.