Skip to content

[Sehwan] Week14 solution with JavaScript #210

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 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions set-matrix-zeroes/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var setZeroes = function (matrix) {
let rows = new Set();
let cols = new Set();

for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] === 0) {
rows.add(row);
cols.add(col);
}
}
}

for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[0].length; col++) {
if (rows.has(row) || cols.has(col)) {
matrix[row][col] = 0;
}
}
}
};

// TC: O(m*n)
// SC: O(m+n)
45 changes: 45 additions & 0 deletions spiral-matrix/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var spiralOrder = function (matrix) {
// Edge case
if (matrix.length === 0) return [];

let result = [];
let rowStart = 0,
rowEnd = matrix.length - 1;
let colStart = 0,
colEnd = matrix[0].length - 1;

while (rowStart <= rowEnd && colStart <= colEnd) {
// Traverse right
for (let col = colStart; col <= colEnd; col++) {
result.push(matrix[rowStart][col]);
}
rowStart++;

// Traverse down
for (let row = rowStart; row <= rowEnd; row++) {
result.push(matrix[row][colEnd]);
}
colEnd--;

// Traverse left (check if rowStart <= rowEnd to avoid duplicates)
if (rowStart <= rowEnd) {
for (let col = colEnd; col >= colStart; col--) {
result.push(matrix[rowEnd][col]);
}
rowEnd--;
}

// Traverse up (check if colStart <= colEnd to avoid duplicates)
if (colStart <= colEnd) {
for (let row = rowEnd; row >= rowStart; row--) {
result.push(matrix[row][colStart]);
}
colStart++;
}
}

return result;
};

// TC: O(m*n)
// SC: O(m*n)
16 changes: 16 additions & 0 deletions sum-of-two-integers/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var getSum = function (a, b) {
while (b !== 0) {
if (b > 0) {
a++;
b--;
} else {
b++;
a--;
}
}
return a;
Comment on lines +2 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와... 이게 λͺ¨μ£ ? γ…‹γ…‹γ…‹

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λΉ„νŠΈ μ—°μ‚°μžλ‘œ ν’€μ—ˆμ–΄μ•Ό 좜제자의 μ˜λ„μ— λΆ€ν•©ν•˜λŠ”κ±΄λ° γ…Žγ…Ž 이런 풀이도 μžˆλ”λΌκ³ μš”

};

// |b| is absolute value of b
// TC: O(|b|)
// SC: O(1)