Skip to content

Commit e2b5640

Browse files
authored
Merge pull request #2482 from DaleStudy/yihyun-kim1
[yihyun-kim1] WEEK 04 Solutions
2 parents c286f6a + c6260ae commit e2b5640

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

coin-change/yihyun-kim1.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
7+
// dp
8+
const coinChange = (coins, amount) => {
9+
const dp = new Array(amount + 1).fill(Infinity);
10+
dp[0] = 0;
11+
12+
for (let i = 1; i <= amount; i++) {
13+
for (const coin of coins) {
14+
if (i - coin >= 0) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
16+
}
17+
}
18+
}
19+
20+
return dp[amount] === Infinity ? -1 : dp[amount];
21+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const findMin = (nums) => {
6+
let left = 0;
7+
let right = nums.length - 1;
8+
9+
while (left < right) {
10+
const mid = Math.floor((left + right) / 2);
11+
12+
if (nums[mid] > nums[right]) {
13+
left = mid + 1;
14+
} else {
15+
right = mid;
16+
}
17+
}
18+
19+
return nums[left];
20+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
const mergeTwoLists = (list1, list2) => {
14+
const dummy = new ListNode(0);
15+
let current = dummy;
16+
17+
while (list1 !== null && list2 !== null) {
18+
if (list1.val <= list2.val) {
19+
current.next = list1;
20+
list1 = list1.next;
21+
} else {
22+
current.next = list2;
23+
list2 = list2.next;
24+
}
25+
current = current.next;
26+
}
27+
28+
current.next = list1 || list2;
29+
30+
return dummy.next;
31+
};

0 commit comments

Comments
 (0)