LeetCode每日一题(Greatest Sum Divisible by Three)

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,
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值