LeetCode 题解工作台
礼盒的最大甜蜜度
给你一个正整数数组 price ,其中 price[i] 表示第 i 类糖果的价格,另给你一个正整数 k 。 商店组合 k 类 不同 糖果打包成礼盒出售。礼盒的 甜蜜度 是礼盒中任意两种糖果 价格 绝对差的最小值。 返回礼盒的 最大 甜蜜度 。 示例 1: 输入: price = [13,5,1,8…
4
题型
6
代码语言
3
相关题
当前训练重点
中等 · 二分·搜索·答案·空间
答案摘要
我们注意到,如果一个甜蜜度为 的礼盒是可行的,那么甜蜜度小于 的礼盒也是可行的,这存在着单调性,因此我们可以使用二分查找的方法,找到最大的可行甜蜜度。 我们首先将数组 排序,然后定义二分查找的左边界 , 。每一次,我们计算出当前的中间值 $mid = \lfloor \frac{l+r+1}{2} \rfloor$,以 作为甜蜜度,判断是否可行。若可行,那么我们将左边界 更新为 ,否则将…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·搜索·答案·空间 题型思路
题目描述
给你一个正整数数组 price ,其中 price[i] 表示第 i 类糖果的价格,另给你一个正整数 k 。
商店组合 k 类 不同 糖果打包成礼盒出售。礼盒的 甜蜜度 是礼盒中任意两种糖果 价格 绝对差的最小值。
返回礼盒的 最大 甜蜜度。
示例 1:
输入:price = [13,5,1,8,21,2], k = 3 输出:8 解释:选出价格分别为 [13,5,21] 的三类糖果。 礼盒的甜蜜度为 min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8 。 可以证明能够取得的最大甜蜜度就是 8 。
示例 2:
输入:price = [1,3,1], k = 2 输出:2 解释:选出价格分别为 [1,3] 的两类糖果。 礼盒的甜蜜度为 min(|1 - 3|) = min(2) = 2 。 可以证明能够取得的最大甜蜜度就是 2 。
示例 3:
输入:price = [7,7,7,7], k = 2 输出:0 解释:从现有的糖果中任选两类糖果,甜蜜度都会是 0 。
提示:
2 <= k <= price.length <= 1051 <= price[i] <= 109
解题思路
方法一:贪心 + 二分查找
我们注意到,如果一个甜蜜度为 的礼盒是可行的,那么甜蜜度小于 的礼盒也是可行的,这存在着单调性,因此我们可以使用二分查找的方法,找到最大的可行甜蜜度。
我们首先将数组 排序,然后定义二分查找的左边界 , 。每一次,我们计算出当前的中间值 ,以 作为甜蜜度,判断是否可行。若可行,那么我们将左边界 更新为 ,否则将右边界 更新为 。最后返回 即可。
那么问题的关键转化为:判断一个甜蜜度是否可行,我们通过函数 来实现。函数 的执行逻辑如下:
定义一个变量 表示当前已经选取的糖果的数量,初始值为 ,定义一个变量 表示上一个选取的糖果的价格,初始值为 。然后我们遍历排好序的数组 ,对于每一个糖果的价格 ,如果 ,那么我们就选取这个糖果,将 更新为 ,并将 加一。最后判断 是否大于等于 ,如果是,那么返回 ,否则返回 。
时间复杂度 ,空间复杂度 。其中 是数组 的长度;而 是数组 中的最大值,本题中 。
class Solution:
def maximumTastiness(self, price: List[int], k: int) -> int:
def check(x: int) -> bool:
cnt, pre = 0, -x
for cur in price:
if cur - pre >= x:
pre = cur
cnt += 1
return cnt >= k
price.sort()
l, r = 0, price[-1] - price[0]
while l < r:
mid = (l + r + 1) >> 1
if check(mid):
l = mid
else:
r = mid - 1
return l
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for the candidate's understanding of binary search over a range of possible answers.
- question_mark
Check if the candidate effectively uses sorting and greedy methods to confirm the validity of a candidate solution.
- question_mark
Ensure the candidate can optimize the approach for both time and space, given the problem constraints.
常见陷阱
外企场景- error
Failing to sort the array before applying the greedy approach can result in incorrect selections of candies.
- error
Not properly adjusting the binary search bounds based on the feasibility of the current mid value.
- error
Overcomplicating the problem by using brute-force methods instead of the efficient binary search and greedy solution.
进阶变体
外企场景- arrow_right_alt
What if the price array contains duplicate values? The approach remains the same, but you need to be mindful that some baskets may have a tastiness of 0.
- arrow_right_alt
What if k is always equal to the array length? The problem reduces to finding the smallest possible price difference between the candies.
- arrow_right_alt
How would the problem change if you were allowed to select fewer than k candies? This would require adjusting the feasibility check in the greedy approach.