diff --git a/longest-substring-without-repeating-characters/hwanmini.js b/longest-substring-without-repeating-characters/hwanmini.js new file mode 100644 index 000000000..890797fab --- /dev/null +++ b/longest-substring-without-repeating-characters/hwanmini.js @@ -0,0 +1,28 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let maxCount = 0; + const map = new Map() + + let leftIdx = 0; + for (let rightIdx = 0 ; rightIdx < s.length; rightIdx++) { + const char = s[rightIdx] + if (map.has(char) && map.get(char) >= leftIdx) leftIdx = map.get(char) + 1; + map.set(char, rightIdx) + maxCount = Math.max(maxCount, rightIdx - leftIdx + 1) + } + + return maxCount +}; + + +console.log(lengthOfLongestSubstring("abcabcbb")) +console.log(lengthOfLongestSubstring("bbbbb")) +console.log(lengthOfLongestSubstring("pwwkew")) +console.log(lengthOfLongestSubstring("abba")) + diff --git a/number-of-islands/hwanmini.js b/number-of-islands/hwanmini.js new file mode 100644 index 000000000..7a4982b18 --- /dev/null +++ b/number-of-islands/hwanmini.js @@ -0,0 +1,52 @@ +// 시간복잡도: O(n * m) +// 공간복잡도: O(n * m) + +const dr = [1,-1,0,0] +const dc = [0,0,-1, 1] + +const isValidMove = (grid, n_row, n_col) => { + return n_row >= 0 && n_row < grid.length && n_col >= 0 && n_col < grid[0].length && grid[n_row][n_col] !== '0' +} + + +/** + * @param {character[][]} grid + * @return {number} + */ +var numIslands = function(grid) { + let islandCount = 0; + + const bfs = (row, col) => { + const que = [[row,col]] + + while (que.length) { + const [row, col] = que.pop() + + for (let i = 0 ; i < dr.length; i++) { + const n_row = row + dr[i] + const n_col = col + dc[i] + + if (isValidMove(grid, n_row, n_col)) { + que.push([n_row, n_col]) + } + } + + grid[row][col] = '0' + + } + + islandCount += 1 + } + + + for (let row = 0; row < grid.length; row++) { + for (let col = 0; col < grid[row].length; col++) { + if (grid[row][col] !== '0') bfs(row, col) + } + } + + + return islandCount +}; + +console.log(numIslands([["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]])) diff --git a/reverse-linked-list/hwanmini.js b/reverse-linked-list/hwanmini.js new file mode 100644 index 000000000..bc70eba04 --- /dev/null +++ b/reverse-linked-list/hwanmini.js @@ -0,0 +1,28 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * 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) { + let curNode = head + let preNode = null + + while (curNode) { + const nextNode = curNode.next + curNode.next = preNode + preNode = curNode + curNode = nextNode + } + + + return preNode +}; diff --git a/set-matrix-zeroes/hwamini.js b/set-matrix-zeroes/hwamini.js new file mode 100644 index 000000000..b0dc64559 --- /dev/null +++ b/set-matrix-zeroes/hwamini.js @@ -0,0 +1,39 @@ +// 시간복잡도: O((n * m) * (n + m)) +// 공간복잡도: O(n * m) + +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + const zeroIndexes = [] + + for (let row = 0; row < matrix.length; row++) { + for (let col = 0; col < matrix[row].length; col++) { + if (matrix[row][col] === 0) { + zeroIndexes.push([row,col]) + } + } + } + + while (zeroIndexes.length) { + const [row, col] = zeroIndexes.pop() + + for (let i = 0; i < matrix[0].length; i++) { + matrix[row][i] = 0 + } + + for (let j = 0; j < matrix.length; j++) { + matrix[j][col] = 0 + } + + } + + return matrix +}; + +console.log(setZeroes([ + [1,1,1], + [1,0,1], + [1,1,1]])) +console.log(setZeroes([[0,1,2,0],[3,4,5,2],[1,3,1,5]]))