当前位置:网站首页>leetcode第84场双周赛
leetcode第84场双周赛
2022-08-08 06:25:00 【Java技术一点通】
leetcode第84场双周赛
一、6141. 合并相似的物品
1. 题目描述

2. 思路分析
解法:模拟
维护一个map<int, int> hash,hash[i]表示价值为i 的物品的重量之和。由于map是有序的,直接把遍历map的结果作为答案即可。
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. 思路分析
解法:枚举
坏数对的数量=所有数对的数量-满足i < j且 j - i == nums[j] - nums[i]的数对数目。
把等式两边移项,变为nums[i] - i == nums[j] - j。因此只需要维护一个unordered_map<int, int> mp,mp[i]表示目前为止满足nums[i] - i == x的i有几个。我们枚举j,并从答案中减去mp[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]表示类型x的任务最近一次的结束时间。按顺序枚举所有任务,设当前任务类型为x,执行当前任务之前已经经过了t的时间,那么:
- 若
t - mp[x] >= space,说明冷却时间已经结束,可以直接执行任务。t +=,并更新mp[x] = t. - 若
t - mp[x] < space,说明冷却时间还未结束。根据题意,此时最早能执行类型x任务的时间是mp[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. 思路分析
解法:贪心
从倒数第二个数开始往前考虑。设当前考虑的数为x,它的后一个数为y:
- 若
x <= y,根据贪心,我们不应拆分x; - 若
x > y,我们需要拆分x使得拆分后的最大值不超过y,且根据贪心,拆分后的最小值要尽量大。根据数学知识,拆的次数越少,且拆得尽量平均才能让最小值尽量大。因此我们对x进行(k - 1)次拆分把它变成 kk 个数,其中的最小值就是 ⌊ 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;
}
};
关注博主不迷路,内容持续更新中。
边栏推荐
猜你喜欢

Vivado安装—Xilinx design tool already exists for 2019.1,specify a different program program group entr

六.Redis 持久化之 RDB

3.关于剪枝论文的分类和整理(随笔)

rhcsa——第二天

Redis实战篇

bp神经网络预测模型原理,神经网络模型怎么预测
![[WUSTCTF2020]CV Maker1](/img/be/989b1ea8597f31f4b82c2edc6345d5.png)
[WUSTCTF2020]CV Maker1

RNN神经网络适用于什么,RNN神经网络基本原理

COSMIC: COmmonSense knowledge for eMotion Identification in Conversations

P20 美颜相机的实现——基础铺垫2
随机推荐
神经网络训练是什么意思,神经网络训练准确率
Analysis of the status quo of the chemical industry: the polyolefin market consumption is nearly 200 million tons
在Mysql的 left/right join 添加where条件
Meta-Learning and in-context Learning
Markdown语法快速入门(以Topyra为例)
九.Redis 集群(cluster 模式)
Refrigerator compressor market status research analysis and development prospect forecast
P21 美颜相机的实现——提速,重绘,撤回
BiLSTM实现imdb情感分类任务
ESP32 温湿度和气体传感器驱动
Equipment industry research report: laser printer market present situation and development trend in the future
传统图像特征提取方法:边缘与角点
Dropout、剪枝、正则化有什么关系?什么是Dropout?
Vivado安装—Xilinx design tool already exists for 2019.1,specify a different program program group entr
PURE(A Frustratingly Easy Approach for Entity and Relation Extraction)
Windows中安装MongoDB的PHP扩展
bp神经网络预测模型原理,神经网络模型怎么预测
MySQL数据库和数据表的增删改查基础
正则爬取豆瓣Top250数据存储到CSV文件(6行代码)
类图是什么?