diff --git a/coin-change/j2h30728.ts b/coin-change/j2h30728.ts new file mode 100644 index 0000000000..4642b1ad9f --- /dev/null +++ b/coin-change/j2h30728.ts @@ -0,0 +1,14 @@ +function coinChange(coins: number[], amount: number): number { + const dp = new Array(amount + 1).fill(amount + 1); + dp[0] = 0; + + for (let i = 1; i <= amount; i++) { + for (const coin of coins) { + if (i - coin >= 0) { + dp[i] = Math.min(dp[i], 1 + dp[i - coin]); + } + } + } + + return dp[amount] > amount ? -1 : dp[amount]; +}; diff --git a/find-minimum-in-rotated-sorted-array/j2h30728.ts b/find-minimum-in-rotated-sorted-array/j2h30728.ts new file mode 100644 index 0000000000..fbbbe87a70 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/j2h30728.ts @@ -0,0 +1,12 @@ +function findMin(nums: number[]): number { + const n = nums.length - 1; + let last = nums[n]; + let left = 0, right = n; + + while(left < right){ + const mid = (left + right) >> 1; + if(nums[mid] > last) left = mid + 1; + else right = mid; + } + return nums[left]; +}; diff --git a/maximum-depth-of-binary-tree/j2h30728.ts b/maximum-depth-of-binary-tree/j2h30728.ts new file mode 100644 index 0000000000..724968007d --- /dev/null +++ b/maximum-depth-of-binary-tree/j2h30728.ts @@ -0,0 +1,20 @@ +/** + * 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 { + if(!root) return 0; + const maxLeft = maxDepth(root.left); + const maxRight = maxDepth(root.right); + return Math.max(maxLeft, maxRight) + 1; +}; diff --git a/merge-two-sorted-lists/j2h30728.ts b/merge-two-sorted-lists/j2h30728.ts new file mode 100644 index 0000000000..bb430b483e --- /dev/null +++ b/merge-two-sorted-lists/j2h30728.ts @@ -0,0 +1,29 @@ +/** + * 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 { + const root = new ListNode(); + let tail = root; + + while(list1 !== null && list2 !== null){ + if(list1.val <= list2.val){ + tail.next = list1; + list1 = list1.next; + }else{ + tail.next = list2; + list2 = list2.next; + } + tail = tail.next; + } + tail.next = list1 !== null ? list1 : list2; + return root.next; +};