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
12 changes: 12 additions & 0 deletions coin-change/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function coinChange(coins: number[], amount: number): number {
const dp = new Array(amount + 1).fill(amount);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i - coins[j] >= 0) {
dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
}
}
}
return dp[amount] === Number.POSITIVE_INFINITY ? -1 : dp[amount];
}
15 changes: 15 additions & 0 deletions find-minimum-in-rotated-sorted-array/casentino.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

보기 좋은 이분 탐색 코드네요 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function findMin(nums: number[]): number {
let left = 0;
let right = nums.length - 1;

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

if (nums[middle] < nums[right]) {
right = middle;
} else {
left = middle + 1;
}
}
return nums[left];
}
36 changes: 36 additions & 0 deletions maximum-depth-of-binary-tree/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function maxDepth(root: TreeNode | null): number {
function recursive(node: TreeNode | null, count: number) {
if (!node) {
return count;
}
const left = recursive(node.left, count + 1);
const right = recursive(node.right, count + 1);
return left > right ? left : right;
}
return recursive(root, 0);
}
Comment on lines +15 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

알고리즘을 아래처럼 이해했는데, 맞게 이해했다면 좋은 로직이네요!

  1. Leaf Node의 자식은 NULL 값으로 더 갈 수 없으니 깊이 count를 반환한다.
  2. Node의 왼쪽(left), 오른쪽(right) 나누어 DFS를 진행하고 그 중 더 큰 값을 반환한다.

현재 함수는 count를 매개변수로 전달하면서 깊이를 추적하고 있는데요,
깊이를 인자로 들고 다니지 않고, 재귀의 반환 값 자체가 깊이가 되도록 만드는 방식도 한 번 고려해보시면 좋을 것 같습니다.

예를 들어 노드가 없을 때 0을 반환하고,
1 + max(leftDepth, rightDepth) 형태로 계산하면
각 재귀 호출이 자신의 서브트리 깊이를 자연스럽게 return 값으로 전달하게 됩니다.

이렇게 하면 재귀 함수의 매개변수를 줄일 수 있어요! 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그러네요! 말씀해주신 방법으로도 풀어봐야겠어요! 조언 감사드려요 🙇‍♂️


class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}
}
Comment on lines +27 to +36
Copy link
Contributor

Choose a reason for hiding this comment

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

주석을 풀어 두신 이유가 있을까요??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

타입스크립트 컴파일 에러 나길래 선언만 해두었어요!

40 changes: 40 additions & 0 deletions merge-two-sorted-lists/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1 || !list2) {
return list1 ?? list2;
}
function merge(node1: ListNode | null, node2: ListNode | null) {
if (!node1 || !node2) {
return node1 ?? node2;
}
if (node1.val < node2.val) {
node1.next = merge(node1.next, node2);
}
if (node1.val >= node2.val) {
node2.next = merge(node1, node2.next);
}

return node1.val < node2.val ? node1 : node2;
}
return merge(list1, list2);
}
Comment on lines +13 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀로도 이렇게 깔끔한 풀이가 가능하네요! 😮

node1.val < node2.val 기준으로 next를 재귀적으로 이어붙이는 구조가 명확하게 보여서 로직이 한눈에 보이네요!!

저는 재귀가 약해서 이해하는 데 조금 시간이 걸렸는데요 😅

다음에는 참고해서 풀어봐야겠어요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

저도 재귀는 아직 구현하면서도 햇갈려서 조건주는게 많이 어색한것같아요 ㅠ 더 많이 풀어봐야할것같아요!


class ListNode {
val: number;
next: ListNode | null;
constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}
44 changes: 44 additions & 0 deletions word-search/casentino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function exist(board: string[][], word: string): boolean {
const m = board.length;
const n = board[0].length;

const visited = Array.from({ length: m }, () => new Array(n).fill(0));
function findDirection(currentI: number, currentJ: number, findNextIdx: number) {
if (findNextIdx === word.length) {
return true;
}
if (
currentI < 0 ||
currentJ < 0 ||
currentI >= m ||
currentJ >= n ||
board[currentI][currentJ] !== word[findNextIdx]
) {
return false;
}
if (visited[currentI][currentJ] === 1) {
return false;
}
visited[currentI][currentJ] = 1;

const isApproachLastWord =
findDirection(currentI + 1, currentJ, findNextIdx + 1) ||
findDirection(currentI - 1, currentJ, findNextIdx + 1) ||
findDirection(currentI, currentJ - 1, findNextIdx + 1) ||
findDirection(currentI, currentJ + 1, findNextIdx + 1);

if (!isApproachLastWord) {
visited[currentI][currentJ] = 0;
}
return isApproachLastWord;
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
const result = findDirection(i, j, 0);
if (result) {
return true;
}
}
}
return false;
}