Skip to content
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

[선재] WEEK07 문제풀이 #493

Merged
merged 6 commits into from
Sep 28, 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
22 changes: 22 additions & 0 deletions longest-substring-without-repeating-characters/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @description
* brainstorming:
* hash table + two pointer
*
* n = length of s
* time complexity: O(n)
* space complexity: O(n)
*/
var lengthOfLongestSubstring = function (s) {
const map = new Map();
let answer = 0;
let start = 0;

for (let i = 0; i < s.length; i++) {
if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start);
map.set(s[i], i);
answer = Math.max(answer, i - start + 1);
}

return answer;
};
49 changes: 49 additions & 0 deletions number-of-islands/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @description
* brainstorming:
* hash table + two pointer
*
* n = length of grid
* k = length of grid[index]
* time complexity: O(n * k)
* space complexity: O(n * k)
*/
var numIslands = function (grid) {
let answer = 0;
const visited = Array.from({ length: grid.length }, (_, i) =>
Array.from({ length: grid[i].length }, () => false)
);

const dfs = (r, c) => {
const dr = [0, 1, 0, -1];
const dc = [1, 0, -1, 0];

for (let i = 0; i < 4; i++) {
const nextR = r + dr[i];
const nextC = c + dc[i];

if (
nextR >= 0 &&
nextR < grid.length &&
nextC >= 0 &&
nextC < grid[r].length &&
grid[nextR][nextC] == 1 &&
!visited[nextR][nextC]
) {
visited[nextR][nextC] = true;
dfs(nextR, nextC);
}
}
};

for (let row = 0; row < grid.length; row++) {
for (let column = 0; column < grid[row].length; column++) {
if (grid[row][column] == 1 && !visited[row][column]) {
visited[row][column] = true;
answer++;
dfs(row, column);
}
}
}
return answer;
};
25 changes: 25 additions & 0 deletions reverse-linked-list/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @description
* brainstorming:
* Thinking of stacking nodes like stacks while traveling
*
* time complexity: O(n)
* space complexity: O(n)
*/

var reverseList = function (head) {
let answer = null;

const buildReverseList = (target) => {
if (target === null) return;

const node = new ListNode(target.val, answer);
answer = node;

buildReverseList(target.next);
};

buildReverseList(head);

return answer;
};
46 changes: 46 additions & 0 deletions set-matrix-zeroes/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @description
* brainstorming:
* memoization
*
* m: length of matrix
* n: length of matrix[i]
* time complexity: O(m * n)
* space complexity: O(m * n)
*/
var setZeroes = function (matrix) {
const stack = [];
const memo = { row: new Set(), column: new Set() };
Copy link
Contributor

Choose a reason for hiding this comment

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

stack이나 memo중 하나만 사용하셨어도 되지 않았을까요? 최적화를 위해 두 가지를 모두 사용하신걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맞아요. 동일한 크기인 matrix를 만들어서 memo를 활용하는 방법도 가능하나, stack이나 queue와 같이 0인지점을 저장하고 모두 소모하는 방식이 조금이라도 공간을 덜 사용하지 않을까 싶어서 선택했어요.


const setZero = ({ r, c, isRow, isColumn }) => {
const length = isRow ? matrix.length : matrix[0].length;

for (let i = 0; i < length; i++) {
const row = isRow ? i : r;
const column = isColumn ? i : c;
matrix[row][column] = 0;
}
};

matrix.forEach((row, r) => {
row.forEach((value, c) => {
if (value === 0) stack.push([r, c]);
});
});

while (stack.length) {
const [r, c] = stack.pop();

if (!memo.row.has(r)) {
setZero({ r, c, isColumn: true });
memo.row.add(r);
}

if (!memo.column.has(c)) {
setZero({ r, c, isRow: true });
memo.column.add(c);
}
}

return matrix;
};
25 changes: 25 additions & 0 deletions unique-paths/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @description
* brainstorming:
* 1. dfs -> time limited
* 2. dynamic programming
*
* time complexity: O(m * n)
* space complexity: O(m * n)
*/

var uniquePaths = function (m, n) {
// initialize
const dp = Array.from({ length: m }, (_, i) =>
Array.from({ length: n }, (_, j) => (i === 0 || j === 0 ? 1 : 0))
);

for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
// recurrence relation
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}

return dp[m - 1][n - 1];
};