From fdeed29de4233e2426ad3f26d6237906f8c04b2e Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Wed, 4 Sep 2024 23:35:44 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-palindrome/hwanmini.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 valid-palindrome/hwanmini.js diff --git a/valid-palindrome/hwanmini.js b/valid-palindrome/hwanmini.js new file mode 100644 index 000000000..cad935a9a --- /dev/null +++ b/valid-palindrome/hwanmini.js @@ -0,0 +1,29 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function(s) { + const strs = s.replace(/[^a-z0-9]/gi, '').toLowerCase(); + + let leftIdx = 0; + let rightIdx = strs.length - 1 + + while (leftIdx <= rightIdx) { + if (strs[leftIdx] !== strs[rightIdx]) return false + + leftIdx++ + rightIdx-- + } + + + return true +}; + +const s = "A man, a plan, a canal: Panama" + + +console.log(isPalindrome(s)) + From 30f5b92817f275bd287892def4e8692f1a8cc24b Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Fri, 6 Sep 2024 22:38:03 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-consecutive-sequence/hwanmini.js | 32 ++++++++++++++++++++++++ missing-number/hwanmini.js | 19 ++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 longest-consecutive-sequence/hwanmini.js create mode 100644 missing-number/hwanmini.js diff --git a/longest-consecutive-sequence/hwanmini.js b/longest-consecutive-sequence/hwanmini.js new file mode 100644 index 000000000..81bca18e7 --- /dev/null +++ b/longest-consecutive-sequence/hwanmini.js @@ -0,0 +1,32 @@ +// 시간 복잡도 O(n log n) +// 공간 복잡도 O(n) + +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function(nums) { + if (nums.length === 0) return [] + + let maxSequenceLength = -Infinity + + + const setNums = [...new Set(nums)].toSorted((a,b) => a - b) + + let count = 0; + for (let i = 0 ; i < setNums.length; i++) { + if (setNums[i]+1 === setNums[i+1]) { + count += 1 + } else { + count += 1 + maxSequenceLength = Math.max(maxSequenceLength, count) + count = 0; + } + } + + return maxSequenceLength +}; + + +console.log(longestConsecutive([100,4,200,1,3,2])) +console.log(longestConsecutive([0,3,7,2,5,8,4,6,0,1])) diff --git a/missing-number/hwanmini.js b/missing-number/hwanmini.js new file mode 100644 index 000000000..67ef0f5cc --- /dev/null +++ b/missing-number/hwanmini.js @@ -0,0 +1,19 @@ +// 시간복잡도 O(n log n) +// 공간복잡도 O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + nums.sort((a,b) => a - b); + + for (let i = 0 ; i <= nums.length; i++) { + if (i !== nums[i]) return i + } + +}; + +console.log(missingNumber([3, 0, 1])) +console.log(missingNumber([0, 1])) +console.log(missingNumber([9,6,4,2,3,5,7,0,1])) From 974e38a4236ab0ff2c14b368359d04cb21462fe4 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 7 Sep 2024 23:51:32 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-search/hwanmini.js | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 word-search/hwanmini.js diff --git a/word-search/hwanmini.js b/word-search/hwanmini.js new file mode 100644 index 000000000..14292ed37 --- /dev/null +++ b/word-search/hwanmini.js @@ -0,0 +1,69 @@ + +// m은 board의 행 수, n은 board의 열 수, 4(상하좌우), l(word) + +// 시간복잡도: O(m * n * 4L) +// 공간복잡도: O(m * n + L) + +const d_row = [1, -1, 0, 0] +const d_col = [0, 0, -1, 1] + + +const isMoveBoard = (new_row, new_col, board) => { + return new_row >= 0 && new_row < board.length && new_col >= 0 && new_col < board[0].length +} + + +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +var exist = function(board, word) { + let result = false + + const visited = Array.from({length: board.length} , () => Array.from({length: board[0].length}).fill(false)) + + const dfs = (strs, row, col, count) => { + + if (strs[count] !== word[count]) return + if (strs.length > word.length) return + + if (strs === word) { + result = true + return + } + + for (let i = 0 ; i < d_row.length; i++) { + const new_row = row + d_row[i] + const new_col = col + d_col[i] + + if (isMoveBoard(new_row, new_col, board) && visited[new_row][new_col] !== true){ + visited[new_row][new_col] = true + dfs(strs+board[new_row][new_col], new_row, new_col, count+1) + visited[new_row][new_col] = false + } + } + + + + } + + + for (let row = 0 ; row < board.length ; row++) { + for (let col = 0 ; col < board[0].length ; col++) { + visited[row][col] = true + dfs(board[row][col], row, col , 0) + visited[row][col] = false + } + } + + + return result +}; + +console.log(exist([["A","B","C","E"], + ["S","F","C","S"], + ["A","D","E","E"]], "ABCCED")) +console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE")) +console.log(exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCB")) + From 1f39448ee3ac11bf90e7e5f1ef8e183a51fde5ae Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 7 Sep 2024 23:53:03 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20=EA=B3=B5=EA=B0=84=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- missing-number/hwanmini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/missing-number/hwanmini.js b/missing-number/hwanmini.js index 67ef0f5cc..23bdef4b6 100644 --- a/missing-number/hwanmini.js +++ b/missing-number/hwanmini.js @@ -1,5 +1,5 @@ // 시간복잡도 O(n log n) -// 공간복잡도 O(1) +// 공간복잡도 O(n) /** * @param {number[]} nums From 0e74592681197f6275eb721777ce036e5e232294 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 8 Sep 2024 18:34:22 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20O(n)=20=ED=92=80=EC=9D=B4=EB=B0=A9?= =?UTF-8?q?=EB=B2=95=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-consecutive-sequence/hwanmini.js | 33 +++++++++++------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/longest-consecutive-sequence/hwanmini.js b/longest-consecutive-sequence/hwanmini.js index 81bca18e7..880d7b2ae 100644 --- a/longest-consecutive-sequence/hwanmini.js +++ b/longest-consecutive-sequence/hwanmini.js @@ -1,32 +1,29 @@ -// 시간 복잡도 O(n log n) -// 공간 복잡도 O(n) - /** * @param {number[]} nums * @return {number} */ var longestConsecutive = function(nums) { - if (nums.length === 0) return [] + if (nums.length === 0) return 0; - let maxSequenceLength = -Infinity + const numSet = new Set(nums); + let maxSequenceLength = 0; + for (const num of numSet) { + if (!numSet.has(num - 1)) { + let currentNum = num; + let currentLength = 1; - const setNums = [...new Set(nums)].toSorted((a,b) => a - b) + while (numSet.has(currentNum + 1)) { + currentNum++; + currentLength++; + } - let count = 0; - for (let i = 0 ; i < setNums.length; i++) { - if (setNums[i]+1 === setNums[i+1]) { - count += 1 - } else { - count += 1 - maxSequenceLength = Math.max(maxSequenceLength, count) - count = 0; + maxSequenceLength = Math.max(maxSequenceLength, currentLength); } } - return maxSequenceLength + return maxSequenceLength; }; - -console.log(longestConsecutive([100,4,200,1,3,2])) -console.log(longestConsecutive([0,3,7,2,5,8,4,6,0,1])) +console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); +console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); From 012e8d0410855cf3fbd564ab0e42d83ed92533e3 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 8 Sep 2024 18:39:47 +0900 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=EA=B3=B5=EA=B0=84=EB=B3=B5?= =?UTF-8?q?=EC=9E=A1=EB=8F=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-search/hwanmini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/word-search/hwanmini.js b/word-search/hwanmini.js index 14292ed37..8944bc1aa 100644 --- a/word-search/hwanmini.js +++ b/word-search/hwanmini.js @@ -2,7 +2,7 @@ // m은 board의 행 수, n은 board의 열 수, 4(상하좌우), l(word) // 시간복잡도: O(m * n * 4L) -// 공간복잡도: O(m * n + L) +// 공간복잡도: O(m * n) const d_row = [1, -1, 0, 0] const d_col = [0, 0, -1, 1]