2558. Take Gifts From the Richest Pile Easy
1/**
2 * [2558] Take Gifts From the Richest Pile
3 *
4 * You are given an integer array gifts denoting the number of gifts in various piles. Every second, you do the following:
5 *
6 * Choose the pile with the maximum number of gifts.
7 * If there is more than one pile with the maximum number of gifts, choose any.
8 * Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
9 *
10 * Return the number of gifts remaining after k seconds.
11 *
12 * <strong class="example">Example 1:
13 *
14 * Input: gifts = [25,64,9,4,100], k = 4
15 * Output: 29
16 * Explanation:
17 * The gifts are taken in the following way:
18 * - In the first second, the last pile is chosen and 10 gifts are left behind.
19 * - Then the second pile is chosen and 8 gifts are left behind.
20 * - After that the first pile is chosen and 5 gifts are left behind.
21 * - Finally, the last pile is chosen again and 3 gifts are left behind.
22 * The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
23 *
24 * <strong class="example">Example 2:
25 *
26 * Input: gifts = [1,1,1,1], k = 4
27 * Output: 4
28 * Explanation:
29 * In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
30 * That is, you can't take any pile with you.
31 * So, the total gifts remaining are 4.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= gifts.length <= 10^3
37 * 1 <= gifts[i] <= 10^9
38 * 1 <= k <= 10^3
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/take-gifts-from-the-richest-pile/
44// discuss: https://leetcode.com/problems/take-gifts-from-the-richest-pile/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn pick_gifts(gifts: Vec<i32>, k: i32) -> i64 {
50
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2558() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.