thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
Java:
class Solution {
    public int largestCombination(int[] candidates) {
        int n = candidates.length;
        int[] dp = new int[24];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= 23; j++) {
                int bit = (int) Math.pow(2, j);
                if ((candidates[i] & bit) != 0) dp[j]++;
            }
        }
        return Arrays.stream(dp).max().getAsInt();
    }
}
 
Python:
class Solution:
    def largestCombination(self, candidates: List[int]) -> int:
        res = 0
        for bit in range(24):
            count = 0
            for can in candidates:
                if can & (1 << bit) > 0:
                    count += 1
            res = max(res, count)
        return res
 
phải xem hint :beat_brick:
C#:
public class Solution {
    public int LargestCombination(int[] candidates)
    {
        var count = new int[28];
        for (int idx = 0; idx < candidates.Length; idx++)
        {
            var i = 27;
            while (candidates[idx] > 0)
            {
                count[i] += candidates[idx] & 1;
                candidates[idx] >>= 1;
                i--;
            }
        }

        return count.Max();
    }
}
 
1730949480700.png

sao cái testcast này lại ra 10 nhỉ mấy bác, 48&62 =48 lớn nhất r phải =2 chứ
kS0RIYB.png
 
Java:
class Solution {
    public int largestCombination(int[] candidates) {
        int n = candidates.length;

        int res = 1;
        int[] freq= new int[25];
        for (int i = 0; i < n; i++) {
            int num = candidates[i];
            int j =0;
            while(num>0){
                if(num%2==1)freq[j]++;
                num=num>>1;
                j++;
            }
        }
        for(int i =24;i>=0;i--){
            if(freq[i]>1){
                res =Math.max(res,freq[i]);
            }
        }

        return res;
    }
}
 
JavaScript:
/**
 * @param {number[]} candidates
 * @return {number}
 */
var largestCombination = function(candidates) {
    const countBit1AtNthBit = Array(24).fill(0);
    for(const candidate of candidates){
        let temp = candidate;
        let index = 23;
        while(temp !== 0){
            const bitAtIndex = temp % 2;
            if(bitAtIndex === 1) countBit1AtNthBit[index]++;
            temp = Math.floor(temp / 2);
            index--;
        }
    }
    return Math.max(...countBit1AtNthBit);
};
 
ủa largest combination là large về size hả, e đọc tưởng largest AND
htM663j.png
em lúc đầu cũng nghĩ thế :beat_brick:
C++:
class Solution {
public:
    int largestCombination(vector<int>& c) {
        vector<int> counter(24, 0);
        int ans = 0;
        for (int i = 0; i < c.size(); i++) {
            for (int j = 0; j < 24; j++) {
                if (c[i] & (1 << j)) counter[j]++;
                ans = max(counter[j], ans);
            }
        }
        return ans;
    }
};
 
Swift:
class Solution {
    func largestCombination(_ candidates: [Int]) -> Int {
        let n = candidates.count
        var countZero = Array(repeating: n, count: 24)
        for can in candidates {
            var can = can
            var index = 0
            while can > 0 {
                if can & 1 == 1 {
                    countZero[index] -= 1
                }
                can >>= 1
                index += 1
            }
        }
        var result = 0
        for count in countZero {
            result = max(result, n - count)
        }
        return result
    }
}
 
C++:
class Solution {
public:
    int largestCombination(vector<int>& candidates) {
        auto mcount = 0;
        auto b = candidates.begin(); auto e = candidates.end();
        for (auto i = 0; i < numeric_limits<unsigned int>::digits; ++i) {
            auto icount = accumulate(b, e, 0, [&i](int a, int c) { return a + ((c >> i) & 1); });
            mcount = ((((mcount - icount) >> 30) | 1) * (mcount - icount) + mcount + icount) >> 1;
        }
        return mcount;
    }
};
 
Sửa lần cuối:
JavaScript:
var largestCombination = function(candidates) {
    const res = Array(32).fill(0);
    for (const n of candidates) {
        for (let i = 0; (1<<i) <= n; i++) {
            if ((1<<i) & n) {
                res[i]++;
            }
        }
    }
    return Math.max(...res);
};
 
LC 7/11/2024 FAST SLOW POINTER
876. Middle of the Linked List

Intuition​

  • FAST SLOW POINTER

Approach​

  • pointer slow : go to next if count is even
  • pointer fast : alway go to next

Complexity​

  • Time complexity:
O(n)
  • Space complexity:
O(1)


Mã:
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        ListNode *s = head;
        ListNode *f = head;
        int count=1;
        while (f!=NULL){
            if (count%2==0)
                s=s->next;
            f=f->next;
            count++;
        }
        return s;
     
    }
};
 
Java:
class Solution {
    public int largestCombination(int[] candidates) {
        int[] bit = new int[24];
        int res = 0;
        for(int c:candidates){
            String bitString = Integer.toBinaryString(c);
            int n = bitString.length();
            for(int i = n-1;i>=0;i--){
                bit[n-1-i]+=bitString.charAt(i)-'0';
                res = Math.max(res,bit[n-1-i]);
            }
        }
        return res;
    }
}
 
LC 2275 GoLang 1.21+
C-like:
func largestCombination(candidates []int) int {
    rs := 0
    mb := int(slices.Max(candidates))
    for bit := 1; bit <= mb; bit <<= 1 {
        cnt := 0
        for _, e := range candidates {
            if e&bit != 0 { cnt++ }
        }
        rs = max(rs, cnt)
    }
    return rs
}
 
Sửa lần cuối:
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.214.267
Quay lại
Lên đầu trang