Skip to content

[krokerdile] WEEK 4 Solutions #1368

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 7 commits into from
Apr 27, 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
25 changes: 25 additions & 0 deletions coin-change/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0; // 0원을 만들기 위한 동전 수는 0개

// bottom-up DP
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] === Infinity ? -1 : dp[amount];
};

// 시간복잡도: O(amount * coins.length)
// -> 각 금액 i마다 모든 coin을 시도하기 때문
// 공간복잡도: O(amount)
// -> dp 배열을 사용하기 때문
26 changes: 26 additions & 0 deletions find-minimum-in-rotated-sorted-array/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let left = 0;
let right = nums.length - 1;

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

// 중간값이 오른쪽보다 크면 최소값은 오른쪽에 있다!
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
// 최소값은 mid를 포함한 왼쪽에 있다!
right = mid;
}
}

// left == right일 때 최소값이 위치함
return nums[left];
};

// 시간 복잡도: O(log n), 이진 탐색으로 탐색 범위를 절반씩 줄여나감
// 공간 복잡도: O(1), 추가적인 공간을 사용하지 않음
30 changes: 30 additions & 0 deletions maximum-depth-of-binary-tree/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 {number}
*/
var maxDepth = function(root) {
if (root === null) return 0;

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

while (queue.length > 0) {
let levelSize = queue.length;
for (let i = 0; i < levelSize; i++) {
const node = queue.shift();
if (node.left) queue.push(node.left);
if (node.right) queue.push(node.right);
}
depth++;
}

return depth;
};
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/krokerdile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function(list1, list2) {
// 결과 리스트의 시작점을 위한 더미 노드
let dummy = new ListNode(-1);
let current = dummy;

// 둘 다 null이 아닐 때까지 반복
while (list1 !== null && list2 !== null) {
if (list1.val <= list2.val) {
current.next = list1;
list1 = list1.next;
} else {
current.next = list2;
list2 = list2.next;
}
current = current.next;
}

// 남은 노드가 있으면 그대로 붙임
current.next = list1 !== null ? list1 : list2;

return dummy.next; // dummy 다음이 진짜 head
};
47 changes: 47 additions & 0 deletions word-search/krokerdile.js
Copy link
Contributor

Choose a reason for hiding this comment

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

방문시에도 board 자체를 그대로 사용하시고 복구시키면서 공간 복잡도를 최적화 하신게 너무 좋습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
const rows = board.length;
const cols = board[0].length;

// DFS 함수 정의
const dfs = (r, c, idx) => {
// 단어 끝까지 찾은 경우
if (idx === word.length) return true;

// 범위 밖이거나, 문자 불일치거나, 이미 방문한 경우
if (
r < 0 || c < 0 || r >= rows || c >= cols ||
board[r][c] !== word[idx]
) {
return false;
}

const temp = board[r][c]; // 현재 문자 저장
board[r][c] = "#"; // 방문 표시

// 상하좌우로 탐색
const found = dfs(r + 1, c, idx + 1) ||
dfs(r - 1, c, idx + 1) ||
dfs(r, c + 1, idx + 1) ||
dfs(r, c - 1, idx + 1);

board[r][c] = temp; // 백트래킹: 원상복구

return found;
};

// 보드의 모든 칸에서 시작해보기
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (dfs(r, c, 0)) return true;
}
}

return false;
};

// 시간복잡도: O(m * n * 4^L), m*n번 DFS 시작 가능하고, 각 DFS는 최대 4방향 * 단어 길이 만큼 탐색