题目如下:

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

解题思路:记sum为nums的和,如果其不能被3整除,那么余数只能是1或者2。如果余数是1,那么只需要减去nums中最小的除以3余数为1的数,或者减去nums中两个最小的除以3余数为2的数即可,两者之间选择较小的那个;如果余数是2,也同理。

代码如下:

class Solution(object):
def maxSumDivThree(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
total = sum(nums)
if total % 3 == 0:return total
nums.sort()
list_1 = []
list_2 = []
for i in nums:
if i % 3 == 1 and len(list_1) < 2:
list_1.append(i)
elif i % 3 == 2 and len(list_2) < 2:
list_2.append(i)
if total % 3 == 1:
minus = float('inf')
if len(list_1) > 0:
minus = list_1[0]
if len(list_2) == 2:
minus = min(minus,list_2[0] + list_2[1])
elif total % 3 == 2:
minus = float('inf')
if len(list_2) > 0:
minus = list_2[0]
if len(list_1) == 2:
minus = min(minus, list_1[0] + list_1[1])
if minus == float('inf'):return 0
return total - minus

【leetcode】1262. Greatest Sum Divisible by Three的更多相关文章

  1. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  4. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  5. 【LeetCode】#1 Two Sum

    [Question] Given an array of integers, return indices of the two numbers such that they add up to a ...

  6. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  7. 【leetcode】974. Subarray Sums Divisible by K

    题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...

  8. 【LeetCode】1018. Binary Prefix Divisible By 5 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 【CUDA开发】Cuda C++ Thrust API与 Cuda Runtime API程序比较

    今天买了本新书<高性能CUDA应用设计与开发方法与最佳实践>,今天读了第一章有点出获,分享给大家. 程序功能:给向量填充数据并计算各元素之和 1. CPU串行运行的代码: //seqSer ...

  2. 2 基于梯度的攻击——PGD

    PGD攻击原论文地址——https://arxiv.org/pdf/1706.06083.pdf 1.PGD攻击的原理 PGD(Project Gradient Descent)攻击是一种迭代攻击,可 ...

  3. DiskSim

    1.使用笔记 http://feifei432.blog.163.com/blog/static/140253361201022211949152/ http://feifei432.blog.163 ...

  4. 看某视频开始做LINUX笔记的第一天

    LINUX系统对分区的基本要求: 1.最少要有一个根分区 / 分区,用来存放系统文件及程序.其大小至少在5GB以上. 2.要有一个 swap (交换)分区,它的作用相当与windows的虚拟内存,sw ...

  5. [Python3] 033 异常

    目录 异常 1. 简介 2. 异常的分类 3. 出现异常小例子 例子 4. 异常处理 5. 解决异常小例子 5.1 例子1 5.2 例子2 5.3 例子3 5.4 例子4 6. 手动引发异常 6.1 ...

  6. 为什么要学习Hive

    一 为什么要学习HIVE?   为什么不是ORACLE和MYSQL?   因为大数据时代 数据量成几何倍数增长,并且数据量非常庞大.大到要用PB EB这种量级去衡量.而我们的ORACLE/MYQL这种 ...

  7. Ubuntu19.04系统SSH连接CentOS7虚拟机

    一.为CentOS7配置静态IP 注意查看宿主机(Ubuntu19.04)所在局域网段,当前为172.18.25.108 修改当前系统下virtual box的网络设置 [控制]-->[设置]- ...

  8. spring配置文件拆分策略及方法

    一.拆分策略 如果一个开发人员负责一个模块,我们采用公用配置(包括数据源.事务等)+每个系统模块一个单独配置文件(包括Dao.Service.Web控制器)的形式 如果是按照分层进行的分工,我们采用公 ...

  9. Navicat 连接数据库避免中文显示乱码问题解决

    在使用Navicat Premium连接数据库进行操作时,为避免出现中文乱码的问题解决: 1.连接SQL Server 在新建数据库时,常规 设置 排序规则 为 Chinese_PRC_CS_AS_W ...

  10. Hive 教程(五)-参数配置

    配置基本操作 hive> set; 查看所有配置hive> set key: 查看某个配置hive> set key value: 设置某个配置 我们可以看到一些 hadoop 的配 ...