Skip to content

Commit 79c9a2d

Browse files
committed
Add solution #1199
1 parent 146e2fa commit 79c9a2d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@
11171117
1196|[How Many Apples Can You Put into the Basket](./solutions/1196-how-many-apples-can-you-put-into-the-basket.js)|Easy|
11181118
1197|[Minimum Knight Moves](./solutions/1197-minimum-knight-moves.js)|Medium|
11191119
1198|[Find Smallest Common Element in All Rows](./solutions/1198-find-smallest-common-element-in-all-rows.js)|Medium|
1120+
1199|[Minimum Time to Build Blocks](./solutions/1199-minimum-time-to-build-blocks.js)|Hard|
11201121
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
11211122
1201|[Ugly Number III](./solutions/1201-ugly-number-iii.js)|Medium|
11221123
1202|[Smallest String With Swaps](./solutions/1202-smallest-string-with-swaps.js)|Medium|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1199. Minimum Time to Build Blocks
3+
* https://leetcode.com/problems/minimum-time-to-build-blocks/
4+
* Difficulty: Hard
5+
*
6+
* You are given a list of blocks, where blocks[i] = t means that the i-th block needs
7+
* t units of time to be built. A block can only be built by exactly one worker.
8+
*
9+
* A worker can either split into two workers (number of workers increases by one) or
10+
* build a block then go home. Both decisions cost some time.
11+
*
12+
* The time cost of spliting one worker into two workers is given as an integer split.
13+
* Note that if two workers split at the same time, they split in parallel so the cost
14+
* would be split.
15+
*
16+
* Output the minimum time needed to build all blocks.
17+
*
18+
* Initially, there is only one worker.
19+
*/
20+
21+
/**
22+
* @param {number[]} blocks
23+
* @param {number} split
24+
* @return {number}
25+
*/
26+
var minBuildTime = function(blocks, split) {
27+
const minHeap = new PriorityQueue((a, b) => a - b);
28+
29+
for (const block of blocks) {
30+
minHeap.enqueue(block);
31+
}
32+
33+
while (minHeap.size() > 1) {
34+
const first = minHeap.dequeue();
35+
const second = minHeap.dequeue();
36+
37+
const mergedTime = split + Math.max(first, second);
38+
minHeap.enqueue(mergedTime);
39+
}
40+
41+
return minHeap.dequeue();
42+
};

0 commit comments

Comments
 (0)