561. Array Partition Easy
1/**
2 * [561] Array Partition
3 *
4 * Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
5 *
6 * Example 1:
7 *
8 * Input: nums = [1,4,3,2]
9 * Output: 4
10 * Explanation: All possible pairings (ignoring the ordering of elements) are:
11 * 1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
12 * 2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
13 * 3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
14 * So the maximum possible sum is 4.
15 * Example 2:
16 *
17 * Input: nums = [6,2,6,5,1,2]
18 * Output: 9
19 * Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
20 *
21 *
22 * Constraints:
23 *
24 * 1 <= n <= 10^4
25 * nums.length == 2 * n
26 * -10^4 <= nums[i] <= 10^4
27 *
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/array-partition/
32// discuss: https://leetcode.com/problems/array-partition/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37 pub fn array_pair_sum(nums: Vec<i32>) -> i32 {
38 0
39 }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn test_561() {
50 }
51}
52
Back
© 2025 bowen.ge All Rights Reserved.