Idea#
just simulate with simple case, nums =[1,3,2,4]
1is 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
1can’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#
- sort the arr to find (smallest, 2nd smallest) elemetns.
- if
idx % 2 == 0, add element to answer, otherwise, just ignore it.
| smallest | 0th | 1th | 2th | 3th |
|---|---|---|---|---|
| num | 1 | 2 | 3 | 4 |
| group | 0 | 0 | 1 | 1 |
| min_val | true | false | true | false |
impl Solution {
pub fn array_pair_sum(mut nums: Vec<i32>) -> i32 {
nums.sort_unstable();
nums.iter().step_by(2).sum()
}
}