Skip to main content

561. Array Partition

·133 words·1 min· loading
Table of Contents

question

Idea
#

just simulate with simple case, nums =[1,3,2,4]

1 is smallest in arr, so it will always be added to answer. That’s because no matter what other element you combined with 1, 1 will always smaller.

It also means that the other element combined with 1 can’t added to answer. So, to minimize the damage , other element should be smallest in arr except for 1. just repeat this !!.

First Code/Solution
#

  1. sort the arr to find (smallest, 2nd smallest) elemetns.
  2. if idx % 2 == 0, add element to answer, otherwise, just ignore it.
smallest0th1th2th3th
num1234
group0011
min_valtruefalsetruefalse
impl Solution {
    pub fn array_pair_sum(mut nums: Vec<i32>) -> i32 {
        nums.sort_unstable();
        nums.iter().step_by(2).sum()
    }
}