Skip to content

Commit 460a1ce

Browse files
committed
Add solution and test-cases for problem 1482
1 parent 1940a25 commit 460a1ce

File tree

3 files changed

+106
-12
lines changed

3 files changed

+106
-12
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# [1482.Minimum Number of Days to Make m Bouquets][title]
2+
3+
## Description
4+
You are given an integer array `bloomDay`, an integer `m` and an integer `k`.
5+
6+
You want to make `m` bouquets. To make a bouquet, you need to use `k` **adjacent flowers** from the garden.
7+
8+
The garden consists of `n` flowers, the i<sup>th</sup> flower will bloom in the `bloomDay[i]` and then can be used in **exactly one** bouquet.
9+
10+
Return the minimum number of days you need to wait to be able to make `m` bouquets from the garden. If it is impossible to make m bouquets return `-1`.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
16+
Output: 3
17+
Explanation: Let us see what happened in the first three days. x means flower bloomed and _ means flower did not bloom in the garden.
18+
We need 3 bouquets each should contain 1 flower.
19+
After day 1: [x, _, _, _, _] // we can only make one bouquet.
20+
After day 2: [x, _, _, _, x] // we can only make two bouquets.
21+
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
28+
Output: -1
29+
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
36+
Output: 12
37+
Explanation: We need 2 bouquets each should have 3 flowers.
38+
Here is the garden after the 7 and 12 days:
39+
After day 7: [x, x, x, x, _, x, x]
40+
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
41+
After day 12: [x, x, x, x, x, x, x]
42+
It is obvious that we can make two bouquets in different ways.
43+
```
44+
45+
## 结语
46+
47+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
48+
49+
[title]: https://leetcode.com/problems/minimum-number-of-days-to-make-m-bouquets
50+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(bloomDay []int, m int, k int) int {
6+
l := len(bloomDay)
7+
need := m * k
8+
if need > l {
9+
return -1
10+
}
11+
var ok func(int) bool
12+
ok = func(day int) bool {
13+
c := 0
14+
complete := 0
15+
for i := 0; i < l; i++ {
16+
if bloomDay[i] <= day {
17+
c++
18+
continue
19+
}
20+
complete += c / k
21+
c = 0
22+
}
23+
complete += c / k
24+
return complete >= m
25+
}
26+
27+
dst := make([]int, 0)
28+
in := make(map[int]struct{})
29+
for _, n := range bloomDay {
30+
if _, ok := in[n]; !ok {
31+
dst = append(dst, n)
32+
in[n] = struct{}{}
33+
}
34+
}
35+
sort.Ints(dst)
36+
left, right := 0, len(dst)
37+
ans := -1
38+
for left < right {
39+
mid := (right + left) / 2
40+
if ok(dst[mid]) {
41+
ans = dst[mid]
42+
right = mid
43+
continue
44+
}
45+
left = mid + 1
46+
}
47+
return ans
548
}

leetcode/1401-1500/1482.Minimum-Number-of-Days-to-Make-m-Bouquets/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
b []int
14+
m, k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 10, 3, 10, 2}, 3, 1, 3},
18+
{"TestCase2", []int{1, 10, 3, 10, 2}, 3, 2, -1},
19+
{"TestCase3", []int{7, 7, 7, 7, 12, 7, 7}, 2, 3, 12},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.b, c.m, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.b, c.m, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)