Skip to content

[hsskey] WEEK 04 solutions #1350

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
18 changes: 18 additions & 0 deletions coin-change/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function(coins, amount) {
const dp = new Array(amount + 1).fill(Infinity)

dp[0] = 0

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

return dp[amount] === Infinity ? -1 : dp[amount]
};
27 changes: 27 additions & 0 deletions find-minimum-in-rotated-sorted-array/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
if(nums.length === 1) {
return nums[0]
}

if(nums[0] < nums[nums.length -1]) {
return nums[0]
}

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 {
right = mid
}
}
return nums[left]
};
25 changes: 25 additions & 0 deletions maximum-depth-of-binary-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* 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) {
function dfs(node) {
if(!node) {
return 0
}

const left = dfs(node.left)
const right = dfs(node.right)

return Math.max(left, right) + 1
}
return dfs(root)
};
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/hsskey.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) {
const dummy = new ListNode()
let tail = dummy

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

if(list1) {
tail.next = list1
} else if(list2) {
tail.next = list2
}
return dummy.next
};
48 changes: 48 additions & 0 deletions word-search/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @param {character[][]} board
* @param {string} word
* @return {boolean}
*/
var exist = function(board, word) {
const rows = board.length;
const cols = board[0].length;

function backtrack(r, c, index) {
// 모든 문자를 다 찾았으면 true 반환
if (index === word.length) return true;

// 경계 조건 및 현재 문자가 일치하지 않으면 false
if (
r < 0 || r >= rows ||
c < 0 || c >= cols ||
board[r][c] !== word[index]
) {
return false;
}

// 현재 위치 문자 저장 후, 방문 표시로 덮어쓰기
const temp = board[r][c];
board[r][c] = '#';

// 4방향 탐색: 상하좌우
const directions = [[-1,0], [1,0], [0,-1], [0,1]];
for (const [dr, dc] of directions) {
if (backtrack(r + dr, c + dc, index + 1)) {
return true;
}
}

// 상태 복구
board[r][c] = temp;
return false;
}

// 모든 위치에서 시작 가능
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (backtrack(i, j, 0)) return true;
}
}

return false;
};