1.AcWing周赛第76场
1.1 4713.反转字符串
1.1.1 原题链接:4713. 反转字符串 - AcWing题库
1.1.2 解题思路:
调用reverse函数进行字符串翻转即可。
1.1.3 参考代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string s, t;
cin >> s >> t;
reverse(s.begin(), s.end());
if(s == t) puts("YES");
else puts("NO");
return 0;
}
1.2 4714.数对
1.2.1 原题链接:4714. 数对 - AcWing题库
1.2.2 解题思路:
题目的意思可以转化为求每个字符的出现次数,然后统计所有的字符的出现次数的平方和即为答案,但是会爆int,所以需要用long long来存储答案。
1.2.3 参考代码:
代码1:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N];
int main()
{
string s;
cin >> s;
int n = s.size();
LL res = 0;
unordered_map<char, int> mp;
for(int i = 0; i < n; i ++ ) mp[s[i]] ++;
for(auto& [k,v]: mp) {
res += 1ll * v * v;
}
cout << res << endl;
return 0;
}
代码2:
#include <iostream>
#include <unordered_map》
using namespace std;
typedef long long LL;
int main () {
unordered_map <char,int> mp;
char ch;
while (cin >> ch) mp[ch]++;
LL ans = 0;
for (char i = 'a';i <= 'z';i++) ans += (LL)mp[i] * mp[i];
for (char i = '0';i <= '9';i++) ans += (LL)mp[i] * mp[i];
cout << ans << endl;
return 0;
}
1.3 4715.构造数组
1.3.1 原题链接:4715. 构造数组 - AcWing题库
1.3.2 解题思路:
前后两次遍历即可。
大致思路其实就是去看连续的'>'和连续的'<',只有这样才会对每个数下限产生制约,否则可以为1。
1.3.3 参考代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1010;
int a[N];
int main()
{
int n;
string s;
cin >> n >> s;
vector<int> a(n, 1);
for(int i = 0; i < s.size(); i ++ ) {
if(s[i] == '=') a[i + 1] = a[i];
else if(s[i] == '<') a[i + 1] = a[i] + 1;
}
for(int i = s.size() - 1; i >= 0; i -- ) {
if(s[i] == '=') a[i] = a[i + 1];
else if(s[i] == '>') a[i] = max(a[i], a[i + 1] + 1);
}
for(int i = 0; i < n; i ++ ) cout << a[i] << ' ';
return 0;
}
2. LeetCode单周赛 318
2.1 6229.对数组执行操作
2.1.1 原题链接:力扣https://leetcode.cn/problems/apply-operations-to-an-array/
2.1.2 解题思路:
具体思路见代码。
2.1.3 参考代码:
class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, 0);
for(int i = 0; i < n - 1; i ++ ) {
if(nums[i] == nums[i + 1]) {
nums[i] = nums[i] * 2;
nums[i + 1] = 0;
}
}
int cnt = 0;
for(int i = 0; i < n; i ++ ) {
if(nums[i]) {
res[cnt++] = nums[i];
}
}
return res;
}
};
2.2 6230.长度为K子数组中的最大和
2.2.1 原题链接:力扣https://leetcode.cn/contest/weekly-contest-318/problems/maximum-sum-of-distinct-subarrays-with-length-k/
2.2.2 解题思路:
1、用定长滑动窗口处理子数组中的元素和。
2、用哈希表统计每个子数组中元素的个数。
3、判断子数组中元素的个数是否等于k,若是,则更新答案res,反之不更新答案res。
2.2.3 参考代码:
typedef long long ll;
class Solution {
public:
long long maximumSubarraySum(vector<int>& nums, int k) {
ll res = 0, tmp = 0;
int n = nums.size();
unordered_map<int, int> mp;
for(int i = 0; i < k; i ++ ) {
tmp += nums[i];
mp[nums[i]] ++;
}
if(mp.size() == k) res = max(res, tmp);
for(int i = k; i < n; i ++ ) {
tmp -= nums[i - k];
tmp += nums[i];
mp[nums[i - k]] --;
mp[nums[i]] ++;
if(mp[nums[i - k]] == 0) mp.erase(nums[i-k]);
if(mp.size() == k && tmp > res) {
res = tmp;
}
}
return res;
}
};
2.3 6231.雇佣K位工人的总代价
2.3.1 原题链接:力扣https://leetcode.cn/problems/total-cost-to-hire-k-workers/
2.3.2 解题思路:
1、用优先队列来维护;
2、每次取出队首元素加到答案中。
2.3.3 参考代码:
typedef long long ll;
typedef pair<int, int> PII;
class Solution {
public:
long long totalCost(vector<int>& costs, int k, int candidates) {
ll res = 0;
int n = costs.size();
int l = candidates, r = n - candidates;
priority_queue<PII, vector<PII>, greater<PII>> q;
if(l < r) {
for(int i = 0; i < l; i ++ ) q.emplace(costs[i], i);
for(int i = r; i < n; i ++ ) q.emplace(costs[i], i);
}
else {
for(int i = 0; i < n; i ++ ) q.emplace(costs[i], i);
}
for(int i = 0; i < k; i ++ ) {
auto t = q.top();
q.pop();
res += t.first;
if(l < r) {
if(t.second < l) q.emplace(costs[l], l ++);
if(t.second >= r) q.emplace(costs[r - 1], r --);
}
}
return res;
}
};