Skip to content

Commit 0d22d95

Browse files
committed
update 1994.md
1 parent c3a56e6 commit 0d22d95

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

leetcode/state_reduction/1986.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
首先见数据范围猜状压,其次,它的转移与数选择的先后有关,我们可以枚举每一个状态,
2+
从后往前遍历自己,寻找每个状态对应的最优解组合是否最优,这种遍历是`O(3 ^n)`,证明见二项式定理
3+
此外还有另一种遍历方式,需要开另一个记录数组,麻烦点.时间复杂度是`O(n * 2^n)`
4+
```c++
5+
class Solution {
6+
public:
7+
int minSessions(vector<int>& a, int b) {
8+
int n = a.size();
9+
vector <int> f(1 << n, 1 << 29); f[0] = 0;
10+
for(int i = 0; i < (1 << n); i++){
11+
int mask = i, t = 0, idx = 0, s = 0;
12+
while(mask){
13+
if(mask & 1) t += a[idx];
14+
if(t == b) t = 0, s++;
15+
if(t > b) {
16+
s++;
17+
t = a[idx];
18+
}
19+
mask >>= 1;
20+
idx++;
21+
}
22+
f[i] = s + (t > 0);
23+
for(int j = i; j; j = (j - 1) & i){
24+
f[i] = min(f[i], f[j ^ i] + f[j]);
25+
}
26+
}
27+
return f.back();
28+
}
29+
};
30+
```

0 commit comments

Comments
 (0)