当前位置:网站首页>[LeetCode84双周赛] [模拟] 6174. 任务调度器 II,[贪心&数学] 6144. 将数组排序的最少替换次数
[LeetCode84双周赛] [模拟] 6174. 任务调度器 II,[贪心&数学] 6144. 将数组排序的最少替换次数
2022-08-09 02:24:00 【哇咔咔负负得正】
LeetCode 6174. 任务调度器 II
https://leetcode.cn/problems/task-scheduler-ii/
这道题一开始我模拟做,超时了,参考别人的题解,发现一些步骤上可以简化的。
版本 1
class Solution {
public:
long long taskSchedulerII(vector<int>& tasks, int space) {
unordered_map<int, long long> hash;
long long ans = 0;
for (auto & x : tasks) {
// 若没有做过同一类型的任务,或者中间间隔天数满足 space,ans 结果加一天
if (hash[x] == 0 || ans - hash[x] > space) {
ans++;
// 否则,将 ans 赋值为上一次同一类型的任务的日子 + space 时间间隔 + 1
} else {
ans = hash[x] + space + 1;
}
// 最后将该类型任务日子变为当前的 ans 的日子
hash[x] = ans;
}
return ans;
}
};
版本 2
class Solution {
public:
long long taskSchedulerII(vector<int>& tasks, int space) {
unordered_map<int, long long> hash;
long long ans = 0;
for (auto & x : tasks) {
ans++; // 完成该任务,天数+1
if (hash[x] != 0) {
ans = max(ans, hash[x] + space + 1); // 看看是否要间隔 space 天
}
hash[x] = ans; // 记录完成任务的时间
}
return ans;
}
};
LeetCode 6144. 将数组排序的最少替换次数
https://leetcode.cn/problems/minimum-replacements-to-sort-the-array/
因为最后的数组要满足递增,所以最后一个不要拆!如果最后一个拆了,前面拆的次数也会变多。
从后往前遍历,维护拆完后的数组中的最小值 minValue
,
若 nums[i] > minValue
,对 nums[i]
进行拆分,怎么拆?
拆出来的最大值 >= minValue
- 拆出来地最小份数 k = ⌈ n u m s [ i ] m i n V a l u e ⌉ \lceil \frac{nums[i]}{minValue} \rceil ⌈minValuenums[i]⌉,拆的次数为
k - 1
- 拆出来的最小值要尽可能地大,这样前面的数拆分也不会太小。其实就是让拆出来的每一份更加均匀,
拆出来的最小值 =
⌊ n u m s [ i ] k ⌋ \lfloor \frac{nums[i]}{k} \rfloor ⌊knums[i]⌋
class Solution:
def minimumReplacement(self, nums: List[int]) -> int:
ans = 0
minValue = nums[-1] # 最后一个不用拆
for x in nums[-2::-1]: # 从倒数第二个开始遍历
if x > minValue:
k = ceil(x / minValue) # 上取整
ans += k - 1
minValue = x // k
else:
minValue = x
return ans
边栏推荐
- Force buckled brush problem record 7.1 -- -- -- -- -- 707. The design list
- 点击div内部默认文本被选中
- New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
- Open3D 均匀采样
- 2022 PMP Project Management Certification Exam Registration Guide (1)
- 配置文件的读取-TOML
- 物联网未来:未来五年的预期
- Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
- Maya engine modeling
- 【izpack】使用izpack为你的程序提供安装程序封装
猜你喜欢
不会吧!不会吧!居然还有人不知道重绘以及回流
力扣刷题记录1.5-----367. 有效的完全平方数
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
NPDP改版前最后一次考试!请注意
pytorch相关知识点总结
力扣刷题记录6.1-----203. 移除链表元素
Etcd realize large-scale application service management of actual combat
企业从云服务的承诺支出中获得最大收益的四种方法
力扣刷题记录4.1-----209. 长度最小的子数组
【云计算】XaaS最全介绍(按24字母合集):AaaS、BaaS、CaaS、DaaS、EaaS、FaaS、GaaS、HaaS、IDaaS…
随机推荐
composer的使用记录
企业面临的五大数据安全挑战
中国SSD产业突围有多难?除了技术“瓶颈”还有哪里挑战?
通过安装VNC服务器x11vnc(或vnc4server)和配置x11vnc.service实现远程通过VNC-Viewer访问VNC服务器。
yii2的安装之路
USB 触摸在竖屏时校准
HNUMSC-C语言第一课
力扣刷题记录6.1-----203. 移除链表元素
eladmin容器部署超详细过程
Line segment tree of knowledge
Open3D 均匀采样
Appium常用操作及H5页面元素定位
年金险的安全性怎么样啊?可靠吗?
Yii2开启 Schema 缓存
C#计算SHA1加密和base64编码
mysql 5.7 入坑
Summary of pytorch related knowledge points
JS 截取数组的最后几个元素
C#计算两个时间相差多少天、时、分、秒
边缘计算的三个关键好处