Skip to content

[uraflower] WEEK 07 solutions #1473

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions longest-substring-without-repeating-characters/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 문자열에서 중복 문자 없는 가장 긴 부분 문자열의 길이를 반환하는 함수
* @param {string} s
* @return {number}
*/
const lengthOfLongestSubstring = function(s) {
let start = 0;
let end = 0;

const set = new Set();
let maxSize = 0;

while (end < s.length) {
while (set.has(s[end])) {
set.delete(s[start]);
start++;
}

set.add(s[end]);
maxSize = Math.max(maxSize, set.size);
end++;
}

return maxSize;
};

// 시간복잡도: O(n) (최대 end로 n번, start로 n번 이동하므로 2n만큼 소요)
// 공간복잡도: O(n)
46 changes: 46 additions & 0 deletions number-of-islands/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 주어진 2차원 격자에서 섬의 개수를 반환하는 함수
* 시간복잡도: O(m * n) (모든 원소를 순회해야 함)
* 공간복잡도: O(m * n) (bfs의 공간복잡도에 따름)
* @param {character[][]} grid
* @return {number}
*/
const numIslands = function (grid) {
let num = 0;

for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[0].length; c++) {
if (grid[r][c] === '1') {
bfs(grid, r, c);
num += 1;
}
}
}

return num;
};

// grid의 주어진 좌표에서 bfs를 수행해 방문했음을 표시
// 시간복잡도: O(m * n) (최악의 경우 모든 원소 순회)
// 공간복잡도: O(m * n) (최악의 경우 queue에 모든 원소 저장)
function bfs(grid, x, y) {
const queue = [[x, y]];
const dx = [0, 0, 1, -1];
const dy = [1, -1, 0, 0];
const rows = grid.length;
const cols = grid[0].length;

while (queue.length) {
const [r, c] = queue.shift();

for (let i = 0; i < 4; i++) {
const nr = r + dx[i];
const nc = c + dy[i];

if (0 <= nr && nr < rows && 0 <= nc && nc < cols && grid[nr][nc] === '1') {
queue.push([nr, nc]);
grid[nr][nc] = '0';
}
}
}
}
48 changes: 48 additions & 0 deletions reverse-linked-list/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function ListNode(val, next) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}

// 첫 번째 시도
// 시간복잡도: O(n)
// 공간복잡도: O(n)
/**
* 단방향 연결 리스트를 reverse하여 반환하는 함수
* @param {ListNode} head
* @return {ListNode}
*/
const reverseList = function (head) {
const newHead = new ListNode();

function _reverseList(head) {
if (!head) {
return newHead;
}

const reversedHead = _reverseList(head.next);
reversedHead.next = new ListNode(head.val, null);

return reversedHead.next;
}

_reverseList(head);
return newHead.next;
};


// 두 번째 시도
// 시간복잡도: O(n)
// 공간복잡도: O(1)
const reverseList = function (head) {
let current = head;
let prev = null;

while (current) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}

return prev;
};
30 changes: 30 additions & 0 deletions set-matrix-zeroes/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 주어진 격자에서 원소가 0인 행과 열의 값을 0으로 수정하는 함수
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
const setZeroes = function (matrix) {
const m = matrix.length;
const n = matrix[0].length;
const rows = new Set();
const cols = new Set();

for (let r = 0; r < m; r++) {
for (let c = 0; c < n; c++) {
if (matrix[r][c] === 0) {
rows.add(r);
cols.add(c);
}
}
}

rows.forEach((row) => matrix[row] = Array(n).fill(0));
cols.forEach((col) => {
for (let r = 0; r < m; r++) {
matrix[r][col] = 0;
}
});
};

// 시간복잡도: O(m * n)
// 공간복잡도: O(m + n)
36 changes: 36 additions & 0 deletions unique-paths/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* (0,0)에서 (m,n)에 도달할 수 있는 방법의 수를 반환하는 함수
* @param {number} m
* @param {number} n
* @return {number}
*/
const uniquePaths = function (m, n) {
const grid = Array.from({length: m}, () => Array(n).fill(0));
let r = 0;
let c = 0;
const queue = [[r, c]];
grid[r][c] = 1;

while (queue.length) {
const [x, y] = queue.shift();

if (x === m - 1 && y === n - 1) {
continue;
}

if (0 <= x + 1 && x + 1 < m) {
if (grid[x+1][y] === 0) queue.push([x + 1, y]);
grid[x+1][y] += grid[x][y];
}

if (0 <= y + 1 && y + 1 < n) {
if (grid[x][y+1] === 0) queue.push([x, y + 1]);
grid[x][y+1] += grid[x][y];
}
}

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

// 시간복잡도: O(m * n)
// 공간복잡도: O(m * n)