You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
public int largestCombination(int[] candidates) {
int res = 0;
for (int i = 0; i < 32; ++i) {
int cur = 0;
for (int c : candidates) {
cur += ((c >> i) & 1);
}
res = Math.max(res, cur);
}
return res;
}
}
这道题是周赛第三题。
难点在于combination这里,我用笨方法深度遍历找到所有subset然后求与结果,这样显然TLE并且没有充分利用题目条件的。那么如何利用题目中的与结果和结果需要大于0呢?
那么想到要让多个数的与结果大于0,只要满足这几个数其中一位都等于1即可。回到这道题上,如何找到所有满足与结果大于0的combination呢?那就对一个数,遍历所有位数,如果该位等于1,就算一个combination,然后找到这个位为1的其他数,有多少数量就能有多少个数的combination。
参考资料:
The text was updated successfully, but these errors were encountered: