Skip to content

[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 2 commits into from
Aug 5, 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
64 changes: 64 additions & 0 deletions set-matrix-zeroes/yolophg.js
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;
}
}
};
33 changes: 33 additions & 0 deletions spiral-matrix/yolophg.js
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());
Copy link
Member

@DaleSeo DaleSeo Aug 3, 2024

Choose a reason for hiding this comment

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

shift() 함수의 시간 복잡도를 O(n)으로 알고 있어요. 맨 앞에 있는 값을 제거한 후에 기존에 배열에 있던 모든 요소를 한 자리씩 왼쪽으로 이동시켜야 하기 때문이죠. 시간 복잡도 분석에 대한 재고가 필요할 것 같습니다.

}
}
}

// return the result array containing the elements in spiral order
return result;
};
16 changes: 16 additions & 0 deletions sum-of-two-integers/yolophg.js
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);
};