classSolution{ publicintminimumHammingDistance(int[] source, int[] target, int[][] allowedSwaps){ int n = source.length; HashMap[] a = new HashMap[n]; int[] f = newint[n]; for (int i = 0; i < n; ++i) f[i] = i; for (int[] e : allowedSwaps) merge(f, e[0], e[1]); for (int i = 0; i < n; ++i) find(f, i); for (int i = 0; i < n; ++i) { if (a[f[i]] == null) a[f[i]] = new HashMap(); if (f[i] != i) a[i] = a[f[i]]; int cnt = (int) a[i].getOrDefault(source[i], 0); a[i].put(source[i], cnt + 1); } int res = n; for (int i = 0; i < n; ++i) { int cnt = (int) a[i].getOrDefault(target[i], 0); if (cnt == 0) continue; --res; a[i].put(target[i],cnt - 1); } return res; }
publicintminimumTimeRequired(int[] jobs, int k){ if (jobs.length <= k) { int x = 0; for (int v : jobs) x = Math.max(x, v); return x; } job = jobs; man = newint[k]; res = 0x7fffffff; f(0, 0, 0); return res; }
privatevoidf(int i, int max, int used){ if (i == job.length) { res = Math.min(res, max); return; } int type = Math.min(man.length, used + 1); for (int j = 0; j < type; ++ j) { man[j] += job[i]; f(i + 1, Math.max(max, man[j]), Math.max(used, j + 1)); man[j] -= job[i]; } } }