itcode
Senior Member
Tiktok nghe bảo còn căng hơn FAANGhồ sơ bị tiktok cho tạch luôn vòng resume, còn k có OA mà làm
via theNEXTvoz for iPhone
Tiktok nghe bảo còn căng hơn FAANGhồ sơ bị tiktok cho tạch luôn vòng resume, còn k có OA mà làm
Giỏi quá bác ơi
Xịn quá fen
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;
}
};
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;
}
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
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
};
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
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
chúc may mắn nhé bác. Thấy bác chia sẻ pv faang từ năm ngoái thì phảicode giải trí trong lúc đợi kết quả pv
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
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
}
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;
}
};
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;
}
};
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 à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
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
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
Loop thì phải là stack overflow chứ nhỉ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 à
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 xemLoop thì phải là stack overflow chứ nhỉ
Em chạy bằng online compiler không lỗi, nhưng submit thì lỗiC++: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
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;
}
};