Skip to content

Commit

Permalink
Merge pull request #530 from wogha95/main
Browse files Browse the repository at this point in the history
[재호] WEEK 10 Solutions
  • Loading branch information
wogha95 authored Oct 21, 2024
2 parents bd5bb4f + 42c0ba4 commit b14e4d9
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
49 changes: 49 additions & 0 deletions course-schedule/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* TC: O(V + E)
* SC: O(V + E)
* N: numCourses(all of vertex), P: prerequisites(all of edge)
*/

/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function (numCourses, prerequisites) {
const STEP = {
before: 0,
ing: 1,
after: 2,
};
const stepBoard = Array.from({ length: numCourses }, () => STEP.before);
const board = Array.from({ length: numCourses }, () => []);

for (const [a, b] of prerequisites) {
board[a].push(b);
}

for (let index = 0; index < numCourses; index++) {
if (isCycle(index)) {
return false;
}
}
return true;

function isCycle(current) {
if (stepBoard[current] === STEP.end) {
return false;
}
if (stepBoard[current] === STEP.ing) {
return true;
}

stepBoard[current] = STEP.ing;
for (const next of board[current]) {
if (isCycle(next)) {
return true;
}
}
stepBoard[current] = STEP.end;
return false;
}
};
33 changes: 33 additions & 0 deletions invert-binary-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* μ–‘μͺ½ μžμ‹ λ…Έλ“œ μ£Όμ†Œλ₯Ό κ΅ν™˜ν•˜κ³  dfs둜 μˆœνšŒν•©λ‹ˆλ‹€.
*
* TC: O(N)
* λͺ¨λ“  트리λ₯Ό μˆœνšŒν•©λ‹ˆλ‹€.
*
* SC: O(N)
* μ΅œμ•…μ˜ 경우 (ν•œμͺ½μœΌλ‘œ 치우친 트리) N만큼 CallStack이 μƒκΉλ‹ˆλ‹€.
*
* N: tree의 λͺ¨λ“  node 수
*/

/**
* 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) {
if (!root) {
return root;
}
[root.left, root.right] = [root.right, root.left];
invertTree(root.left);
invertTree(root.right);
return root;
};
37 changes: 37 additions & 0 deletions jump-game/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* TC: O(N)
* SC: O(1)
* N: nums.length
*/

/**
* @param {number[]} nums
* @return {boolean}
*/
var canJump = function (nums) {
if (nums.length === 1) {
return true;
}

let maximumIndex = 0;

for (let index = 0; index < nums.length; index++) {
const jumpLength = nums[index];

if (jumpLength === 0) {
continue;
}

if (maximumIndex < index) {
return false;
}

maximumIndex = Math.max(maximumIndex, index + nums[index]);

if (maximumIndex >= nums.length - 1) {
return true;
}
}

return false;
};
65 changes: 65 additions & 0 deletions search-in-rotated-sorted-array/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* λͺ¨λ“  μΌ€μ΄μŠ€κ°€ λ§Žμ§€ μ•Šλ‹€κ³  μƒκ°λ˜μ–΄ λΆ„κΈ°μ²˜λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€..
* 더 κ°„κ²°ν•œ 풀이법을 κ³ λ €ν•΄λ³΄λŠ” 쀑..
*
* TC: O(log N)
* 이진탐색을 μ΄μš©ν•˜μ—¬ μˆœνšŒν•©λ‹ˆλ‹€.
*
* SC: O(1)
* 이진탐색에 μ΄μš©λ˜λŠ” 투 ν¬μΈν„°μ˜ κ³΅κ°„λ³΅μž‘λ„λ₯Ό κ°–μŠ΅λ‹ˆλ‹€.
*/

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var search = function (nums, target) {
if (nums.length === 1) {
return target === nums[0] ? 0 : -1;
}

let left = 0;
let right = nums.length - 1;

while (left < right) {
const center = Math.floor((left + right) / 2);
if (target === nums[left]) {
return left;
}
if (target === nums[center]) {
return center;
}
if (target === nums[right]) {
return right;
}

if (nums[left] <= nums[center] && nums[center] < nums[right]) {
if (target < nums[left] || nums[right] < target) {
return -1;
} else if (nums[left] < target && target < nums[center]) {
right = center;
} else if (nums[center] < target && target < nums[right]) {
left = center + 1;
}
} else if (nums[right] < nums[left] && nums[left] <= nums[center]) {
if (nums[right] < target && target < nums[left]) {
return -1;
} else if (nums[left] < target && target < nums[center]) {
right = center;
} else {
left = center + 1;
}
} else if (nums[center] < nums[right] && nums[right] < nums[left]) {
if (nums[center] < target && target < nums[right]) {
left = center + 1;
} else if (nums[right] < target && target < nums[left]) {
return -1;
} else {
right = center;
}
}
}

return -1;
};

0 comments on commit b14e4d9

Please sign in to comment.