2517. Maximum Tastiness of Candy Basket Medium

@problem@discussion
#Array#Binary Search#Sorting



1/**
2 * [2517] Maximum Tastiness of Candy Basket
3 *
4 * You are given an array of positive integers price where price[i] denotes the price of the i^th candy and a positive integer k.
5 * The store sells baskets of k distinct candies. The tastiness of a candy basket is the smallest absolute difference of the prices of any two candies in the basket.
6 * Return the maximum tastiness of a candy basket.
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: price = [13,5,1,8,21,2], k = 3
11 * Output: 8
12 * Explanation: Choose the candies with the prices [13,5,21].
13 * The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8.
14 * It can be proven that 8 is the maximum tastiness that can be achieved.
15 * 
16 * <strong class="example">Example 2:
17 * 
18 * Input: price = [1,3,1], k = 2
19 * Output: 2
20 * Explanation: Choose the candies with the prices [1,3].
21 * The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2.
22 * It can be proven that 2 is the maximum tastiness that can be achieved.
23 * 
24 * <strong class="example">Example 3:
25 * 
26 * Input: price = [7,7,7,7], k = 2
27 * Output: 0
28 * Explanation: Choosing any two distinct candies from the candies we have will result in a tastiness of 0.
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	2 <= k <= price.length <= 10^5
34 * 	1 <= price[i] <= 10^9
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/maximum-tastiness-of-candy-basket/
40// discuss: https://leetcode.com/problems/maximum-tastiness-of-candy-basket/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn maximum_tastiness(price: Vec<i32>, k: i32) -> i32 {
46        0
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2517() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.