Skip to content

[uraflower] WEEK 04 solutions #1346

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 5 commits into from
Apr 26, 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
20 changes: 20 additions & 0 deletions coin-change/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 주어진 금액을 동전으로 거스를 때 최소 동전 개수를 반환하는 함수
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
const coinChange = function (coins, amount) {
const dp = [0, ...Array(amount).fill(amount + 1)];

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

return dp[amount] > amount ? -1 : dp[amount]
};

// 시간복잡도: O(c*n) (c: coins.length, n: amount)
// 공간복잡도: O(n)
35 changes: 35 additions & 0 deletions find-minimum-in-rotated-sorted-array/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 주어진 배열의 최솟값을 반환하는 함수
* @param {number[]} nums
* @return {number}
*/
// 첫 번째 시도
const findMin = function(nums) {
return Math.min(...nums);
};

// 시간복잡도: O(n)
// 공간복잡도: O(1)

// ===========================================
// 두 번째 시도
const findMin = function(nums) {
let left = 0;
let right = nums.length - 1;
let mid;

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

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

return nums[left];
}

// 시간복잡도: O(logn)
// 공간복잡도: O(1)
31 changes: 31 additions & 0 deletions maximum-depth-of-binary-tree/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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}
*/
const maxDepth = function(root) {
let maxDepth = 0;

function bfs(node, depth) {
if (!node) return;

depth += 1;
if (maxDepth < depth) maxDepth = depth;
bfs(node.left, depth);
bfs(node.right, depth);
}

bfs(root, maxDepth);
return maxDepth;
};

// 시간복잡도: O(n)
// 공간복잡도: O(h) (h: 트리의 높이. 최악의 경우 편향트리일 때 h===n으로 O(n))
40 changes: 40 additions & 0 deletions merge-two-sorted-lists/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* 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}
*/
const mergeTwoLists = function (list1, list2) {
let head = new ListNode(-1, null);
let mergedList = head;

let node1 = list1;
let node2 = list2;

while (node1 && node2) {
if (node1.val >= node2.val) {
mergedList.next = node2;
node2 = node2.next;
} else if (node1.val < node2.val) {
mergedList.next = node1;
node1 = node1.next;
}

mergedList = mergedList.next;
}

mergedList.next = node1 ?? node2;

return head.next;
};

// 시간복잡도: O(n)
// 공간복잡도: O(n)
46 changes: 46 additions & 0 deletions word-search/uraflower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 주어진 2차 배열에 word가 있는지 여부를 반환하는 함수
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
const exist = function (board, word) {
const row = board.length;
const col = board[0].length;
const visited = Array.from({ length: row }, () => Array(col).fill(false));
Copy link
Contributor

Choose a reason for hiding this comment

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

지금 풀이도 충분히 좋은데, 참고로 board를 임시로 마킹하는 방법으로 공간 복잡도를 줄일 수도 있어서 가볍게 고려해보셔도 좋을 것 같아요. 한 주 동안 고생 많으셨습니다. 👍

const dr = [0, 0, 1, -1];
const dc = [1, -1, 0, 0];

function dfs(r, c, charIdx) {

if (charIdx + 1 === word.length) return true;

for (let i = 0; i < 4; i++) {
const nr = r + dr[i];
const nc = c + dc[i];

if (0 <= nr && nr < row && 0 <= nc && nc < col && !visited[nr][nc] && word[charIdx + 1] === board[nr][nc]) {
visited[nr][nc] = true;
if (dfs(nr, nc, charIdx + 1)) return true;
visited[nr][nc] = false;
}
}

return false;
}

for (let r = 0; r < row; r++) {
for (let c = 0; c < col; c++) {
if (word[0] === board[r][c]) {
visited[r][c] = true;
if (dfs(r, c, 0)) return true;
visited[r][c] = false;
}
}
}

return false;
};

// 시간복잡도: O(m * n * 4^L) (L = word.length. 네 방향으로 최대 L만큼 수행할 수 있음.)
// 공간복잡도: O(m * n)