HomeSoftware DevelopmentMinimal time required by n automobiles to journey by way of all...

Minimal time required by n automobiles to journey by way of all the m roads


Given m roads and n automobiles. The automobiles are numbered from 1 to n. You might be additionally given an array arr[] of measurement m, every street has a worth arr[i] – the index of a automobile that runs quick on this street. If a automobile is quick on a street, then it travels throughout the street in 1 hour, else it takes 2 hours (if not proficient) to journey by way of this street. Discover out the minimal time required by these n automobiles to journey by way of all of those m roads. 

All automobiles run independently of one another and every automobile can run on a single street directly. All roads have the identical size. 

Examples: 

Enter:  N = 2, M = 4, arr[] = {1, 2, 1, 2}
Output: 2
Clarification: The primary automobile will run on the first and third roads and the Second automobile can be working on the 2nd and 4th roads. Each automobiles accomplished 2 roads in 2 hours as each are proficient in touring their corresponding roads.

Enter: N = 2, M = 4, arr[] = {1, 1, 1, 1}
Output: 3
Clarification: The primary automobile will run on the first, 2nd, and third roads, whereas the 4th street can be assigned to the 2nd automobile. The primary automobile can be ending his street in 3 hours, whereas the second automobile spends 2 hours (because the 2nd automobile just isn’t proficient in touring the 4th street).

Strategy: The issue will be solved based mostly on the next remark.

If you need to use the automobiles in such a manner that each one roads will be traveled in time x, Then, you possibly can full all these roads utilizing x + 1 or extra time as properly.

Observe the steps talked about beneath to implement the thought:

  • As we’re binary trying to find the reply, we’ve to outline the higher sure. 
  • Within the Worst case, the full time will be 2 * m hours to journey by way of all roads. For instance: if you assign all roads to a single automobile, and this automobile just isn’t proficient in any of those roads.
  • To test whether or not all roads will be accomplished within the given time we can be making a lambda perform.
  • The perform can be protecting monitor of free roads and wanted roads. 
  • If the given time is bigger than the full roads assigned to a automobile.
    • Add the additional time to the free roads slot.
  • Else, Add the left roads to the wanted street.
  • In the long run, if wanted roads are larger than the free roads slot, Thus it’s not doable to finish all the roads within the given time quantity.

Under is the Implementation of the above strategy:

C++

// C++ code of the above strategy
#embrace <bits/stdc++.h>
utilizing namespace std;

// Operate to calcuate minimal
// time required
void minTimeRequired(int n, int m, vector<int> arr)
{

    // cnt to rely the full variety of
    // street the ith automobile is proficient in
    vector<int> cnt(n + 1);

    // Calculating the full no. of
    // proficient roads that every
    // automobile can do
    for (int i = 0; i < m; i++)

        cnt[arr[i]]++;

    // Lambda perform to test climate
    // given time is sufficient to full
    // all m roads utilizing n automobiles
    auto test = [&](int time) {
        lengthy lengthy free = 0, wanted = 0;

        for (int i = 1; i <= n; i++) {
            if (time >= cnt[i]) {

                // Storing the variety of
                // roads that may be achieved
                // if the offered time is
                // greater than the variety of
                // environment friendly roads assigned
                // to a automobile
                lengthy lengthy extraTime = (time - cnt[i]);

                // We're dividing the
                // distinction by 2 as a result of
                // as it's talked about within the
                // query if the automobile is
                // not proficient then 2
                // sec are required to
                // full a street.
                free += extraTime / 2;
            }
            else {

                // Storing the variety of
                // roads which might be left
                wanted += cnt[i] - time;
            }
        }

        // If free street slots are larger
        // than or equal to the wanted,
        // then it's doable to finish
        // all m roads within the given time

        return wanted <= free;
    };

    // Most required time in worst
    // case is 2*m, in the event you assign all
    // roads to a single automobile, and this automobile
    // just isn't proficient in any of
    // these roads.
    int l = 0, r = 2 * m;
    int minTime = -1;

    // Binary Search on minTime
    whereas (l <= r) {

        int m = (l + r) / 2;
        if (test(m)) {

            // If we're capable of full
            // all roads utilizing m unit of
            // time then test with lesser
            // time once more
            minTime = m;

            r = m - 1;
        }
        else {
            l = m + 1;
        }
    }

    cout << minTime << 'n';
}

// Driver Code
int predominant()
{
    int n = 2, m = 4;
    vector<int> arr(m);
    arr = { 1, 2, 1, 2 };

    // Operate name
    minTimeRequired(n, m, arr);

    return 0;
}

Time Complexity: O(n*logm)
Auxiliary Area: O(n)

Associated Articles:

RELATED ARTICLES

Most Popular

Recent Comments