0

CSES Solutions - Factory Machines - GeeksforGeeks

 1 month ago
source link: https://www.geeksforgeeks.org/cses-solutions-factory-machines/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

CSES Solutions – Factory Machines

A factory has N machines which can be used to make products. Your goal is to make a total of T products.

For each machine, you know the number of seconds it needs to make a single product, given as array K[]. The machines can work simultaneously, and you can freely decide their schedule.

What is the shortest time needed to make T products?

Examples:

Input: N = 3, T = 7, K[] = {3, 2, 5}
Output: 8
Explanation:

  • Machine 1 will make 2 products. Total time taken = 3 * 2 = 6 seconds.
  • Machine 2 will make 4 products. Total time taken = 2 * 4 = 8 seconds.
  • Machine 3 will make 1 product. Total time taken = 5 * 1 = 5 seconds.

Shortest time to make 7 products = 8 seconds.

Input: N = 2, T = 5, K[] = {1, 2}
Output: 4
Explanation:

  • Machine 1 will make 3 products. Total time taken = 1 * 3 = 3 seconds.
  • Machine 2 will make 2 products. Total time taken = 2 * 2 = 4 seconds

Shortest time to make 5 products = 4 seconds.

Approach: To solve the problem, follow the below idea:

The problem can be solved using Binary Search on Answer. We can binary search for the time needed to make the T products. If we observe carefully, the time to make T products will be at least 1 second and the maximum time to make T products will be when we have only 1 machine and it takes maximum amount of time to make one product. So, we can initialize low = 1 and high = T * max value in K[]. Now, we can find mid and find the total number of products we can making using all machines. If the total number of products is less than T, then it means we need more time so we will shift lo = mid + 1. Otherwise, we shift high = mid – 1 and update the answer. We keep on searching till low <= high and after all the iterations, we can return the final answer.

Step-by-step algorithm:

  • Maintain a function check(mid, N, T, K) to check whether it is possible to make T products in time <= mid.
  • Also maintain a variable ans to store the minimum time to make T products.
  • Declare the range in which our answer can lie, that is low = 1, high = T * max value in K[].
  • Iterate till low <= high,
    • Find the mid in range [low, high] and check if we can make T products in time <= mid.
    • If we can make T products in time <= mid, then update the answer and reduce search space by moving high to mid – 1.
    • Otherwise, move low = mid + 1.
  • After all the iterations, return ans as the final answer.

Below is the implementation of the algorithm:

C++

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

// Make the check function to check if we can make T
// products in time <= mid
bool check(ll mid, ll N, ll T, ll* K)
{
    // Variable to count the number of products made
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        sum += (mid / K[i]);
        if (sum >= T)
            return true;
    }
    return false;
}

// Function to find the minimum time to make T products
ll solve(ll N, ll T, ll* K)
{
    // Define the range in which our answer can lie
    ll low = 1, high = (*max_element(K, K + N)) * T ;
    ll ans;

    // Binary Search for the minimum time to make T products
    while (low <= high) {
        ll mid = (low + high) / 2;

        // Check if we can make T products in time <= mid
        if (check(mid, N, T, K)) {
            // Update the answer and reduce search space by
            // moving high to mid - 1
            ans = mid;
            high = mid - 1;
        }
        else {
            // Reduce the search space by moving low to mid
            // + 1
            low = mid + 1;
        }
    }
    return ans;
}

int main()
{
    // Sample Input
    ll N = 3, T = 7;
    ll K[] = { 3, 2, 5 };

    cout << solve(N, T, K);
}

JavaPythonC#JavaScript

Output
8

Time Complexity: O(N * log(T * max(K[]))), N is the number of machines, T is the number of products and K[] is the array of time which each machine takes to make 1 product.
Auxiliary Space: O(1)

"The DSA course helped me a lot in clearing the interview rounds. It was really very helpful in setting a strong foundation for my problem-solving skills. Really a great investment, the passion Sandeep sir has towards DSA/teaching is what made the huge difference." - Gaurav | Placed at Amazon

Before you move on to the world of development, master the fundamentals of DSA on which every advanced algorithm is built upon. Choose your preferred language and start learning today: 

DSA In JAVA/C++
DSA In Python
DSA In JavaScript
Trusted by Millions, Taught by One- Join the best DSA Course Today!

Recommended Problems
Frequently asked DSA Problems
Last Updated : 21 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK