地址 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. 理解ES6的新数据类型:Symbol

    ES6之前的数组类型 在ES6之前JS只有6种数据类型,分别是:Undefined.Null.布尔值(Boolean).字符串(String).数值(Number).对象(Object). ES6引入 ...

  2. 默认值操作符(Freemarker的空值处理)

    默认值操作符: 使用形式例如: userName!default_expr 或 userName! 或 (userName)!default_expr 或 (userName)! 这个操作符允许你为可 ...

  3. linux 磁盘分区和挂载看这一篇就够了

    Linux fdisk 和 mount 命令操作指南,linux磁盘管理.新增磁盘.挂载新硬盘(linux运维入门) 首先列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多 ...

  4. Weather with you主题说明

    使用前请确保拥有js权限!!! 源代码: css: /*广告去死*/ #ad_t2 { display: none !important; } #i-amphtml-fill-content { di ...

  5. 《Web Development with Go》Middleware之共享数据

    这个库值得学, 好像写起来越来越溜 package main import ( "fmt" "log" "net/http" //" ...

  6. JavaScript-----8.数组

    1.数组的概念 数组是指一组数据的集合,其中每个数据被称为元素,在数组中可以存放任意类型的元素 2. 创建数组 创建数组的两种方式: 利用new创建数组 利用数组字面量创建数组(最常用) 2.1 利用 ...

  7. Ubuntu18.04 卸载mysql5.7

    查看MySQL的依赖项:dpkg --list|grep mysql 要删除上面的这些. 开始卸载: sudo apt-get autoremove --purge mysql-server sudo ...

  8. 14. Go 语言编译与工具

    Go 语言编译与工具 Go 语言的工具链非常丰富,从获取源码.编译.文档.测试.性能分析,到源码格式化.源码提示.重构工具等应有尽有. 在 Go 语言中可以使用测试框架编写单元测试,使用统一的命令行即 ...

  9. 围观高手是如何写好 Python 循环,把内存用到极致的?

    0 前言 说到处理循环,我们习惯使用for, while等,比如依次打印每个列表中的字符: lis = ['I', 'love', 'python'] for i in lis:     print( ...

  10. 获取windows操作系统所有用户

    一.知识点简单介绍 1. 利用WindowsApi获取 [DllImport("Netapi32.dll ")] extern static int NetUserEnum([Ma ...