diff --git a/combination-sum/sooooo-an.ts b/combination-sum/sooooo-an.ts new file mode 100644 index 000000000..2e552d15e --- /dev/null +++ b/combination-sum/sooooo-an.ts @@ -0,0 +1,23 @@ +function combinationSum(candidates: number[], target: number): number[][] { + const result: number[][] = []; + + const dfs = (start: number, path: number[], sum: number) => { + if (sum === target) { + result.push([...path]); + return; + } + + if (sum > target) { + return; + } + + for (let i = start; i < candidates.length; i++) { + path.push(candidates[i]); + dfs(i, path, sum + candidates[i]); + path.pop(); + } + }; + + dfs(0, [], 0); + return result; +} diff --git a/find-minimum-in-rotated-sorted-array/sooooo-an.ts b/find-minimum-in-rotated-sorted-array/sooooo-an.ts new file mode 100644 index 000000000..16f93e1a4 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/sooooo-an.ts @@ -0,0 +1,15 @@ +function findMin(nums: number[]): number { + let left = 0, + right = nums.length - 1; + + while (left < right) { + const mid = left + Math.floor((right - left) / 2); + if (nums[mid] > nums[right]) { + left = mid + 1; + } else { + right = mid; + } + } + + return nums[left]; +} diff --git a/maximum-depth-of-binary-tree/sooooo-an.ts b/maximum-depth-of-binary-tree/sooooo-an.ts new file mode 100644 index 000000000..3e8078a00 --- /dev/null +++ b/maximum-depth-of-binary-tree/sooooo-an.ts @@ -0,0 +1,6 @@ +function maxDepth(root: TreeNode | null): number { + if (!root) return 0; + const left = maxDepth(root.left); + const right = maxDepth(root.right); + return Math.max(left, right) + 1; +} diff --git a/merge-two-sorted-lists/sooooo-an.ts b/merge-two-sorted-lists/sooooo-an.ts new file mode 100644 index 000000000..f84245582 --- /dev/null +++ b/merge-two-sorted-lists/sooooo-an.ts @@ -0,0 +1,22 @@ +function mergeTwoLists( + list1: ListNode | null, + list2: ListNode | null +): ListNode | null { + const result = new ListNode(); + let tail = result; + + 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 result.next; +}