LeetCode 5365. 可被三整除的最大和 Greatest Sum Divisible by Three
地址 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的更多相关文章
- Leetcode 1262. 可被三整除的最大和
题目:给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 1: 输入:nums = [3,6,5,1,8] 输出:18 解释:选出数字 3, 6, 1 和 8,它们的和是 18( ...
- 【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 ...
- 【JavaScript】Leetcode每日一题-最大整除子集
[JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...
- [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 ...
- Leetcode之回溯法专题-39. 组合总数(Combination Sum)
Leetcode之回溯法专题-39. 组合总数(Combination Sum) 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使 ...
- [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 ...
- 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 ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
- [LeetCode] Self Dividing Numbers 自整除数字
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
随机推荐
- 精通awk系列(7):awk读取行的细节
回到: Linux系列文章 Shell系列文章 Awk系列文章 详细分析awk如何读取文件 awk读取输入文件时,每次读取一条记录(record)(默认情况下按行读取,所以此时记录就是行).每读取一条 ...
- Refresh design pattern
多年后, 再次翻阅设计模式书籍, 将每种模式的要点总结于此, 需要本身有一定设计模式基础, 再结合要点, 帮助更好理解与运用. 策略模式 (Strategy) 附加功能单独抽象不同接口 功能以组合方式 ...
- epub.js的使用
epub.js的使用 npm安装 npm install epubjs epub阅读器开发 ePub电子书解析和渲染 生成Book对象 this.book = new Epub(DOWNLOAD_UR ...
- js-02-循环语句
循环语句分类{ for while do ( ) while } 一.for循环语句和for循环的嵌套 for循环格式eg: <script> var sim = 0; for(var i ...
- JS---DOM---part3课程介绍和part2复习
part3课程介绍 节点 为什么要学节点 节点的操作的相关属性------>作用 12行代码----有用的----熟练的问题, 节点的案例 元素的创建三种方式------重点的内容 为什 ...
- navicat连接不上Linux服务器上的mysql的解决办法
一开始,心情是沉痛的,截图如下: 转载请注明出处:https://www.cnblogs.com/NaughtyCat/p/how-to-connect-to-mysql-on-linux-by-na ...
- (办公)记事本_Linux目录和文件都能操作的命令
参考谷粒学院Linux:http://www.gulixueyuan.com/course/300/task/7082/show .cp 1.1.作用主要是拷贝,可以拷贝文件或者目录. 1.2.语法: ...
- MAC地址表(交换机)、ARP缓存表以及路由表(路由器)
- docker jenkins安装
https://hub.docker.com/r/jenkins/jenkins jenkins的docker官方镜像地址 https://jenkins.io/ jenkins官方网站 环境: 阿里 ...
- 前后台交互ajax请求模块
下载依赖包axios npm i axios -d //在packge.json内配置proxy,配置请求基础路径 "proxy":"http://localhost:5 ...