Skip to content

Commit aa7ad7c

Browse files
authored
Update 638-shopping-offers.js
1 parent 6726799 commit aa7ad7c

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

638-shopping-offers.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,45 @@
55
* @return {number}
66
*/
77
const shoppingOffers = function (price, special, needs) {
8-
return helper(price, special, needs, 0);
8+
return helper(price, special, needs, 0)
99
}
1010

11-
1211
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+
}
2424

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+
)
2831
}
32+
}
2933

30-
return local_min;
34+
return local_min
3135
}
3236

3337
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+
}
3842

39-
return total;
43+
return total
4044
}
4145

46+
4247
// another
4348

4449
/**

0 commit comments

Comments
 (0)