Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 2275. Largest Combination With Bitwise AND Greater Than Zero #118

Open
Woodyiiiiiii opened this issue May 15, 2022 · 0 comments
Open

Comments

@Woodyiiiiiii
Copy link
Owner

Woodyiiiiiii commented May 15, 2022

这道题是周赛第三题。

难点在于combination这里,我用笨方法深度遍历找到所有subset然后求与结果,这样显然TLE并且没有充分利用题目条件的。那么如何利用题目中的与结果和结果需要大于0呢?

那么想到要让多个数的与结果大于0,只要满足这几个数其中一位都等于1即可。回到这道题上,如何找到所有满足与结果大于0的combination呢?那就对一个数,遍历所有位数,如果该位等于1,就算一个combination,然后找到这个位为1的其他数,有多少数量就能有多少个数的combination。

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;

    }
    
}

参考资料:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant