一、AcWing周赛 70
1、4618.两个素数
(1)原题链接:4618. 两个素数 - AcWing题库
(2)解题思路:
1、先遍历找到一个素数;
2、然后再从该素数开始往后遍历找到另一个素数,若两个素数的乘积等于x,则直接退出循环,返回这两个素数。
整体思路就是暴力干!
(3)参考代码:
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int x)
{
if(x < 2) return false;
for(int i = 2; i <= x / i; i ++)
{
if(x % i == 0) return false;
}
return true;
}
int main()
{
int x;
cin >> x;
int t1 = 0, t2 = 0;
for(int i = 2; i <= x; i ++) {
if(is_prime(i)) {
t1 = i;
for(int j = i; j <= x; j ++) {
if(is_prime(j)) {
t2 = j;
if(t1 * t2 == x) break;
}
}
}
if(t1 * t2 == x) break;
}
cout << t1 << ' ' << t2 << endl;
return 0;
}
2、4619.减法操作
(1)原题链接:4619. 减法操作 - AcWing题库
(2)解题思路:
1、题目的意思可以转化为先将所有的数都改变为偶数,若不能则返回NO, 反之则返回YES;
2、我们可以通过操作2(任选两个相邻元素,并将两个元素的值各减去 1)将所有的元素转换为偶数。具体操作是遍历到奇数时,将当前元素和与之相邻的下一个元素的值都减1,以此类推。
3、最后再遍历整个数组,若存在小于零的数或者存在奇数,则说明无法满足题目条件。
(3)参考代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 10;
int n;
int a[N];
bool check()
{
for(int i = 0; i < n - 1; i ++ ) {
if(a[i] % 2) {
a[i] --;
a[i + 1] --;
}
}
for(int i = 0; i < n; i ++ ) {
if(a[i] % 2 || a[i] < 0) return false;
}
return true;
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++ ) cin >> a[i];
if(check()) puts("YES");
else puts("NO");
return 0;
}
二、LeetCode单周赛 312
1、6188.按身高排序
(1)原题链接:力扣https://leetcode.cn/problems/sort-the-people/
(2)解题思路:
1、用一个vector<pair<int, int>> tmp保存身高和对应的下标;
2、对tmp进行排序,因为vector<pair<int, int>>排序默认的是第一个值,所以再依次通过下标找到对应的名字即可。
(3)参考代码:
class Solution {
public:
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
vector<pair<int, int>> tmp;
for(int i = 0; i < heights.size(); i ++ ) {
tmp.push_back({heights[i], i});
}
sort(tmp.rbegin(), tmp.rend());
vector<string> res;
for(int i = 0; i < tmp.size(); i ++ ) {
res.push_back(names[tmp[i].second]);
}
return res;
}
};
2、6189.按位与最大的最长子数组
(1)原题链接:力扣https://leetcode.cn/problems/longest-subarray-with-maximum-bitwise-and/
(2)解题思路:
题目意思可以转换为找到最大的数,并且返回其最大的连续长度。具体见代码。
(3)参考代码:
class Solution {
public:
int longestSubarray(vector<int>& nums) {
int maxm = 0;
for(auto& x: nums) maxm = max(maxm, x);
int res = 1;
int cnt = 0;
for(auto& x: nums) {
if(maxm == x) cnt ++;
else {
res = max(res, cnt);
cnt = 0;
}
}
res = max(res, cnt);
return res;
}
};
3、6190.找到所有好下标
(1)原题链接:力扣https://leetcode.cn/problems/find-all-good-indices/
(2)解题思路:
整体思路:先顺序遍历找到满足下标 i
之前 的 k
个元素是 非递增的下标 i 保存到哈希表中,再逆序遍历找到满足下标 i
之后 的 k
个元素是 非递减的下标 i ,判断其是否在哈希表中,若在则将下标保存到答案数组中。
1、用一个队列来保存满足条件(非递增的)的下标 i 对应的元素及其之前的 k 个元素,当队列的长度为 k 时,用哈希表保存满足条件(非递增的)的下标 i ;
2、用另一个队列保存满足条件(非递减的)的下标 i 对应的元素及其之后的 k 个元素,当队列的长度为 k 时,在哈希表中判断该下标是否存在,若存在则保存到答案数组中。
3、对答案数组排序。
(3)参考代码:
class Solution {
public:
vector<int> goodIndices(vector<int>& nums, int k) {
int n = nums.size();
vector<int> res;
unordered_map<int, int> mp;
queue<int> q, q1;
q.push(nums[0]);
for(int i = 1; i < n ; i ++ ) {
if(q.size() == k) {
mp[i] = 1;
if(nums[i] <= q.back()) q.push(nums[i]);
else {
while(!q.empty()) q.pop();
q.push(nums[i]);
}
}
else{
if(nums[i] <= q.back()) q.push(nums[i]);
else {
while(!q.empty()) q.pop();
q.push(nums[i]);
}
}
if(q.size() == k + 1) q.pop();
}
q1.push(nums[n - 1]);
for(int i = n - 2; i >= 0; i -- ) {
if(q1.size() == k) {
if(mp.count(i)) res.push_back(i);
if(nums[i] <= q1.back()) q1.push(nums[i]);
else {
while(!q1.empty()) q1.pop();
q1.push(nums[i]);
}
}
else {
if(nums[i] <= q1.back()) q1.push(nums[i]);
else {
while(!q1.empty()) q1.pop();
q1.push(nums[i]);
}
}
if(q1.size() == k + 1) q1.pop();
}
sort(res.begin(), res.end());
return res;
}
};