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
21 changes: 21 additions & 0 deletions coin-change/Yu-Won.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*
* 문제: https://leetcode.com/problems/coin-change/
* 요구사항: 동전과 총 금액이 주어질 때 해당 금액을 만드는데 최소 동전 개수를 반환
*/
const coinChange = (coins, amount) => {
let dp = new Array(amount + 1).fill(amount + 1);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (i - coin >= 0) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}

return dp[amount] > amount ? -1 : dp[amount];
};
23 changes: 23 additions & 0 deletions find-minimum-in-rotated-sorted-array/Yu-Won.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number}
*
* 문제: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
* 요구사항: 이진트리 활용
*/
const findMin = (nums) => {
let left = 0;
let right = nums.length - 1;

while (left < right) {
let mid = Math.floor((left + right) / 2);

if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}

return nums[left];
};
34 changes: 34 additions & 0 deletions maximum-depth-of-binary-tree/Yu-Won.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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)
* }
*
* 문제: https://leetcode.com/problems/maximum-depth-of-binary-tree/
* 요구사항: 이진 트리의 최대 깊이를 반환하라.
*/
/**
* @param {TreeNode} root
* @return {number}
*/
const maxDepth = (root) => {
if(!root) return 0;

let queue = [root];
let depth = 0;

while(queue.length) {
let size = queue.length;

for(let i = 0; i < size; i++) {
let node = queue.shift();

if(node.left) queue.push(node.left);
if(node.right) queue.push(node.right);
}
depth++;
}
return depth;
};
35 changes: 35 additions & 0 deletions merge-two-sorted-lists/Yu-Won.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*
* 문제: https://leetcode.com/problems/merge-two-sorted-lists/
* 요구사항: 정의된 ListNode 를 활용해서 정렬된 배열을 리턴
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/


const mergeTwoLists = (list1, list2) => {
const dummy = new ListNode(0);
let cur = dummy;

while (list1 && list2) {
if (list1.val <= list2.val) {
cur.next = list1;
list1 = list1.next;
} else {
cur.next = list2;
list2 = list2.next;
}
cur = cur.next;
}

cur.next = list1 ?? list2;
return dummy.next;
};
41 changes: 41 additions & 0 deletions word-search/Yu-Won.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*
* 문제: https://leetcode.com/problems/word-search/
* 요구사항: 그리드 구조에서 word의 문자열 존재 여부에 따라 true/false를 리턴한다.
* 백트래킹, dfs
*/
const exist = (board, word) => {
let rows = board.length;
let cols = board[0].length;

const dfs = (r, c, index) => {
if (index === word.length) return true;

if (r < 0 || r >= rows || c < 0 || c >= cols || board[r][c] !== word[index]) {
return false;
}

let temp = board[r][c];
board[r][c] = '#';

let found = dfs(r + 1, c, index + 1) ||
dfs(r - 1, c, index + 1) ||
dfs(r, c + 1, index + 1) ||
dfs(r, c - 1, index + 1);

board[r][c] = temp;

return found;
}

for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (board[i][j] === word[0] && dfs(i, j, 0)) return true;
}
}

return false;
};
Loading