3428. Maximum and Minimum Sums of at Most Size K Subsequences Medium
1/**
2 * [3428] Maximum and Minimum Sums of at Most Size K Subsequences
3 *
4 * You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all <span data-keyword="subsequence-sequence-nonempty">subsequences</span> of nums with at most k elements.
5 * Since the answer may be very large, return it modulo 10^9 + 7.
6 *
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [1,2,3], k = 2</span>
10 * Output: 24
11 * Explanation:
12 * The subsequences of nums with at most 2 elements are:
13 * <table style="border: 1px solid black;">
14 * <tbody>
15 * <tr>
16 * <th style="border: 1px solid black;">Subsequence </th>
17 * <th style="border: 1px solid black;">Minimum</th>
18 * <th style="border: 1px solid black;">Maximum</th>
19 * <th style="border: 1px solid black;">Sum</th>
20 * </tr>
21 * <tr>
22 * <td style="border: 1px solid black;">[1]</td>
23 * <td style="border: 1px solid black;">1</td>
24 * <td style="border: 1px solid black;">1</td>
25 * <td style="border: 1px solid black;">2</td>
26 * </tr>
27 * <tr>
28 * <td style="border: 1px solid black;">[2]</td>
29 * <td style="border: 1px solid black;">2</td>
30 * <td style="border: 1px solid black;">2</td>
31 * <td style="border: 1px solid black;">4</td>
32 * </tr>
33 * <tr>
34 * <td style="border: 1px solid black;">[3]</td>
35 * <td style="border: 1px solid black;">3</td>
36 * <td style="border: 1px solid black;">3</td>
37 * <td style="border: 1px solid black;">6</td>
38 * </tr>
39 * <tr>
40 * <td style="border: 1px solid black;">[1, 2]</td>
41 * <td style="border: 1px solid black;">1</td>
42 * <td style="border: 1px solid black;">2</td>
43 * <td style="border: 1px solid black;">3</td>
44 * </tr>
45 * <tr>
46 * <td style="border: 1px solid black;">[1, 3]</td>
47 * <td style="border: 1px solid black;">1</td>
48 * <td style="border: 1px solid black;">3</td>
49 * <td style="border: 1px solid black;">4</td>
50 * </tr>
51 * <tr>
52 * <td style="border: 1px solid black;">[2, 3]</td>
53 * <td style="border: 1px solid black;">2</td>
54 * <td style="border: 1px solid black;">3</td>
55 * <td style="border: 1px solid black;">5</td>
56 * </tr>
57 * <tr>
58 * <td style="border: 1px solid black;">Final Total</td>
59 * <td style="border: 1px solid black;"> </td>
60 * <td style="border: 1px solid black;"> </td>
61 * <td style="border: 1px solid black;">24</td>
62 * </tr>
63 * </tbody>
64 * </table>
65 * The output would be 24.
66 * </div>
67 * <strong class="example">Example 2:
68 * <div class="example-block">
69 * Input: <span class="example-io">nums = [5,0,6], k = 1</span>
70 * Output: 2<span class="example-io">2</span>
71 * Explanation:
72 * For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is 5 + 5 + 0 + 0 + 6 + 6 = 22.
73 * </div>
74 * <strong class="example">Example 3:
75 * <div class="example-block">
76 * Input: <span class="example-io">nums = [1,1,1], k = 2</span>
77 * Output: 12
78 * Explanation:
79 * The subsequences [1, 1] and [1] each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12.
80 * </div>
81 *
82 * Constraints:
83 *
84 * 1 <= nums.length <= 10^5
85 * 0 <= nums[i] <= 10^9
86 * <font face="monospace">1 <= k <= min(70, nums.length)</font>
87 *
88 */
89pub struct Solution {}
90
91// problem: https://leetcode.com/problems/maximum-and-minimum-sums-of-at-most-size-k-subsequences/
92// discuss: https://leetcode.com/problems/maximum-and-minimum-sums-of-at-most-size-k-subsequences/discuss/?currentPage=1&orderBy=most_votes&query=
93
94// submission codes start here
95
96impl Solution {
97 pub fn min_max_sums(nums: Vec<i32>, k: i32) -> i32 {
98 0
99 }
100}
101
102// submission codes end
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_3428() {
110 }
111}
112
Back
© 2025 bowen.ge All Rights Reserved.