diff --git a/longest-substring-without-repeating-characters/jdy8739.js b/longest-substring-without-repeating-characters/jdy8739.js new file mode 100644 index 000000000..9ab6e60f7 --- /dev/null +++ b/longest-substring-without-repeating-characters/jdy8739.js @@ -0,0 +1,33 @@ +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let start = 0; + let end = 0; + + const set = new Set(); + + let max = 0; + + while (end < s.length) { + const char = s[end]; + + if (set.has(char)) { + set.delete(s[start]); + + start++; + } else { + set.add(char); + + end++; + } + + max = Math.max(max, set.size); + } + + return max; +}; + +// 시간복잡도 O(2n) -> 최대 2n번 반복 +// 공간복잡도 O(n) -> 최대 s의 길이만큼 공간을 사용 diff --git a/number-of-islands/jdy8739.js b/number-of-islands/jdy8739.js new file mode 100644 index 000000000..bb2417382 --- /dev/null +++ b/number-of-islands/jdy8739.js @@ -0,0 +1,43 @@ +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function (grid) { + const sink = (row, col) => { + grid[row][col] = '0'; + + const neighbor = [ + [row + 1, col], [row, col + 1], [row - 1, col], [row, col - 1] + ].filter(([y, x]) => { + return y >= 0 && x >= 0 && y < grid.length && x < grid[0].length; + }).filter(([y, x]) => { + return grid[y][x] === '1'; + }); + + neighbor.forEach(([y, x]) => { + const el = grid[y][x]; + + if (el === '1') { + sink(y, x); + } + }) + } + + let count = 0; + + for (let i = 0; i < grid.length; i++) { + for (let j = 0; j < grid[0].length; j++) { + + if (grid[i][j] === '1') { + count++; + sink(i, j); + } + } + } + + return count; +}; + +// 시간복잡도 O(2 * m * n) -> m * n 만큼 반복 + 재귀적으로 방문할 수 있는 셀의 수는 총 m * n 개 +// 공간복잡도 O(1) -> 입력 배열을 사용하지 않고 변수만 사용 + diff --git a/reverse-linked-list/jdy8739.js b/reverse-linked-list/jdy8739.js new file mode 100644 index 000000000..089ae0d69 --- /dev/null +++ b/reverse-linked-list/jdy8739.js @@ -0,0 +1,45 @@ + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + if (head === null) { + return null; + } + + if (head.next === null) { + return head; + } + + const stack = []; + + let nextNode = head; + + while (nextNode) { + stack.push(nextNode); + + nextNode = nextNode.next; + } + + for (let i=stack.length - 1; i>=0; i--) { + if (i === 0) { + stack[i].next = null; + } else { + stack[i].next = stack[i - 1]; + } + } + + return stack[stack.length - 1]; +}; + +// 시간복잡도 O(2n) + + diff --git a/set-matrix-zeroes/jdy8739.js b/set-matrix-zeroes/jdy8739.js new file mode 100644 index 000000000..119dc3f72 --- /dev/null +++ b/set-matrix-zeroes/jdy8739.js @@ -0,0 +1,38 @@ +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function (matrix) { + const coord = []; + + for (let i = 0; i < matrix.length; i++) { + for (let j = 0; j < matrix[i].length; j++) { + + const num = matrix[i][j]; + + if (num === 0) { + coord.push({ y: i, x: j }); + } + } + } + + for (let k = 0; k < coord.length; k++) { + const { y } = coord[k]; + + for (let j = 0; j < matrix[0].length; j++) { + matrix[y][j] = 0; + } + } + + for (let l = 0; l < coord.length; l++) { + const { x } = coord[l]; + + for (let j = 0; j < matrix.length; j++) { + matrix[j][x] = 0; + } + } +}; + +// 시간복잡도 O(n * m) +// 공간복잡도 O(n * m) + diff --git a/unique-paths/jdy8739.js b/unique-paths/jdy8739.js new file mode 100644 index 000000000..1577de0e6 --- /dev/null +++ b/unique-paths/jdy8739.js @@ -0,0 +1,72 @@ +var uniquePaths = function (m, n) { + const cache = new Map(); + + const dfs = (row, col) => { + const cacheKey = `${row}-${col}`; + + if (cache.has(cacheKey)) { + return cache.get(cacheKey); + } + + if (row === m - 1 && col === n - 1) { + return 1; + } + + let count = 0; + + if (row < m - 1) { + count += dfs(row + 1, col); + } + + if (col < n - 1) { + count += dfs(row, col + 1); + } + + cache.set(cacheKey, count); + + return count; + } + + return dfs(0, 0); +}; + +// 시간복잡도 O(m * n) +// 공간복잡도 O(m * n) - 1 (matrix[m][n]에 대한 캐시는 포함되지 않으므로) + +var uniquePaths = function(m, n) { + const matrix = []; + + for (let i=0; i