File tree Expand file tree Collapse file tree 3 files changed +72
-0
lines changed
find-minimum-in-rotated-sorted-array Expand file tree Collapse file tree 3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments