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.
Mã:
func largestCombination(candidates []int) int {
    bitCounts := make([]int, 32)
    max := 1
    for _, num := range candidates {
        for bit := 0; num > 0; bit++ {
            bitCounts[bit] += num & 1
            num >>= 1
            if bitCounts[bit] > max {
                max = bitCounts[bit]
            }
        }
    }
    return max
}
 
Beat được 95%
 

Tệp đính kèm

  • Screenshot_20241107_141145_Samsung Internet.jpg
    Screenshot_20241107_141145_Samsung Internet.jpg
    328,7 KB · Lượt xem: 28
Python:
class Solution(object):
    def largestCombination(self, candidates):
        """
        :type candidates: List[int]
        :rtype: int
        """

        max_bits = max(candidates).bit_length()
        count = [0] * max_bits
        for i in range(max_bits):
            for can in candidates:
                if can & (1 << i):
                    count[i] += 1
        
        return max(count)
1730967270633.png
 

Tệp đính kèm

  • 1730967266872.png
    1730967266872.png
    11,4 KB · Lượt xem: 25
C-like:
impl Solution {
    pub fn can_sort_array(nums: Vec<i32>) -> bool {
        let (mut segment_bit_count, mut segment_min, mut segment_max, mut prev_segment_max) =
            (nums[0].count_ones(), nums[0], nums[0], 0);

        for num in nums {
            let bit_count = num.count_ones();

            if bit_count == segment_bit_count {
                (segment_min, segment_max) = (segment_min.min(num), segment_max.max(num));
                continue;
            }

            if segment_min < prev_segment_max {
                return false;
            }

            (segment_bit_count, segment_min, segment_max, prev_segment_max) =
                (bit_count, num, num, segment_max);
        }

        if segment_min < prev_segment_max {
            return false;
        }

        true
    }
}

C-like:
impl Solution {
    pub fn largest_combination(candidates: Vec<i32>) -> i32 {
        let mut max_count = 0;

        for i in 0..24 {
            let mut count = 0;
            let mask = 1 << i;

            for num in candidates.iter().copied() {
                if (num & mask != 0) {
                    count +=1;
                }
            }

            max_count = max_count.max(count);
        }

        max_count
    }
}
 
C-like:
impl Solution {
    pub fn can_sort_array(nums: Vec<i32>) -> bool {
        let (mut segment_bit_count, mut segment_min, mut segment_max, mut prev_segment_max) =
            (nums[0].count_ones(), nums[0], nums[0], 0);

        for num in nums {
            let bit_count = num.count_ones();

            if bit_count == segment_bit_count {
                (segment_min, segment_max) = (segment_min.min(num), segment_max.max(num));
                continue;
            }

            if segment_min < prev_segment_max {
                return false;
            }

            (segment_bit_count, segment_min, segment_max, prev_segment_max) =
                (bit_count, num, num, segment_max);
        }

        if segment_min < prev_segment_max {
            return false;
        }

        true
    }
}

C-like:
impl Solution {
    pub fn largest_combination(candidates: Vec<i32>) -> i32 {
        let mut max_count = 0;

        for i in 0..24 {
            let mut count = 0;
            let mask = 1 << i;

            for num in candidates.iter().copied() {
                if (num & mask != 0) {
                    count +=1;
                }
            }

            max_count = max_count.max(count);
        }

        max_count
    }
}
tưởng fency bay nick bên thớt bầu cử rồi :shame:

via theNEXTvoz for iPhone
 
đù mod @thuyvan lộng quyền vậy hả
có kèo mà fency :byebye:


via theNEXTvoz for iPhone
 
có kèo mà fency :byebye:


via theNEXTvoz for iPhone

không đọc thread vote chơi thôi :D
 
Python:
class Solution:
    def largestCombination(self, candidates: List[int]) -> int:
        # 0010000
        # 0010001
        # 0111110
        # 0011000

        # 1000111
        # 0001100
        # 0001110

        count = [0] * 28
        for num in candidates:
            i = 0
            tmp = num
            while tmp:
                if tmp & 1:
                    count[i] += 1
                tmp >>= 1
                i += 1

        return max(count)
 
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.213.760
Quay lại
Lên đầu trang