Skip to content

[jun0811] WEEK 04 Solutions #1821

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions coin-change/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var coinChange = function(coins, amount) {
if(amount == 0) return 0

const dp = [0, ...new Array(amount).fill(amount+1)]

for (const coin of coins) {
for (let i = coin; i <=amount; i++) {
dp[i] = Math.min(dp[i], dp[i-coin] + 1)
}
}

return dp[amount] < amount+1 ? dp[amount] : -1
};
16 changes: 16 additions & 0 deletions find-minimum-in-rotated-sorted-array/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var findMin = function (nums) {
let left = 0,
right = nums.length - 1;

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

if (nums[mid] > nums[right]) {
left = mid + 1; // 최솟값이 오른쪽에 있음
} else {
right = mid; // 최솟값이 왼쪽에 있음 (mid 포함)
}
}

return nums[left];
};
22 changes: 22 additions & 0 deletions maximum-depth-of-binary-tree/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var maxDepth = function (root) {
let res = 0;

if (!root) return res; // root 자체가 없을 경우만 0

function check(node, depth) {
if (!node.left && !node.right) {
res = Math.max(res, depth);
return;
}

if (node.left) {
check(node.left, depth + 1);
}
if (node.right) {
check(node.right, depth + 1);
}
}

check(root, 1); // 루트부터 시작
return res;
};
20 changes: 20 additions & 0 deletions merge-two-sorted-lists/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/

var mergeTwoLists = function (list1, list2) {
if (!list1) return list2;
else if (!list2) return list1;
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

상단 부터 이런 '예외 처리(?)' 좋은 것 같습니다!


if (list1.val <= list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
};
58 changes: 58 additions & 0 deletions word-search/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/

const directions = [
[0, -1],
[1, 0],
[-1, 0],
[0, 1],
];

var exist = function (board, word) {
const cols = board[0].length; // 가로 (열 개수)
const rows = board.length; // 세로 (행 개수)
let res = false;

for (let col = 0; col < cols; col++) {
for (let row = 0; row < rows; row++) {
if (board[row][col] != word[0]) continue;

const visited = Array.from({ length: rows }, () =>
Array(cols).fill(false)
);
if (res) break;
dfs(row, col, board[row][col], visited);
}
}

function check(row, col) {
if (!(row >= 0 && row < rows)) return false;
if (!(col >= 0 && col < cols)) return false;
return true;
}

function dfs(row, col, str, visited) {
if (str == word) {
res = true;
return;
}
if (str.length >= word.length) return;
visited[row][col] = true;

for (const direction of directions) {
const [d_r, d_c] = direction;
const newCol = col + d_c;
const newRow = row + d_r;

if (check(newRow, newCol) && !visited[newRow][newCol]) {
dfs(newRow, newCol, str + board[newRow][newCol], visited);
}
}
visited[row][col] = false;
}

return res;
};