
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum Time to Build Blocks in C++
Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.
So, if the input is like blocks = [1,2], and split = 5, then the output will be 7 as we can split the worker into 2 workers in 5 time units then assign each of them to a block so the cost is 5 + maximum of 1 and 2 = 7.
To solve this, we will follow these steps −
define one priority queue pq
for initialize i := 0, when i < size of blocks, update (increase i by 1), do −
insert blocks[i] into pq
while size of pq > 1, do −
delete element from pq
x := top element of pq
delete element from pq
insert split + x into pq
return top element of pq
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int minBuildTime(vector<int>& blocks, int split) { priority_queue<int, vector<int>, greater<int> > pq; for (int i = 0; i < blocks.size(); i++) pq.push(blocks[i]); while (pq.size() > 1) { pq.pop(); int x = pq.top(); pq.pop(); pq.push(split + x); } return pq.top(); } }; main(){ Solution ob; vector<int> v = {1,2}; cout << (ob.minBuildTime(v, 5)); }
Input
{1,2}, 5
Output
7
- Related Articles
- Minimum Time Difference in C++
- Minimum Time Visiting All Points in C++
- Minimum Time to Collect All Apples in a Tree in C++
- Program to determine the minimum cost to build a given string in python
- Find minimum time to finish all jobs with given constraints in C++
- C++ code to find minimum time needed to do all tasks
- How to use Try/catch blocks in C#?
- Program to find minimum time to complete all tasks in python
- Program to find minimum time to finish all jobs in Python
- Finding minimum time difference in an array in JavaScript
- Explain the scope rules related to the statement blocks in C language
- C++ Program to get blocks position after right side rotation
- Find minimum time to finish all jobs with given constraints in Python
- Program to find minimum time required to complete tasks with k time gap between same type tasks in Python
- Minimum Cost to Connect Sticks in C++
