c++ - 在不改变顺序的情况下完成所有任务所需的最短时间
问题描述
最短时间要求问题:
给定一个由 N 个字符(表示要执行的任务)和一个正整数 K 组成的字符串 S,任务是找到按给定顺序完成所有给定任务所需的最短时间,使得每个任务花费一个单位时间,并且每个相同类型的任务必须以 K 个单位的间隔执行。
链接到这里的问题 - >链接
我不明白以下部分,特别是curr_time - map
以下代码中的内容。确实了解是什么curr_time
,但curr_time - map
我不明白。
// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// time required to complete
// tasks without changing their order
void findMinimumTime(string tasks, int K)
{
// Keeps track of the last
// time instant of each task
unordered_map<char, int> map;
// Stores the required result
int curr_time = 0;
// Traverse the given string
for (char c : tasks) {
// Check last time instant of
// task, if it exists before
if (map.find(c) != map.end()) {
// Increment the time
// if task is within
// the K units of time
if (curr_time - map <= K) {
curr_time += K - (curr_time - map) + 1;
}
}
// Update the time of the
// current task in the map
map = curr_time;
// Increment the time by 1
curr_time++;
}
// Print the result
cout << curr_time;
}
// Driver Code
int main()
{
string S = "ABACCA";
int K = 2;
findMinimumTime(S, K);
return 0;
}
// This code is contributed by Kingash.
解决方案
似乎他们不检查其他人提交的内容是否正确。在这段代码中,无论它有什么map
,它都应该有map[c]
:
if (curr_time - map[c] <= K) {
curr_time += K - (curr_time - map[c]) + 1;
}
...
// Update the time of the
// current task in the map
map[c] = curr_time;
推荐阅读
- nosql - Orientdb sql,从多个表中选择
- reactjs - 在 React 中的 style={{}} 中连接 props 和静态值
- spring - 将 spring.jackson.serialization.write-dates-as-timestamps & date-format 设置为全局 ISO1806
- sql-server - 通过选择返回返回值
- python - Django 表单外键保存
- oracle - 从 SSIS 执行 SQL 任务返回 Oracle LONG 值
- sql - 将多个表与单个表关联
- apache - Impala_queries 中的 Nan 值
- python - 如何将此列表转换为 Python 3.7 中的有效 json 对象?
- java - 保存按时间排序的最后 10 个对象列表的最佳数据结构是什么