Skip to content

Commit 6c9305a

Browse files
committed
feat: add solutions to lc problem: No.1199
No.1199.Minimum Time to Build Blocks
1 parent 633e33c commit 6c9305a

File tree

6 files changed

+197
-2
lines changed

6 files changed

+197
-2
lines changed

solution/1100-1199/1199.Minimum Time to Build Blocks/README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,99 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:贪心 + 优先队列(小根堆)**
62+
63+
先考虑只有一个街区的情况,此时不需要分裂工人,直接让他去建造街区,时间花费为 `block[0]`
64+
65+
如果有两个街区,此时需要把工人分裂为两个,然后让他们分别去建造街区,时间花费为 `split + max(block[0], block[1])`
66+
67+
如果有超过两个街区,此时每一步都需要考虑将几个工人进行分裂,正向思维不好处理。
68+
69+
我们不妨采用逆向思维,不分裂工人,而是将街区进行合并。我们选取任意两个街区 $i$, $j$ 进行合并,建造一个新的街区的时间为 `split + max(block[i], block[j])`
70+
71+
为了让耗时长的街区尽可能少参与到合并中,我们可以每次贪心地选取耗时最小的两个街区进行合并。因此,我们可以维护一个小根堆,每次取出最小的两个街区进行合并,直到只剩下一个街区。最后剩下的这个街区的建造时间就是答案。
72+
73+
时间复杂度 $O(n\log n)$。其中 $n$ 是街区数量。
74+
6175
<!-- tabs:start -->
6276

6377
### **Python3**
6478

6579
<!-- 这里可写当前语言的特殊实现逻辑 -->
6680

6781
```python
68-
82+
class Solution:
83+
def minBuildTime(self, blocks: List[int], split: int) -> int:
84+
heapify(blocks)
85+
while len(blocks) > 1:
86+
heappop(blocks)
87+
heappush(blocks, heappop(blocks) + split)
88+
return blocks[0]
6989
```
7090

7191
### **Java**
7292

7393
<!-- 这里可写当前语言的特殊实现逻辑 -->
7494

7595
```java
96+
class Solution {
97+
public int minBuildTime(int[] blocks, int split) {
98+
PriorityQueue<Integer> q = new PriorityQueue<>();
99+
for (int x : blocks) {
100+
q.offer(x);
101+
}
102+
while (q.size() > 1) {
103+
q.poll();
104+
q.offer(q.poll() + split);
105+
}
106+
return q.poll();
107+
}
108+
}
109+
```
110+
111+
### **C++**
112+
113+
```cpp
114+
class Solution {
115+
public:
116+
int minBuildTime(vector<int>& blocks, int split) {
117+
priority_queue<int, vector<int>, greater<int>> pq;
118+
for (int v : blocks) pq.push(v);
119+
while (pq.size() > 1) {
120+
pq.pop();
121+
int x = pq.top();
122+
pq.pop();
123+
pq.push(x + split);
124+
}
125+
return pq.top();
126+
}
127+
};
128+
```
76129
130+
### **Go**
131+
132+
```go
133+
func minBuildTime(blocks []int, split int) int {
134+
q := hp{}
135+
for _, v := range blocks {
136+
heap.Push(&q, v)
137+
}
138+
for q.Len() > 1 {
139+
heap.Pop(&q)
140+
heap.Push(&q, heap.Pop(&q).(int)+split)
141+
}
142+
return q.IntSlice[0]
143+
}
144+
145+
type hp struct{ sort.IntSlice }
146+
147+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
148+
func (h *hp) Pop() interface{} {
149+
a := h.IntSlice
150+
v := a[len(a)-1]
151+
h.IntSlice = a[:len(a)-1]
152+
return v
153+
}
77154
```
78155

79156
### **...**

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,76 @@ The cost is 1 + max(3, 1 + max(1, 2)) = 4.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def minBuildTime(self, blocks: List[int], split: int) -> int:
62+
heapify(blocks)
63+
while len(blocks) > 1:
64+
heappop(blocks)
65+
heappush(blocks, heappop(blocks) + split)
66+
return blocks[0]
6167
```
6268

6369
### **Java**
6470

6571
```java
72+
class Solution {
73+
public int minBuildTime(int[] blocks, int split) {
74+
PriorityQueue<Integer> q = new PriorityQueue<>();
75+
for (int x : blocks) {
76+
q.offer(x);
77+
}
78+
while (q.size() > 1) {
79+
q.poll();
80+
q.offer(q.poll() + split);
81+
}
82+
return q.poll();
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int minBuildTime(vector<int>& blocks, int split) {
93+
priority_queue<int, vector<int>, greater<int>> pq;
94+
for (int v : blocks) pq.push(v);
95+
while (pq.size() > 1) {
96+
pq.pop();
97+
int x = pq.top();
98+
pq.pop();
99+
pq.push(x + split);
100+
}
101+
return pq.top();
102+
}
103+
};
104+
```
66105
106+
### **Go**
107+
108+
```go
109+
func minBuildTime(blocks []int, split int) int {
110+
q := hp{}
111+
for _, v := range blocks {
112+
heap.Push(&q, v)
113+
}
114+
for q.Len() > 1 {
115+
heap.Pop(&q)
116+
heap.Push(&q, heap.Pop(&q).(int)+split)
117+
}
118+
return q.IntSlice[0]
119+
}
120+
121+
type hp struct{ sort.IntSlice }
122+
123+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
124+
func (h *hp) Pop() interface{} {
125+
a := h.IntSlice
126+
v := a[len(a)-1]
127+
h.IntSlice = a[:len(a)-1]
128+
return v
129+
}
67130
```
68131

69132
### **...**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int minBuildTime(vector<int>& blocks, int split) {
4+
priority_queue<int, vector<int>, greater<int>> pq;
5+
for (int v : blocks) pq.push(v);
6+
while (pq.size() > 1) {
7+
pq.pop();
8+
int x = pq.top();
9+
pq.pop();
10+
pq.push(x + split);
11+
}
12+
return pq.top();
13+
}
14+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func minBuildTime(blocks []int, split int) int {
2+
q := hp{}
3+
for _, v := range blocks {
4+
heap.Push(&q, v)
5+
}
6+
for q.Len() > 1 {
7+
heap.Pop(&q)
8+
heap.Push(&q, heap.Pop(&q).(int)+split)
9+
}
10+
return q.IntSlice[0]
11+
}
12+
13+
type hp struct{ sort.IntSlice }
14+
15+
func (h *hp) Push(v interface{}) { h.IntSlice = append(h.IntSlice, v.(int)) }
16+
func (h *hp) Pop() interface{} {
17+
a := h.IntSlice
18+
v := a[len(a)-1]
19+
h.IntSlice = a[:len(a)-1]
20+
return v
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int minBuildTime(int[] blocks, int split) {
3+
PriorityQueue<Integer> q = new PriorityQueue<>();
4+
for (int x : blocks) {
5+
q.offer(x);
6+
}
7+
while (q.size() > 1) {
8+
q.poll();
9+
q.offer(q.poll() + split);
10+
}
11+
return q.poll();
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def minBuildTime(self, blocks: List[int], split: int) -> int:
3+
heapify(blocks)
4+
while len(blocks) > 1:
5+
heappop(blocks)
6+
heappush(blocks, heappop(blocks) + split)
7+
return blocks[0]

0 commit comments

Comments
 (0)