|
5 | 5 | * @return {number}
|
6 | 6 | */
|
7 | 7 | const shoppingOffers = function (price, special, needs) {
|
8 |
| - return helper(price, special, needs, 0); |
| 8 | + return helper(price, special, needs, 0) |
9 | 9 | }
|
10 | 10 |
|
11 |
| - |
12 | 11 | function helper(price, special, needs, pos) {
|
13 |
| - let local_min = directPurchase(price, needs); |
14 |
| - for (let i = pos; i < special.length; i++) { |
15 |
| - let offer = special[i]; |
16 |
| - let temp = []; |
17 |
| - for (let j = 0; j < needs.length; j++) { |
18 |
| - if (needs[j] < offer[j]) { // check if the current offer is valid |
19 |
| - temp = null; |
20 |
| - break; |
21 |
| - } |
22 |
| - temp.push(needs[j] - offer[j]); |
23 |
| - } |
| 12 | + let local_min = directPurchase(price, needs) |
| 13 | + for (let i = pos; i < special.length; i++) { |
| 14 | + let offer = special[i] |
| 15 | + let temp = [] |
| 16 | + for (let j = 0; j < needs.length; j++) { |
| 17 | + if (needs[j] < offer[j]) { |
| 18 | + // check if the current offer is valid |
| 19 | + temp = null |
| 20 | + break |
| 21 | + } |
| 22 | + temp.push(needs[j] - offer[j]) |
| 23 | + } |
24 | 24 |
|
25 |
| - if (temp !== null) { // use the current offer and try next |
26 |
| - local_min = Math.min(local_min, offer[offer.length - 1] + helper(price, special, temp, i)); |
27 |
| - } |
| 25 | + if (temp !== null) { |
| 26 | + // use the current offer and try next |
| 27 | + local_min = Math.min( |
| 28 | + local_min, |
| 29 | + offer[offer.length - 1] + helper(price, special, temp, i), |
| 30 | + ) |
28 | 31 | }
|
| 32 | + } |
29 | 33 |
|
30 |
| - return local_min; |
| 34 | + return local_min |
31 | 35 | }
|
32 | 36 |
|
33 | 37 | function directPurchase(price, needs) {
|
34 |
| - let total = 0; |
35 |
| - for (let i = 0; i < needs.length; i++) { |
36 |
| - total += price[i] * needs[i]; |
37 |
| - } |
| 38 | + let total = 0 |
| 39 | + for (let i = 0; i < needs.length; i++) { |
| 40 | + total += price[i] * needs[i] |
| 41 | + } |
38 | 42 |
|
39 |
| - return total; |
| 43 | + return total |
40 | 44 | }
|
41 | 45 |
|
| 46 | + |
42 | 47 | // another
|
43 | 48 |
|
44 | 49 | /**
|
|
0 commit comments