当前位置:网站首页>Double week leetcode 84th game
Double week leetcode 84th game
2022-08-08 07:03:00 【Java technology made easy】
leetcode第84场双周赛
一、6141. 合并相似的物品
1. 题目描述

2. 思路分析
解法:模拟
维护一个map<int, int> hash,hash[i]表示价值为i the sum of the weights of the items.由于map是有序的,traverse directlymapThe result can be used as the answer.
3. 代码实现
class Solution {
public:
vector<vector<int>> mergeSimilarItems(vector<vector<int>>& items1, vector<vector<int>>& items2) {
map<int, int> hash;
for (auto& p : items1) hash[p[0]] += p[1];
for (auto& p : items2) hash[p[0]] += p[1];
vector<vector<int>> res;
for (auto& [k, v] : hash)
res.push_back({k, v});
return res;
}
};
二、6142. 统计坏数对的数目
1. 题目描述

2. 思路分析
解法:枚举
The number of bad pairs=The number of all pairs-满足i < j且 j - i == nums[j] - nums[i]的数对数目.
Shift terms on both sides of the equation,变为nums[i] - i == nums[j] - j.So only one needs to be maintainedunordered_map<int, int> mp,mp[i]Indicates satisfaction so farnums[i] - i == x的i有几个.我们枚举j,and subtract it from the answermp[nums[j] - j]的值即可.
时间复杂度为O(n).
3. 代码实现
typedef long long LL;
class Solution {
public:
long long countBadPairs(vector<int>& nums) {
int n = nums.size();
LL ans = n * (n - 1ll) / 2;
unordered_map<int, int> mp;
for (int i = 0; i < n; i ++ ) {
int t = nums[i] - i;
ans -= mp[t];
mp[t] ++;
}
return ans;
}
};
三、6174. 任务调度器 II
1. 题目描述

2. 思路分析
解法:模拟
维护一个unordered_map<int, int> mp,mp[x]表示类型xThe most recent end time of the task.Enumerates all tasks in order,Set the current task type to x,Elapsed before executing the current taskt的时间,那么:
- 若
t - mp[x] >= space,Indicates that the cooldown time has expired,可以直接执行任务.t +=,并更新mp[x] = t. - 若
t - mp[x] < space,Indicates that the cooldown time has not expired.根据题意,The earliest executable type at this timexThe time of the task ismp[x] + space + 1.因此,t = mp[x] + space + 1,并更新mp[x] = t.
3. 代码实现
typedef long long LL;
class Solution {
public:
long long taskSchedulerII(vector<int>& tasks, int space) {
int n = tasks.size();
unordered_map<int, LL> mp;
for (int x : tasks) mp[x] = -1e8;
LL ans = 0;
for (int i = 0; i < n; i ++ ) {
LL &last = mp[tasks[i]];
if (ans - last >= space) ans ++, last = ans;
else ans = last + space + 1, last = ans;
}
return ans;
}
};
四、6144. 将数组排序的最少替换次数
1. 题目描述

2. 思路分析
解法:贪心
Think forward from the second-to-last number.Let the number currently under consideration be x,Its last number is y:
- 若
x <= y,根据贪心,We should not splitx; - 若
x > y,我们需要拆分xso that the maximum value after splitting does not exceedy,And according to greed,The minimum value after splitting should be as large as possible.根据数学知识,The less the number of dismantling,And split as much as possible to make the minimum value as large as possible.因此我们对x进行(k - 1)The split turns it into kk 个数,The minimum of these is ⌊ x / k ⌋ \lfloor x/k \rfloor ⌊x/k⌋.
3. 代码实现
typedef long long LL;
class Solution {
public:
long long minimumReplacement(vector<int>& nums) {
int n = nums.size();
LL res = 0;
for (int i = n - 2, last = nums.back(); i >= 0; i -- ) {
if (nums[i] < last) last = nums[i];
else {
int k = (nums[i] + last - 1) / last;
res += k - 1;
last = nums[i] / k;
}
}
return res;
}
};
关注博主不迷路,内容持续更新中.
边栏推荐
猜你喜欢
随机推荐
二分查找一个数首次与最后出现的位置
带头双向循环链表的增删查改
通过使用fgets()统计文件的行号和使用fgets()/fputs()拷贝文件
CV代码细节总结(一)
C语言实现顺序表的九个常用接口
深度学习基本实用工具
MongoDB的备份与恢复
3.关于剪枝论文的分类和整理(随笔)
P19 美颜相机的实现——基础铺垫1
用C语言实现万年历的代码及思路(详细教程)
神经网络预测值几乎一样,神经网络为什么能预测
神经网络原理的简单介绍,神经网络原理及应用
P22 美颜相机——引入动态数组,优化重绘
Unity中获取一个物体下所有的子物体的方法
Lightning Sixteen Whip
rhcsa——第三天
简悦音乐播放器用到的相关技术点都在这里了(一)
二叉树代码练习
神经网络预测结果分析,神经网络怎么预测数据
MySQL数据库和数据表的增删改查基础








