Skip to content

[YeomChaeeun] Week 15 #1116

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

Merged
merged 4 commits into from
Mar 22, 2025
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
45 changes: 45 additions & 0 deletions longest-palindromic-substring/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 중심 확장법을 이용하여 가장 긴 팰린드롬 찾기
* 알고리즘 복잡도
* - 시간 복잡도: O(n2)
* - 공간 복잡도: O(1)
* @param s
*/
function longestPalindrome(s: string): string {
let maxLength = 0;
let longestPal = '';

for (let x = 0; x < s.length; x++) {
// 1. 홀수 길이 팰린드롬 - 한 문자를 중심으로 함
let left = x
let right = x

while (left >= 0 && right < s.length && s[left] === s[right]) {
// 현재 발견한 팰린드롬이 이전에 발견한 것보다 길면 갱신
if (right - left + 1 > maxLength) {
maxLength = right - left + 1;
longestPal = s.substring(left, right + 1);
}

left--
right++
}

// 2. 짝수 길이 팰린드롬 - 두 문자 사이를 중심으로 함
left = x
right = x + 1

while (left >= 0 && right < s.length && s[left] === s[right]) {
// 현재 발견한 팰린드롬이 이전에 발견한 것보다 길면 갱신
if (right - left + 1 > maxLength) {
maxLength = right - left + 1;
longestPal = s.substring(left, right + 1);
}

left--
right++
}
}

return longestPal
}
58 changes: 58 additions & 0 deletions rotate-image/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
Do not return anything, modify matrix in-place instead.
*/
/**
* 90도 회전시키기
* 알고리즘 복잡도
* - 시간 복잡도: O(n2)
* - 공간 복잡도: O(1)
* @param matrix
*/
function rotate(matrix: number[][]): void {
let n = matrix.length;

// 0,0 -> 2,0
// 1,0 -> 2,1
// 2,0 -> 2,2

// 0,1 -> 1,0
// 1,1 -> 1,1
// 2,1 -> 1,2

// 0,2 -> 0,0
// 1,2 -> 0,1
// 2,2 -> 0,2

// 1 2 3
// 4 5 6
// 7 8 9

// 1 4 7
// 2 5 8
// 3 6 9

// 7 4 1
// 8 5 2
// 9 6 3

// 1. 행과 열을 바꿈(행과 열의 전치)
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
// 대각선을 기준으로 대칭
if (i !== j) {
const temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}

// 2. 각 행을 좌우로 뒤집기(= 90도 회전)
for (let i = 0; i < n; i++) {
for (let j = 0; j < Math.floor(n / 2); j++) {
const temp = matrix[i][j];
matrix[i][j] = matrix[i][n - 1 - j];
matrix[i][n - 1 - j] = temp;
}
}
}
37 changes: 37 additions & 0 deletions subtree-of-another-tree/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* 트리 내에 동일한 서브트리가 있는지 찾는 알고리즘
* 알고리즘 복잡도
* - 시간 복잡도: O(n×m) - n(root 노드 수), m(subRoot 노드 수)
* - 공간 복잡도: O(n+m) - 첫 번째 isSubtree()의 호출 스택의 깊이와 isSameTree()의 호출 스택의 깊이를 더한 것
* @param root
* @param subRoot
*/
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
if(!subRoot) return true
if(!root) return false

// 이전에 풀이한 isSameTree를 추가
function isSameTree(p: TreeNode | null, q: TreeNode | null) {
if(!p || !q) {
return p === q;
}
return p.val === q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}

if (isSameTree(root, subRoot)) return true
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot)
}

34 changes: 34 additions & 0 deletions validate-binary-search-tree/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* 이진트리 유효성 검사하기
* 알고리즘 복잡도
* - 시간 복잡도: O(n) - n: 노드의 총 개수
* - 공곤 복잡도: O(h) - h: 트리의 높이
* @param root
*/
function isValidBST(root: TreeNode | null): boolean {
if (!root) return true;

function isValid(node: TreeNode | null, min: number, max: number): boolean {
if (!node) return true;
if (node.val <= min || node.val >= max) return false;

return isValid(node.left, min, node.val) &&
isValid(node.right, node.val, max)
}

// 초기 호출 (루트 노드의 범위는 무한대)
return isValid(root, -Infinity, Infinity)
}