【发布时间】:2021-07-31 19:07:50
【问题描述】:
最小时间要求问题:
给定一个由 N 个字符组成的字符串 S(表示要 执行)和一个正整数K,任务是找到最小值 按给定顺序完成所有给定任务所需的时间,例如 每个任务需要一个单位的时间并且每个任务的类型相同 必须以 K 个单位的间隔执行。
链接到这里的问题 ->link
下面的部分看不懂,具体是什么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.
【问题讨论】:
-
该代码无法编译。不只是复制/粘贴其他人的解决方案可能是一个很好的理由。
-
那个网站到处都是写得不好和损坏的代码,不幸的是,这也不例外。