返回介绍

solution / 1100-1199 / 1199.Minimum Time to Build Blocks / README_EN

发布于 2024-06-17 01:03:22 字数 5946 浏览 0 评论 0 收藏 0

1199. Minimum Time to Build Blocks

中文文档

Description

You are given a list of blocks, where blocks[i] = t means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker.

A worker can either split into two workers (number of workers increases by one) or build a block then go home. Both decisions cost some time.

The time cost of spliting one worker into two workers is given as an integer split. Note that if two workers split at the same time, they split in parallel so the cost would be split.

Output the minimum time needed to build all blocks.

Initially, there is only one worker.

 

Example 1:

Input: blocks = [1], split = 1
Output: 1
Explanation: We use 1 worker to build 1 block in 1 time unit.

Example 2:

Input: blocks = [1,2], split = 5
Output: 7
Explanation: We split the worker into 2 workers in 5 time units then assign each of them to a block so the cost is 5 + max(1, 2) = 7.

Example 3:

Input: blocks = [1,2,3], split = 1
Output: 4
Explanation: Split 1 worker into 2, then assign the first worker to the last block and split the second worker into 2.
Then, use the two unassigned workers to build the first two blocks.
The cost is 1 + max(3, 1 + max(1, 2)) = 4.

 

Constraints:

  • 1 <= blocks.length <= 1000
  • 1 <= blocks[i] <= 10^5
  • 1 <= split <= 100

Solutions

Solution 1: Greedy + Priority Queue (Min Heap)

First, consider the case where there is only one block. In this case, there is no need to split the worker, just let him build the block directly. The time cost is $block[0]$.

If there are two blocks, you need to split the worker into two, and then let them build the blocks separately. The time cost is $split + \max(block[0], block[1])$.

If there are more than two blocks, at each step you need to consider how many workers to split. This is not easy to handle with forward thinking.

We might as well use reverse thinking, not splitting workers, but merging blocks. We select any two blocks $i$, $j$ for merging. The time to build a new block is $split + \max(block[i], block[j])$.

In order to let the blocks with long time consumption participate in the merge as little as possible, we can greedily select the two blocks with the smallest time consumption for merging each time. Therefore, we can maintain a min heap, take out the two smallest blocks for merging each time, until there is only one block left. The build time of the last remaining block is the answer.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the number of blocks.

class Solution:
  def minBuildTime(self, blocks: List[int], split: int) -> int:
    heapify(blocks)
    while len(blocks) > 1:
      heappop(blocks)
      heappush(blocks, heappop(blocks) + split)
    return blocks[0]
class Solution {
  public int minBuildTime(int[] blocks, int split) {
    PriorityQueue<Integer> q = new PriorityQueue<>();
    for (int x : blocks) {
      q.offer(x);
    }
    while (q.size() > 1) {
      q.poll();
      q.offer(q.poll() + split);
    }
    return q.poll();
  }
}
class Solution {
public:
  int minBuildTime(vector<int>& blocks, int split) {
    priority_queue<int, vector<int>, greater<int>> pq;
    for (int v : blocks) pq.push(v);
    while (pq.size() > 1) {
      pq.pop();
      int x = pq.top();
      pq.pop();
      pq.push(x + split);
    }
    return pq.top();
  }
};
func minBuildTime(blocks []int, split int) int {
  q := hp{}
  for _, v := range blocks {
    heap.Push(&q, v)
  }
  for q.Len() > 1 {
    heap.Pop(&q)
    heap.Push(&q, heap.Pop(&q).(int)+split)
  }
  return q.IntSlice[0]
}

type hp struct{ sort.IntSlice }

func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}
function minBuildTime(blocks: number[], split: number): number {
  const pq = new MinPriorityQueue();
  for (const x of blocks) {
    pq.enqueue(x);
  }
  while (pq.size() > 1) {
    pq.dequeue()!;
    pq.enqueue(pq.dequeue()!.element + split);
  }
  return pq.dequeue()!.element;
}
use std::collections::BinaryHeap;
use std::cmp::Reverse;

impl Solution {
  pub fn min_build_time(blocks: Vec<i32>, split: i32) -> i32 {
    let mut pq = BinaryHeap::new();

    for x in blocks {
      pq.push(Reverse(x));
    }

    while pq.len() > 1 {
      pq.pop();
      let new_element = pq.pop().unwrap().0 + split;
      pq.push(Reverse(new_element));
    }

    pq.pop().unwrap().0
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文