7.8暑假算法训练营打卡

Cauchy 2022-07-08 08:52:48

 

class Solution {
public:
    int minimumDifference(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int res = 2e9;
        for (int i = nums.size() - 1; i >= k - 1; i--) res = min(res, nums[i] - nums[i - k + 1]);
        return res;
    }
};

class Solution {
public:
    int numSubarrayProductLessThanK(vector<int>& nums, int k) {
        int n = nums.size();
        int r = 0, res = 0;
        int now = 1;
        for (int i = 0; i < n; i++) {
            if(r <= i) r = i;
            while(r < n && now * nums[r] < k) now *= nums[r], r ++;
            res += max(0, r - i - 1);
            if(r > i) now /= nums[i];
        }
        for (auto x: nums) if(x < k) res += 1;
        return res;
    }
};

 

class Solution {
public:
    int slove(string &nums, char x, int k) {
        int n = nums.size();
        int l = 0, r = 0;
        int cnt = 0, historyMax = 0;
        for (r = 0; r < n; r++) {
            if(nums[r] == x) {
                cnt++;
            }
            historyMax = max(historyMax, cnt);

            if(r - l + 1 > historyMax + k) {
                if(nums[l] == x) cnt--;
                l++;
            }
        }
        return n - l;
    }
    int maxConsecutiveAnswers(string answerKey, int k) {
        return max(slove(answerKey, 'T', k), slove(answerKey, 'F', k));
    }
};

 

void solve() {
	int n = 1; cin >> n;
	vi a(n, 0);
	int cnt = 0;
	bool o = true;
	
	rep(i, 0, n) {
		cin >> a[i];
		if(a[i] == 1) cnt ++;
		if(a[i] % 2== 0) o = false;
	}
	sort(all(a));
	
	bool oo = false;
	for (int i = 0; i < n - 1; i++) {
		if(a[i] + 1 == a[i + 1]) {
			oo = true;
		}
	}
 
	if(cnt && oo) {
		cout << "NO" << endl; 
	} else {
		cout << "YES" << endl;
	}
}

 

...全文
29 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,733

社区成员

发帖
与我相关
我的任务
社区描述
和众多高校算法内卷分子,一起学习和交流算法那。浓郁的算法交流氛围,拒绝躺平,有效内卷。加入我们,私信我拉你入核心内卷群。
算法数据结构leetcode 个人社区
社区管理员
  • 执 梗
  • Dream-Y.ocean
  • ღCauchyོꦿ࿐
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

 刷题!

试试用AI创作助手写篇文章吧