From 8af04febdb2641a9850c74f5acf1faadcd94ad66 Mon Sep 17 00:00:00 2001 From: raejun92 Date: Thu, 19 Feb 2026 22:51:19 +0900 Subject: [PATCH 1/3] feat(raejun): add Binary Search, Invert Binary Tree, Valid Anagram, Valid Palindrome solutions --- raejun/BinarySearch.js | 55 ++++++++++++++++++++++++++++++++++++++ raejun/InvertBinaryTree.js | 39 +++++++++++++++++++++++++++ raejun/ValidAnagram.js | 26 ++++++++++++++++++ raejun/ValidPalindrome.js | 33 +++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 raejun/BinarySearch.js create mode 100644 raejun/InvertBinaryTree.js create mode 100644 raejun/ValidAnagram.js create mode 100644 raejun/ValidPalindrome.js diff --git a/raejun/BinarySearch.js b/raejun/BinarySearch.js new file mode 100644 index 0000000..e0ba270 --- /dev/null +++ b/raejun/BinarySearch.js @@ -0,0 +1,55 @@ +// /** +// * @param {number[]} nums +// * @param {number} target +// * @return {number} +// */ +// var search = function(nums, target) { +// let start = 0; +// let end = nums.length - 1; + +// while (start <= end) { +// const s = nums[start]; +// const e = nums[end]; + +// if (s === target) return start; +// if (e === target) return end; +// if (s < target) start++; +// if (e > target) end--; +// } + +// return -1; +// }; + +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function (nums, target) { + let start = 0; + let end = nums.length - 1; + + while (start <= end) { + if (nums[start] === target) return start; + if (nums[end] === target) return end; + + const mid = Math.floor((start + end) / 2); + const value = nums[mid]; + + if (value === target) return mid; + else if (value > target) end = mid - 1; + else start = mid + 1; + } + + return -1; +}; + +/* +20분 걸림. + +시간 복잡도는 O(logn)이다. + +이진 탐색 알고리즘을 이용하여 풀이했다. + +처음에 양 끝에서부터 하나씩 비교해가며 target과 같은 값이 있는지 확인하는 방식으로 풀이했는데, 시간 복잡도가 O(n)이어서 이진 탐색 알고리즘을 이용하여 풀이했다. +*/ diff --git a/raejun/InvertBinaryTree.js b/raejun/InvertBinaryTree.js new file mode 100644 index 0000000..ea587bd --- /dev/null +++ b/raejun/InvertBinaryTree.js @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val, left, right) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var invertTree = function (root) { + function change(root) { + if (root === null) return; + + let tmp = root.right; + + root.right = root.left; + root.left = tmp; + change(root.left); + change(root.right); + } + + change(root); + + return root; +}; + +/* +43분 걸림. + +시간 복잡도는 O(n)이다. + +재귀적으로 트리를 탐색하며, 각 노드에서 왼쪽과 오른쪽 자식 노드를 바꿔주는 방식으로 풀이했다. + +재귀가 익숙하지 않아 풀이하는 데 시간이 다소 걸렸다. +완전 이진트리와 이진트리 개념이 헷갈려서 풀이하는 데 시간이 더 걸렸던 것 같다. +*/ diff --git a/raejun/ValidAnagram.js b/raejun/ValidAnagram.js new file mode 100644 index 0000000..933a800 --- /dev/null +++ b/raejun/ValidAnagram.js @@ -0,0 +1,26 @@ +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isAnagram = function (s, t) { + s = s + .split("") + .sort((a, b) => a.charCodeAt() - b.charCodeAt()) + .join(""); + t = t + .split("") + .sort((a, b) => a.charCodeAt() - b.charCodeAt()) + .join(""); + + return s === t; +}; + +/* +4분 걸림. + +시간 복잡도는 O(nlogn)이다. + +문자열을 문자 단위로 나눈 후, 알파벳 순서대로 정렬한 다음 다시 문자열로 합쳐서 두 문자열이 같은지 비교하는 방식으로 풀이했다. + +*/ diff --git a/raejun/ValidPalindrome.js b/raejun/ValidPalindrome.js new file mode 100644 index 0000000..5ea260f --- /dev/null +++ b/raejun/ValidPalindrome.js @@ -0,0 +1,33 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function (s) { + const sArr = s + .toLowerCase() + .split("") + .filter( + (s) => + (48 <= s.charCodeAt() && s.charCodeAt() <= 57) || + (97 <= s.charCodeAt() && s.charCodeAt() <= 122), + ); + + let len = sArr.length - 1; + + for (let i = 0; i <= len; i++) { + if (sArr[i] !== sArr[len - i]) return false; + } + + return true; +}; + +/* +20분 걸림. + +시간 복잡도는 O(n)이다. + +알파벳과 숫자만 남기고 모두 제거한 후, 양 끝에서부터 하나씩 비교해가며 팰린드롬인지 확인하는 방식으로 풀이했다. + +평소 정규표현식에 대한 지식이 있다면 더 간단하게 풀이할 수 있었을 것 같다. +매서드가 익숙지 않아 시간 다소 걸렸지만, 풀이 자체는 어렵지 않았다. +*/ From 1d3561e6fb130bffcc329050589814512e69bc45 Mon Sep 17 00:00:00 2001 From: raejun92 Date: Fri, 20 Feb 2026 00:37:08 +0900 Subject: [PATCH 2/3] feat(raejun): add Flood Fill solution --- .gitignore | 2 ++ raejun/FloodFill.js | 60 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 .gitignore create mode 100644 raejun/FloodFill.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69ba332 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +CLAUDE.md +.omc \ No newline at end of file diff --git a/raejun/FloodFill.js b/raejun/FloodFill.js new file mode 100644 index 0000000..658ea43 --- /dev/null +++ b/raejun/FloodFill.js @@ -0,0 +1,60 @@ +/** + * @param {number[][]} image + * @param {number} sr + * @param {number} sc + * @param {number} color + * @return {number[][]} + */ +var floodFill = function (image, sr, sc, color) { + const queue = [[sr, sc]]; + let cur = 0; + const row = image.length - 1; + const col = image[0].length - 1; + const min = 0; + const tmp = image[sr][sc]; + + if (tmp === color) return image; + + image[sr][sc] = color; + + while (cur < queue.length) { + const [x, y] = queue[cur]; + + if (y + 1 <= col && image[x][y + 1] === tmp) { + image[x][y + 1] = color; + queue.push([x, y + 1]); + } + + if (y - 1 >= min && image[x][y - 1] === tmp) { + image[x][y - 1] = color; + queue.push([x, y - 1]); + } + + if (x + 1 <= row && image[x + 1][y] === tmp) { + const col = image[0].length - 1; + image[x + 1][y] = color; + queue.push([x + 1, y]); + } + + if (x - 1 >= min && image[x - 1][y] === tmp) { + image[x - 1][y] = color; + queue.push([x - 1, y]); + } + + cur++; + } + + return image; +}; + +/* +문제 풀지 못함. + +시간 복잡도는 O(n)이다. + +BFS 알고리즘을 이용하여 풀이했다. + +BFS를 이용해서 문제를 풀려고 했다. +2차원 배열의 row와 col이 다소 헷갈렸고 반복문의 조건 설정이 어려웠다. +if문의 예외 처리를 생각하지 못하여 풀이를 하지 못하였다. +*/ From 82c6cfa0001c951a31f7ed4d2d1a9924cbd1a3a8 Mon Sep 17 00:00:00 2001 From: raejun92 Date: Fri, 20 Feb 2026 00:38:50 +0900 Subject: [PATCH 3/3] docs(raejun): update Flood Fill review comments --- raejun/FloodFill.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/raejun/FloodFill.js b/raejun/FloodFill.js index 658ea43..2b91e6e 100644 --- a/raejun/FloodFill.js +++ b/raejun/FloodFill.js @@ -57,4 +57,6 @@ BFS 알고리즘을 이용하여 풀이했다. BFS를 이용해서 문제를 풀려고 했다. 2차원 배열의 row와 col이 다소 헷갈렸고 반복문의 조건 설정이 어려웠다. if문의 예외 처리를 생각하지 못하여 풀이를 하지 못하였다. +다른 사람들의 풀이를 보니 DFS로 푼 것이 공간 및 시간 복잡도 측면에서 더 효율적으로 나왔다. +왜 그런 걸까? */