leet-code/src/main/java/leetcode/editor/cn/MinimumNumberOfDaysToMakeMBouquets.java
2021-05-10 12:42:37 +08:00

130 lines
4.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个整数数组 bloomDay以及两个整数 m 和 k 。
//
// 现需要制作 m 束花。制作花束时,需要使用花园中 相邻的 k 朵花 。
//
// 花园中有 n 朵花,第 i 朵花会在 bloomDay[i] 时盛开,恰好 可以用于 一束 花中。
//
// 请你返回从花园中摘 m 束花需要等待的最少的天数。如果不能摘到 m 束花则返回 -1 。
//
//
//
// 示例 1
//
// 输入bloomDay = [1,10,3,10,2], m = 3, k = 1
//输出3
//解释让我们一起观察这三天的花开过程x 表示花开,而 _ 表示花还未开。
//现在需要制作 3 束花,每束只需要 1 朵。
//1 天后:[x, _, _, _, _] // 只能制作 1 束花
//2 天后:[x, _, _, _, x] // 只能制作 2 束花
//3 天后:[x, _, x, _, x] // 可以制作 3 束花,答案为 3
//
//
// 示例 2
//
// 输入bloomDay = [1,10,3,10,2], m = 3, k = 2
//输出:-1
//解释:要制作 3 束花,每束需要 2 朵花,也就是一共需要 6 朵花。而花园中只有 5 朵花,无法满足制作要求,返回 -1 。
//
//
// 示例 3
//
// 输入bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
//输出12
//解释:要制作 2 束花,每束需要 3 朵。
//花园在 7 天后和 12 天后的情况如下:
//7 天后:[x, x, x, x, _, x, x]
//可以用前 3 朵盛开的花制作第一束花。但不能使用后 3 朵盛开的花,因为它们不相邻。
//12 天后:[x, x, x, x, x, x, x]
//显然,我们可以用不同的方式制作两束花。
//
//
// 示例 4
//
// 输入bloomDay = [1000000000,1000000000], m = 1, k = 1
//输出1000000000
//解释:需要等 1000000000 天才能采到花来制作花束
//
//
// 示例 5
//
// 输入bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
//输出9
//
//
//
//
// 提示:
//
//
// bloomDay.length == n
// 1 <= n <= 10^5
// 1 <= bloomDay[i] <= 10^9
// 1 <= m <= 10^6
// 1 <= k <= n
//
// Related Topics 数组 二分查找
// 👍 143 👎 0
package leetcode.editor.cn;
//1482:制作 m 束花所需的最少天数
public class MinimumNumberOfDaysToMakeMBouquets {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumNumberOfDaysToMakeMBouquets().new Solution();
//3
System.out.println(solution.minDays(new int[]{1, 10, 3, 10, 2}, 3, 1));
//-1
System.out.println(solution.minDays(new int[]{1, 10, 3, 10, 2}, 3, 2));
//12
System.out.println(solution.minDays(new int[]{7, 7, 7, 7, 12, 7, 7}, 2, 3));
//1000000000
System.out.println(solution.minDays(new int[]{1000000000, 1000000000}, 1, 1));
//9
System.out.println(solution.minDays(new int[]{1, 10, 2, 9, 3, 8, 4, 7, 5, 6}, 4, 2));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minDays(int[] bloomDay, int m, int k) {
if (m * k > bloomDay.length) {
return -1;
}
int min = Integer.MAX_VALUE, max = 0;
for (int day : bloomDay) {
min = Math.min(min, day);
max = Math.max(max, day);
}
while (min < max) {
int days = (max - min) / 2 + min;
if (isTrue(bloomDay, days, m, k)) {
max = days;
} else {
min = days + 1;
}
}
return min;
}
public boolean isTrue(int[] bloomDay, int days, int m, int k) {
int count = 0;
int nums = 0;
int length = bloomDay.length;
for (int i = 0; i < length && count < m; i++) {
if (bloomDay[i] <= days) {
nums++;
if (nums == k) {
count++;
nums = 0;
}
} else {
nums = 0;
}
}
return count >= m;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}