【问题标题】:Minimum time required to complete all tasks without altering their order在不改变顺序的情况下完成所有任务所需的最短时间
【发布时间】: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.

【问题讨论】:

  • 该代码无法编译。不只是复制/粘贴其他人的解决方案可能是一个很好的理由。
  • 那个网站到处都是写得不好和损坏的代码,不幸的是,这也不例外。

标签: c++ hashmap


【解决方案1】:

似乎他们不检查其他人提交的内容是否正确。在这段代码中,只要有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;

【讨论】:

  • 哦,我只是一个初学者,所以我只是从那里复制粘贴它,但我现在明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2011-04-25
  • 2014-12-28
  • 2013-11-20
  • 2022-01-22
  • 1970-01-01
相关资源
最近更新 更多