地址 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. WPF 3D Cube及点击交互

    在WPF中构建一个简单的立方体比较容易实现,可参考资料也比较众多.比较麻烦的是处理点击交互. 我在WPF中用两种方式实现了3DCube,效果图如下: 方式一: 最常见的3D内容构建模式,结构如下图. ...

  2. 一文理解Java IO/NIO/AIO

      目录 概述 一.IO流(同步.阻塞) 二.NIO(同步.非阻塞) 三.NIO2(异步.非阻塞) 正文 概述 在我们学习Java的IO流之前,我们都要了解几个关键词 同步与异步(synchronou ...

  3. Javascript获取元素的xpath

    //获取xpath function readXPath(element) { if (element.id !== "") {//判断id属性,如果这个元素有id,则显 示//* ...

  4. Centos 7.5私有域名服务器部署(coredns+etcd)

    单机配置: 一.安装etcd:   1.安装 yum install etcd -y   2.启动 systemctl start etcd   3.设置开机启动 systemctl enable e ...

  5. TypeScript初体验

    第一次运行TypeScript 1.创建文件夹并初始化项目 mkdir ts-demo cd ts-demo npm init -y 2.安装typescript与ts-node # 局部安装 npm ...

  6. Python元组是什么

    引出 在使用Python过程中,列表.集合和字典是比较常用的数据结构. 列表简单说就是数组,不对,它就是数组 集合就是去重的元素结构,和JAVA中的set一样 字典就是一个key-value的键值对, ...

  7. RabbitMQ基础理解

    RabbitMQ基本理解 MQ是消息中间件,常见的有RabbitMQ,Kafka,RocketMQ,activeMQ 等,用于分布式系统中.作用有三点 解耦 异步 削峰 RabbitMQ 整体上是一个 ...

  8. 删除文件linux

    bool LxDeleteFile(const char* src){ int32_t iRe = remove(src); ) return true; else return false; }

  9. Map随笔:有序的HashMap——LinkedHashMap

    目录 Map随笔:有序的HashMap--LinkedHashMap 一,概述 二,源码结构 三,总结 Map随笔:有序的HashMap--LinkedHashMap 一,概述 ​ LinkedHas ...

  10. LeetCode 771: 宝石与石头 Jewels and Stones

    题目: 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. You're given strings ...