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
| class Solution { int[] pre1, cache; int k, u;
public int minNumberOfSemesters(int n, int[][] relations, int k) { this.k = k; pre1 = new int[n]; for (int[] e : relations) { pre1[e[1] - 1] |= 1 << (e[0] - 1); } u = (1 << n) - 1; cache = new int[1 << n]; Arrays.fill(cache, -1); return dfs(u); }
private int dfs(int i) { if (i == 0) return 0; if (cache[i] != -1) return cache[i]; int i1 = 0, ci = u ^ i; for (int j = 0; j < pre1.length; j++) { if ((i >> j & 1) > 0 && (pre1[j] | ci) == ci) { i1 |= 1 << j; } } if (Integer.bitCount(i1) <= k) { return cache[i] = dfs(i ^ i1) + 1; } int ans = Integer.MAX_VALUE; for (int j = i1; j > 0; j = (j - 1) & i1) { if (Integer.bitCount(j) == k) { ans = Math.min(ans, dfs(i ^ j) + 1); } } return cache[i] = ans; } }
|