Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[재호] WEEK 15 Solutions #604

Merged
merged 5 commits into from
Nov 24, 2024
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
38 changes: 38 additions & 0 deletions longest-palindromic-substring/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* TC: O(N^2)
* 주어진 s 문자열이 한 종류의 문자로 이루어져있다면 for문에서 O(N), while문에서 O(N) 이므로 O(N * 2N)
*
* SC: O(1)
*/

/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function (s) {
let result = "";

for (let index = 0; index < s.length; index++) {
const [start1, end1] = getPalindromicSubstringLength(index, index);
const [start2, end2] = getPalindromicSubstringLength(index, index + 1);

if (result.length < end1 - start1 + 1) {
result = s.substring(start1, end1 + 1);
}

if (result.length < end2 - start2 + 1) {
result = s.substring(start2, end2 + 1);
}
}

return result;

function getPalindromicSubstringLength(start, end) {
while (0 <= start && end < s.length && s[start] === s[end]) {
start -= 1;
end += 1;
}

return [start + 1, end - 1];
}
};
27 changes: 27 additions & 0 deletions rotate-image/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 전치(행과 열을 교환) 후 행 반전(뒤집기)
*
* TC: O(N^2)
* SC: O(1)
*/

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function (matrix) {
const N = matrix.length;

for (let row = 0; row < N; row++) {
for (let column = row; column < N; column++) {
[matrix[row][column], matrix[column][row]] = [
matrix[column][row],
matrix[row][column],
];
}
}

for (let row = 0; row < N; row++) {
matrix[row].reverse();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}
};
65 changes: 65 additions & 0 deletions subtree-of-another-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* tree를 순회하면서 subRoot의 시작값과 동일한 노드를 찾습니다.
* 찾으면 동일한 트리인지 확인합니다.
*
* TC: O(N)
* 최악의 경우, root tree의 모든 node를 순회합니다.
*
* SC: O(N)
* 최악의 경우, root tree의 모든 node를 순회하기 위해 queue를 사용합니다.
*/

/**
* 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
* @param {TreeNode} subRoot
* @return {boolean}
*/
var isSubtree = function (root, subRoot) {
const queue = [root];

while (queue.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

queue로 푸는건 생각도 못했는데 배워갑니다;

const current = queue.shift();

if (!current) {
continue;
}

if (current.val === subRoot.val && isSameTree(current, subRoot)) {
return true;
}

if (current.left) {
queue.push(current.left);
}

if (current.right) {
queue.push(current.right);
}
}

return false;

function isSameTree(rootA, rootB) {
if (rootA === null && rootB === null) {
return true;
}

if (rootA === null || rootB === null) {
return false;
}

return (
rootA.val === rootB.val &&
isSameTree(rootA.left, rootB.left) &&
isSameTree(rootA.right, rootB.right)
);
}
};
40 changes: 40 additions & 0 deletions validate-binary-search-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* TC: O(N)
* SC: O(N)
* N: total count of tree nodes
*/

/**
* 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 {boolean}
*/
var isValidBST = function (root) {
return isValidBSTWithBoundary(
root,
Number.MIN_SAFE_INTEGER,
Number.MAX_SAFE_INTEGER
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

희찬님의 경우 이 부분을 null로 해서 풀이를 하셨는데, 주어진 범위의 MIN, MAX를 사용하니 코드가 더 깔끔해 지는것 같습니다. null로 했을때 약간 null을 왜 쓸까 라는 한번 더 생각하는 부분도 MIN, MAX의 범위를 넘어가지 않도록 한다는것에 의미가 더 전달되는것 같아 파악하기도 좋은것 같구요!

);

function isValidBSTWithBoundary(current, min, max) {
if (!current) {
return true;
}

if (current.val <= min || max <= current.val) {
return false;
}

return (
isValidBSTWithBoundary(current.left, min, current.val) &&
isValidBSTWithBoundary(current.right, current.val, max)
);
}
};