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

C++:
class Solution {
public:
    int maxSubarrayLength(vector<int> const& nums, int k) {
        unordered_map<int, queue<int>> m;
        int left = 0, res = 0;
        for (int i = 0; i < nums.size(); ++i) {
            int e = nums[i];
            m[e].push(i);
            while (m[e].front() < left) m[e].pop();
            if (m[e].size() > k) left = m[e].front() + 1, m[e].pop();
            else res = max(res, i - left + 1);
        }
        return res;
    }
};
 
Java:
    public int maxSubarrayLength(int[] nums, int k) {
        Map<Integer,Integer> fequency = new HashMap<>();
        int right = 0, left = 0, ans = 0, n = nums.length;
        while(right < n){
            fequency.put(nums[right],fequency.getOrDefault(nums[right], 0)+1);
            while(fequency.get((nums[right])) > k){
                fequency.put(nums[left],fequency.get(nums[left])-1);
                left++;
            }
            ans = Math.max(ans,1 + right - left);
            right++;
        }
        
        return ans;
    }
 
code giải trí trong lúc đợi kết quả pv
1vnd6cj.gif



Python:
class Solution:
    def maxSubarrayLength(self, nums: List[int], k: int) -> int:
        dic = {}
        left = right = 0
        n = len(nums)
        ans = 0
        while right < n:
            while right < n and dic.get(nums[right], 0) < k:
                dic[nums[right]] = dic.get(nums[right], 0) + 1
                right += 1
            
            ans = max(ans, right - left)
            dic[nums[left]] -= 1
            left += 1
        return ans
 
JavaScript:
function maxSubarrayLength(nums: number[], k: number): number {
    let left: number = 0, right: number = 0;
    let max: number = 0;
    let map: Map<number, number> = new Map();

    while (right < nums.length) {
        if (map.has(nums[right])) {
            map.set(nums[right], map.get(nums[right]) + 1)
        } else {
            map.set(nums[right], 1)
        }

        while (map.get(nums[right]) > k) {
            map.set(nums[left], map.get(nums[left]) - 1)
            left++
        }

        max = Math.max(right - left + 1, max)
        right++
    }

    return max
};
 
Qua k làm nên nay submit 2 bài luôn, đều là sliding window
Code:
class Solution:
    def maxSubarrayLength(self, nums: List[int], k: int) -> int:
        l, r = 0, 0
        res = 0
        counter = {}
        while r < len(nums):
            counter[nums[r]] = counter.get(nums[r], 0) + 1
            while counter[nums[r]] > k and l < r:
                counter[nums[l]] -= 1
                l += 1
            res = max(res, r-l+1)
            r += 1
        return res
Code:
class Solution:
    def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
        if k <= 1:
            return 0

        left, right, product, count = 0, 0, 1, 0
        n = len(nums)

        while right < n:
            product *= nums[right]
            while product >= k:
                product //= nums[left]
                left += 1
            count += 1 + (right - left)
            right += 1

        return count
 
Last edited:
code giải trí trong lúc đợi kết quả pv
1vnd6cj.gif



Python:
class Solution:
    def maxSubarrayLength(self, nums: List[int], k: int) -> int:
        dic = {}
        left = right = 0
        n = len(nums)
        ans = 0
        while right < n:
            while right < n and dic.get(nums[right], 0) < k:
                dic[nums[right]] = dic.get(nums[right], 0) + 1
                right += 1
           
            ans = max(ans, right - left)
            dic[nums[left]] -= 1
            left += 1
        return ans
chúc may mắn nhé bác. Thấy bác chia sẻ pv faang từ năm ngoái thì phải
 
class Solution {
public int maxSubarrayLength(int[] nums, int k) {
int left = 0, right = 0, max = 0;
HashMap<Integer, Integer> map = new HashMap<>();
while (right < nums.length) {
map.put(nums
, map.getOrDefault(nums
,0) + 1);

while (map.get(nums
)> k) {
map.put(nums
, map.get(nums
) - 1);
left++;
}
max = Math.max(max, right - left + 1);
right++;
}
return max;
}
}
 
Code xấu, nhưng pass.

Code:
pub fn max_subarray_length(nums: Vec<i32>, k: i32) -> i32 {
    use std::collections::HashMap;

    let mut left = 0;
    let mut right = 0;
    let mut freq = HashMap::new();
    freq.insert(nums[0], 1);
    let mut max_len = 1;
    while left < nums.len() {
        right += 1;
        if right == nums.len() {
            break;
        }
        let mut count = *freq
            .entry(nums[right])
            .and_modify(|count| *count += 1)
            .or_insert(1);
        if count <= k {
            max_len = max_len.max(right - left + 1);
        } else {
            while count > k {
                freq.entry(nums[left])
                    .and_modify(|count| *count -= 1)
                    .or_insert(0);
                if nums[left] == nums[right] {
                    count -= 1;
                }
                left += 1;
            }
        }
    }
    max_len as i32
}
 
