Skip to content

Commit 3c37151

Browse files
authored
Merge pull request #1820 from sooooo-an/main
[sooooo-an] WEEK 04 Solutions
2 parents 038e655 + 5a69f49 commit 3c37151

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

combination-sum/sooooo-an.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const result: number[][] = [];
3+
4+
const dfs = (start: number, path: number[], sum: number) => {
5+
if (sum === target) {
6+
result.push([...path]);
7+
return;
8+
}
9+
10+
if (sum > target) {
11+
return;
12+
}
13+
14+
for (let i = start; i < candidates.length; i++) {
15+
path.push(candidates[i]);
16+
dfs(i, path, sum + candidates[i]);
17+
path.pop();
18+
}
19+
};
20+
21+
dfs(0, [], 0);
22+
return result;
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function findMin(nums: number[]): number {
2+
let left = 0,
3+
right = nums.length - 1;
4+
5+
while (left < right) {
6+
const mid = left + Math.floor((right - left) / 2);
7+
if (nums[mid] > nums[right]) {
8+
left = mid + 1;
9+
} else {
10+
right = mid;
11+
}
12+
}
13+
14+
return nums[left];
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function maxDepth(root: TreeNode | null): number {
2+
if (!root) return 0;
3+
const left = maxDepth(root.left);
4+
const right = maxDepth(root.right);
5+
return Math.max(left, right) + 1;
6+
}

merge-two-sorted-lists/sooooo-an.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function mergeTwoLists(
2+
list1: ListNode | null,
3+
list2: ListNode | null
4+
): ListNode | null {
5+
const result = new ListNode();
6+
let tail = result;
7+
8+
while (list1 !== null && list2 !== null) {
9+
if (list1.val <= list2.val) {
10+
tail.next = list1;
11+
list1 = list1.next;
12+
} else {
13+
tail.next = list2;
14+
list2 = list2.next;
15+
}
16+
tail = tail.next;
17+
}
18+
19+
tail.next = list1 !== null ? list1 : list2;
20+
21+
return result.next;
22+
}

0 commit comments

Comments
 (0)