Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.
Example 1:
Input: nums = [3,6,5,1,8]
Output: 18
Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).
Example 2:
Input: nums = [4]
Output: 0
Explanation: Since 4 is not divisible by 3, do not pick any number.
Example 3:
Input: nums = [1,2,3,4,4]
Output: 12
Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).
Constraints:
- 1 <= nums.length <= 4 * 10^4
- 1 <= nums[i] <= 10^4
为啥是要被3整除呢?因为再大一点,可能难度就会大大增加
任何整数除以3, 余数就三种情况, 0, 1, 2. nums的sum % 3也就这三种情况:
- 余数为1, 我们要减去1个最小的余数为1的数, 或者减去2个最小的余数为2的数(4 % 3 = 1), 两种情况对比, 挑小的减
- 余数为2, 我们要减去2个最小的余数为1的数, 或者减去1个最小的余数为2的数, 两种情况对比, 挑小的减
- 余数为0, 不用处理了, 直接返回, 这就是最优的答案
代码实现(Rust):
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pub fn max_sum_div_three(nums: Vec<i32>) -> i32 {
let mut remain_one: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
let mut remain_two: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
let mut sum = 0;
for n in nums {
if n % 3 == 1 {
remain_one.push(Reverse(n));
} else if n % 3 == 2 {
remain_two.push(Reverse(n));
}
sum += n;
}
match sum % 3 {
1 => {
if remain_one.len() == 0 && remain_two.len() < 2 {
return 0;
}
if remain_one.len() > 0 && remain_two.len() < 2 {
return sum - remain_one.pop().unwrap().0;
}
if remain_one.len() == 0 && remain_two.len() >= 2 {
return sum - remain_two.pop().unwrap().0 - remain_two.pop().unwrap().0;
}
let one = remain_one.pop().unwrap().0;
let two = remain_two.pop().unwrap().0 + remain_two.pop().unwrap().0;
sum - one.min(two)
},
2 => {
if remain_one.len() == 0 && remain_two.len() < 2 {
return 0;
}
if remain_one.len() >= 2 && remain_two.len() == 0 {
return sum - remain_one.pop().unwrap().0 - remain_one.pop().unwrap().0;
}
if remain_one.len() < 2 && remain_two.len() > 0 {
return sum - remain_two.pop().unwrap().0;
}
let one = remain_one.pop().unwrap().0 + remain_one.pop().unwrap().0;
let two = remain_two.pop().unwrap().0;
sum - one.min(two)
},
_ => sum,
}
}
}