地址 https://leetcode-cn.com/problems/greatest-sum-divisible-by-three/

题目描述
给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和。

示例 :

输入:nums = [,,,,]
输出:
解释:选出数字 , , 和 ,它们的和是 (可被 整除的最大和)。
示例 : 输入:nums = []
输出:
解释: 不能被 整除,所以无法选出数字,返回 。
示例 : 输入:nums = [,,,,]
输出:
解释:选出数字 , , 以及 ,它们的和是 (可被 整除的最大和)。
  提示: <= nums.length <= * ^
<= nums[i] <= ^

算法1
最后数组和 只有三种情况
1 除以3余0 直接返回
2 除以3余1 那么要么减少一个除以3余1的数字 或者减少两个除以3余2的数字
3 除以3余2 那么要么减少一个除以3余2的数字 要么减少两个除以3余1的数字

class Solution {
public:
vector<int> v[];
int Check(int singleIdx,int doubleIdx,int sum)
{
if (v[doubleIdx].size() < ) {
return sum - v[singleIdx][];
}
else if (v[singleIdx].size() == ) {
return sum - v[doubleIdx][] - v[doubleIdx][];
}
else {
int rem = v[singleIdx][];
if (rem > (v[doubleIdx][] + v[doubleIdx][])) rem = (v[doubleIdx][] + v[doubleIdx][]); return sum - rem;
}
} int maxSumDivThree(vector<int>& nums) {
int sum = ;
for (int i = ; i < nums.size(); i++)
{
sum += nums[i];
if (nums[i] % == ) {
v[].push_back(nums[i]);
}
else if(nums[i] % == ){
v[].push_back(nums[i]);
}
} sort(v[].begin(), v[].end());
sort(v[].begin(), v[].end());
int sum_n = sum % ; if (sum_n == ) return sum;
if (sum_n == ) {
//减少两个v2 和一个v1 选择
return Check( , ,sum);
} if(sum_n == ){
return Check( , ,sum);
} return -;
} };

LeetCode 5365. 可被三整除的最大和 Greatest Sum Divisible by Three的更多相关文章

  1. Leetcode 1262. 可被三整除的最大和

    题目:给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 1: 输入:nums = [3,6,5,1,8] 输出:18 解释:选出数字 3, 6, 1 和 8,它们的和是 18( ...

  2. 【leetcode】1262. Greatest Sum Divisible by Three

    题目如下: Given an array nums of integers, we need to find the maximum possible sum of elements of the a ...

  3. 【JavaScript】Leetcode每日一题-最大整除子集

    [JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...

  4. [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  5. Leetcode之回溯法专题-39. 组合总数(Combination Sum)

    Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...

  6. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. LeetCode 15. 3Sum(三数之和)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  8. LeetCode第二天&第三天

    leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...

  9. [LeetCode] Self Dividing Numbers 自整除数字

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

随机推荐

  1. Python中使用class(),面向对象有什么优势

    首先我是辣鸡,然后这个问题的确有点意思 首先,类是一个集合,包含了数据,操作描述的一个抽象集合 你可以首先只把类当做一个容器来使用 class Cycle: def __init__(self,r): ...

  2. Ted:1 Vulnhub Walkthrough

    主机层面端口扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.134 Starting Nmap 7.70 ( https://nmap.org ) at 2019-08- ...

  3. C语言编程的一些小总结

    1. static:可用于定义静态局部变量 在局部变量前,加上关键字static,该变量就被定义成为一个静态局部变量. 举一个静态局部变量的例子: void fn() { static int n=1 ...

  4. 华为网络设备修改console密码

    user-interface con 0set authentication password cipher 新密码 有的版本为 <FW> system-view[FW] user-int ...

  5. Kali Linux上外网

    第一部分 攻击者杀链 第一章  走进Kali Linux 镜像准备:32位kali linxu 2019(之前下载好了,按书上的版本太低了要重新配置好多东西就用最新版了) , win XP 前两章照着 ...

  6. [译]Vulkan教程(30)深度缓存

    [译]Vulkan教程(30)深度缓存 Depth buffering 深度缓存 Introduction 入门 The geometry we've worked with so far is pr ...

  7. Yii2中$model->load($data)一直返回false问题

    上次使用$model->load()方法时一直返回false,数据添加不成功,这里记录一下: 出错代码: $data = [ 'name' => 'test', 'phone' => ...

  8. sshd服务安装-ssh命令使用方法

    SSHD服务概述 介绍:SSH协议:安全外壳协议.为 Secure Shell的缩写.SSH 为建立在应用层和传输层基础上的安全协议. 作用:SSHD服务使用SSH协议可以用来进行远程控制,或在计算机 ...

  9. redis 事务(悲观锁和乐观锁)

    MULTI 开启事务,后续的命令会被加入到同一个事务中 事务中的操作会发送给客服端,但是不会立即执行,而是将操作放到了该事务对应的一个队列中,服务端返回QUEQUD EXEC 执行EXEC后,事务中的 ...

  10. OAuth2.0授权登录

    最近工作中遇到了多系统间的授权登录,对OAuth2.0进行了学习研究,并总结备忘. [场景] 我们登录一些论坛等网站的时候,如果不想单独注册该网站账号,可以选择用微信或QQ账号进行授权登录. 这样的第 ...