-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic-programming.js
66 lines (53 loc) · 1.68 KB
/
dynamic-programming.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @param {number[]} price
* @param {number[][]} special
* @param {number[]} needs
* @return {number}
*/
var shoppingOffers = function(price, special, needs) {
let cur = [needs];
const strLen = needs.length;
const least = new Map([[needs.join(','), 0]]);
while (cur.length) {
let next = [];
for (let i = 0, iCur = cur[i]; iCur = cur[i]; ++i) {
for (let j = 0, iSpecial = special[j]; iSpecial = special[j]; ++j) {
const status = [];
for (let m = 0; m < strLen; ++m) {
const snum = iCur[m] - iSpecial[m];
if (snum < 0) {
break;
}
status.push(snum);
}
if (status.length < strLen) {
continue;
}
const _status = status.join(',');
const _least = least.get(_status);
const money = iSpecial[strLen] + least.get(iCur.join(','));
if (!_least || _least > money) {
least.set(_status, money);
next.push(status);
}
}
}
cur = next;
}
const strZero = '0' + ',0'.repeat(strLen - 1);
let leastest = least.get(strZero);
if (undefined === leastest) {
leastest = Number.MAX_SAFE_INTEGER;
}
least.delete(strZero);
for (let [status, money] of least) {
const remains = status.split(',');
for (let i = 0; i < strLen; ++i) {
money += remains[i] * price[i];
}
if (leastest > money) {
leastest = money;
}
}
return leastest;
};