C++:
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        int ans = 0;
        int l = 0, r = 0;
        int n = nums.size();
        while (r < n) {
            int tmp = nums[r];
            mp[tmp]++;
            if (mp[tmp] <= k) {
                ans = max(ans, r - l + 1);
                r++;
            } else {
                while (mp[tmp] > k) {
                    mp[nums[l]]--;
                    l++;
                }
            }
        }
        return ans;
    }
};

cho em hỏi sao code em lại bị lỗi heap-buffer-overflow với =((
 
C++:
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        auto s = 0;
        auto re = 1;
        unordered_map<int, int> mFre;
        for(auto e=0;e<nums.size();++e)
        {
            mFre[nums[e]]++;
            if(mFre[nums[e]] > k)
            {
                re = max (re, e-s);
                while(s<e) {
                    auto tmp = nums[s];
                    mFre[tmp]--;
                    ++s;
                    if(tmp == nums[e]) break;
                }
            }
            else
                re = max (re, e-s+1);
        }
        return re;
    }
};
 
C++:
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        unordered_map<int, int> mp;
        int ans = 0;
        int l = 0, r = 0;
        int n = nums.size();
        while (r < n) {
            int tmp = nums[r];
            mp[tmp]++;
            if (mp[tmp] <= k) {
                ans = max(ans, r - l + 1);
                r++;
            } else {
                while (mp[tmp] > k) {
                    mp[nums[l]]--;
                    l++;
                }
            }
        }
        return ans;
    }
};

cho em hỏi sao code em lại bị lỗi heap-buffer-overflow với=((
Ngó qua vào case else dưới là ko r ko tăng được nên cứ mãi gây loop vô hạn bác à
 
Code:
class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        l, r, res, mx, mx_count, n = 0, 0, 0, max(nums), 0, len(nums)
    
        while r < len(nums):
            if nums[r] == mx:
                mx_count += 1

            while mx_count >= k:
                res += (n-r)  # if current sub array satisfy the condition, adding any more element to the right also works
                if nums[l] == mx:
                    mx_count -= 1
                l += 1
            r += 1

        return res
 
Python:
class Solution:
    def countSubarrays(self, nums: List[int], k: int) -> int:
        max_element = max(nums)
        count, left, result = 0, 0, 0
        n = len(nums)

        for right, num in enumerate(nums):
            if num == max_element:
                count += 1
            
            while count >= k:
                result += (n - right)
                if nums[left] == max_element:
                    count -= 1
                left += 1
        return result
 
C++:
class Solution {
public:
    long long countSubarrays(vector<int>& nums, int k) {
        auto max_ele = *max_element(nums.begin(), nums.end());
        auto s = -1;
        long long re = 0;
        auto count_max =0;
        for(auto e=0;e<nums.size();++e)
        {
            if(nums[e] == max_ele)
            {
                ++count_max;
                while(count_max>=k && s<=e)
                {
                    ++s;
                    if(nums[s] == max_ele)
                    {
                        --count_max;
                        break;
                    }
                }
            }
            re += s+1;
        }
        return re;
    }
};

Loop thì phải là stack overflow chứ nhỉ
Bác thử in cout cái giá trị "r" ra xem
 
C++:
class Solution {
public:
    long long countSubarrays(vector<int>& nums, int k) {
        auto max_ele = *max_element(nums.begin(), nums.end());
        auto s = -1;
        long long re = 0;
        auto count_max =0;
        for(auto e=0;e<nums.size();++e)
        {
            if(nums[e] == max_ele)
            {
                ++count_max;
                while(count_max>=k && s<=e)
                {
                    ++s;
                    if(nums[s] == max_ele)
                    {
                        --count_max;
                        break;
                    }
                }
            }
            re += s+1;
        }
        return re;
    }
};


Bác thử in cout cái giá trị "r" ra xem
Em chạy bằng online compiler không lỗi, nhưng submit thì lỗi =((
 
Mấy nay toàn sliding window không, làm phát ngán rồi, :beat_shot:
C++:
class Solution {
public:
    long long countSubarrays(vector<int>& nums, int k) {
        int max = *max_element(nums.begin(), nums.end());
        long long ret = 0;
        for (int i = 0, j = 0, cur = 0; j < nums.size(); ++j) {
            cur += nums[j] == max;
            while (cur >= k) {
                cur -= nums[i] == max;
                i++;
            }
            ret += i;
        }
        return ret;
    }
};
 
Back
Top