We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent c3a56e6 commit 0d22d95Copy full SHA for 0d22d95
leetcode/state_reduction/1986.md
@@ -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