Skip to content

Commit 21c536e

Browse files
authored
Merge pull request #215 from yolophg/main
[Helena] Week 14 solutions
2 parents 29177b5 + ce8fdc3 commit 21c536e

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

set-matrix-zeroes/yolophg.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Time Complexity: O(rows * cols)
2+
// Space Complexity: O(1)
3+
4+
var setZeroes = function (matrix) {
5+
// number of rows in the matrix
6+
const rows = matrix.length;
7+
// number of cols in the matrix
8+
const cols = matrix[0].length;
9+
// to check if the first row has any zeros
10+
let rowZero = false;
11+
// to check if the first col has any zeros
12+
let colZero = false;
13+
14+
// check if the first row has any zeros
15+
for (let c = 0; c < cols; c++) {
16+
if (matrix[0][c] === 0) {
17+
rowZero = true;
18+
break;
19+
}
20+
}
21+
22+
// check if the first col has any zeros
23+
for (let r = 0; r < rows; r++) {
24+
if (matrix[r][0] === 0) {
25+
colZero = true;
26+
break;
27+
}
28+
}
29+
30+
// use the first row and col to mark zeros
31+
for (let r = 1; r < rows; r++) {
32+
for (let c = 1; c < cols; c++) {
33+
if (matrix[r][c] === 0) {
34+
// mark corresponding col in first row
35+
matrix[0][c] = 0;
36+
// mark corresponding row in first col
37+
matrix[r][0] = 0;
38+
}
39+
}
40+
}
41+
42+
// set matrix elements to zero based on markers in the first row and col
43+
for (let r = 1; r < rows; r++) {
44+
for (let c = 1; c < cols; c++) {
45+
if (matrix[0][c] === 0 || matrix[r][0] === 0) {
46+
matrix[r][c] = 0;
47+
}
48+
}
49+
}
50+
51+
// handle the first row if there was any zero
52+
if (rowZero) {
53+
for (let c = 0; c < cols; c++) {
54+
matrix[0][c] = 0;
55+
}
56+
}
57+
58+
// handle the first col if there was any zero
59+
if (colZero) {
60+
for (let r = 0; r < rows; r++) {
61+
matrix[r][0] = 0;
62+
}
63+
}
64+
};

spiral-matrix/yolophg.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(m*n) m = number of rows, n = number of cols
2+
// Space Complexity: O(m*n)
3+
4+
var spiralOrder = function (matrix) {
5+
let result = [];
6+
7+
while (matrix.length > 0) {
8+
// add the first row to the result
9+
result = result.concat(matrix.shift());
10+
11+
// add the last element of each remaining row to the result
12+
for (let i = 0; i < matrix.length; i++) {
13+
if (matrix[i].length > 0) {
14+
result.push(matrix[i].pop());
15+
}
16+
}
17+
18+
// add the last row in reverse order to the result, if any rows are left
19+
if (matrix.length > 0) {
20+
result = result.concat(matrix.pop().reverse());
21+
}
22+
23+
// add the first element of each remaining row to the result in reverse order
24+
for (let i = matrix.length - 1; i >= 0; i--) {
25+
if (matrix[i].length > 0) {
26+
result.push(matrix[i].shift());
27+
}
28+
}
29+
}
30+
31+
// return the result array containing the elements in spiral order
32+
return result;
33+
};

sum-of-two-integers/yolophg.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time Complexity: O(log max(a, b))
2+
// Space Complexity: O(log max(a, b))
3+
4+
var getSum = function (a, b) {
5+
// if there is no carry, return a as the result
6+
if (b === 0) return a;
7+
8+
// calculate the sum without carry using XOR
9+
let sum = a ^ b;
10+
11+
// calculate the carry using AND and left shift
12+
let carry = (a & b) << 1;
13+
14+
// recursively call getSum with sum and carry
15+
return getSum(sum, carry);
16+
};

0 commit comments

Comments
 (0)