Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CLAUDE.md
.omc
55 changes: 55 additions & 0 deletions raejun/BinarySearch.js
Original file line number Diff line number Diff line change
@@ -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;
Comment on lines +33 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

시간 복잡에는 문제가 없는거 같지만, 왜 이 비교(매 루프마다 양 끝 비교하는 부분)가 필요한지의 관점에서 보면 mid 비교만 남긴다면 더 단순해질 것 같아요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇네요 반복문 전에 한 번 확인하고 반복문에서는 mid만 확인하면 될 것 같아요!


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)이어서 이진 탐색 알고리즘을 이용하여 풀이했다.
*/
62 changes: 62 additions & 0 deletions raejun/FloodFill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 shift() 말고 바로 포인터 방식 떠올리셨나요? 대박

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문의 예외 처리를 생각하지 못하여 풀이를 하지 못하였다.
다른 사람들의 풀이를 보니 DFS로 푼 것이 공간 및 시간 복잡도 측면에서 더 효율적으로 나왔다.
왜 그런 걸까?
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 다른 사람 풀이를 보긴 했는데,
BFS로 푼 방식도 좋은 풀이인 것 같습니다 👍
shift 메서드를 쓰시지 않아서 더 좋은 것 같아욥

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shift를 쓰지않고 BFS로 풀어도 DFS가 더 좋게 나오더라구요!

39 changes: 39 additions & 0 deletions raejun/InvertBinaryTree.js
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change 재정의 안하고 invertTree 자체를 재귀로 돌려도 좋을 것 같슴다! 저도 재귀 넘 어려워요ㅠ

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)이다.

재귀적으로 트리를 탐색하며, 각 노드에서 왼쪽과 오른쪽 자식 노드를 바꿔주는 방식으로 풀이했다.

재귀가 익숙하지 않아 풀이하는 데 시간이 다소 걸렸다.
완전 이진트리와 이진트리 개념이 헷갈려서 풀이하는 데 시간이 더 걸렸던 것 같다.
*/
26 changes: 26 additions & 0 deletions raejun/ValidAnagram.js
Original file line number Diff line number Diff line change
@@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

charCodeAt 메서드로도 풀 수 있군요..!
메서드체이닝으로 코드가 간단해져서 풀이가 잘 보이네요 👍

.join("");
t = t
.split("")
.sort((a, b) => a.charCodeAt() - b.charCodeAt())
.join("");

return s === t;
};

/*
4분 걸림.

시간 복잡도는 O(nlogn)이다.

문자열을 문자 단위로 나눈 후, 알파벳 순서대로 정렬한 다음 다시 문자열로 합쳐서 두 문자열이 같은지 비교하는 방식으로 풀이했다.

*/
33 changes: 33 additions & 0 deletions raejun/ValidPalindrome.js
Original file line number Diff line number Diff line change
@@ -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),
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메서드를 잘 쓰시는군요 👍


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)이다.

알파벳과 숫자만 남기고 모두 제거한 후, 양 끝에서부터 하나씩 비교해가며 팰린드롬인지 확인하는 방식으로 풀이했다.

평소 정규표현식에 대한 지식이 있다면 더 간단하게 풀이할 수 있었을 것 같다.
매서드가 익숙지 않아 시간 다소 걸렸지만, 풀이 자체는 어렵지 않았다.
*/