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

[YeomChaeeun] Week 10 #1020

Merged
merged 3 commits into from
Feb 16, 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 invert-binary-tree/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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)
* - 공간 복잡도: O(n)
* @param root
*/
function invertTree(root: TreeNode | null): TreeNode | null {

// 1. 재귀 알고리즘
// if(root === null) return null
//
// const right = root.right;
// root.right = invertTree(root.left)
// root.left = invertTree(right)
//
// return root

// 2. 스택을 사용하여 트리를 순회 - 반복 알고리즘
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀를 순회로 바꿔보신 것 정말 좋네요!

let stack : (TreeNode | null)[] = [root]
while(stack.length > 0) {
const node = stack.pop();
if(!node) continue

[node.left, node.right] = [node.right, node.left];

stack.push(node.left)
stack.push(node.right)
}
return root
}
27 changes: 27 additions & 0 deletions jump-game/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 점프 게임 현재 인덱스에서 인덱스 값만큼 점프하여 마지막 인덱스까지 도달할 수 있는지 구하기
* 알고리즘 복잡도
* - 시간 복잡도: O(n2)
* - 공간 복잡도: O(n)
* @param nums
*/
function canJump(nums: number[]): boolean {
const dp = new Array(nums.length).fill(false)

dp[0] = true
for(let i = 0; i < nums.length; i++) {
if(!dp[i]) continue;

for(let step = 1; step <= nums[i]; step++) {
if(i + step < nums.length) {
dp[i + step] = true;
}
}

if(dp[nums.length -1]) {
return true;
}
}

return dp[nums.length - 1];
}
38 changes: 38 additions & 0 deletions search-in-rotated-sorted-array/YeomChaeeun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 정렬된 배열에 target 문자의 인덱스 찾기
* 알고리즘 복잡도
* - 시간 복잡도: O(logn)
* - 공간 복잡도: O(1)
* @param nums
* @param target
*/
function search(nums: number[], target: number): number {
// findIndex 는 시간복잡도 O(n) 임
// return nums.findIndex(value => value === target)?? -1

// 정렬되어 있는 특성을 이용한 풀이
let low = 0;
let high = nums.length - 1;
while(low <= high) {
let mid = low + Math.floor((high - low) / 2);
Copy link
Contributor

Choose a reason for hiding this comment

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

오버플로를 고려하셨네요! 👍


if(nums[mid] === target) return mid

if(nums[low] <= nums[mid]) {
if(nums[low] <= target && target < nums[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if(nums[mid] < target && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}

}

return -1
}