【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 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的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】#1 Two Sum
[Question] Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 【leetcode】974. Subarray Sums Divisible by K
题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...
- 【LeetCode】1018. Binary Prefix Divisible By 5 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 再谈MV*(MVVM MVP MVC)模式的设计原理—封装与解耦
精炼并增补于:界面之下:还原真实的MV*模式 图形界面的应用程序提供给用户可视化的操作界面,这个界面提供给数据和信息.用户输入行为(键盘,鼠标等)会执行一些应用逻辑,应用逻辑(application ...
- Centos磁盘空间不足,找不到占用文件
服务器报警,系统"/"空间不足,但找不到哪些文件占用. 1.使用du -sh *,层层目录查看依然找不到 2.使用"lsof / | grep -i delete&quo ...
- PTA(Basic Level)1058.A+B in Hogwarts
If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- a ...
- Firefox、IE、chrome浏览器和驱动下载地址
一.Firefox和驱动下载地址 selenium2.X最高支持的Firefox版本为46,使用selenium2.X的话不需要下载火狐驱动,只需要配置火狐的启动路径即可. Selenium3.0开始 ...
- 2018.07.17【省赛模拟】模拟B组 比赛总结
题目 [GDKOI2003]最大公共子串 [题目描述] 从一个给定的串中删去(不一定连续地删去)0个或0个以上的字符,剩下的字符按原来的顺序组成的串是该串的字串.例如:"", &q ...
- linux下安装php的lua扩展
1. 进入管理员权限使用yum安装 readline(也可以使用wget下载后./configure 然后 make && make install进行安装) yum install ...
- redis 学习(9)-- redis 客户端 -- redis-py
redis 客户端 -- redis-py 简介 关于 redis 的各种客户端,我们可以在官网上寻找并使用,比如我这里的 python 客户端,可以在官网上找到:redis-client . 获取 ...
- SpringBoot上传文件,经过spingCloud-Zuul,中文文件名乱码解决办法
最近用springCloud整合springboot做分布式服务发现经过zuul之后上传的中文文件名乱码全都变成?????,从而引发异常,单独用springboot却是好的,在网上找到相关资料总结如下 ...
- Brain的同步规则
这段话来自Java编程思想并发一章 什么时候使用同步 如果你正在写一个变量,它可能接下来将被另一个线程读取,或者正在读取一个上一次已经被另一个线程写过的变量,那么你必须使用同步,并且,读写线程都必须用 ...
- workerman 实践 及 不能多人连接的问题
官网:https://www.workerman.net/ 手册地址:https://www.workerman.net/doc 追加内容: 请在开发前多读读 开发必读http://doc.worke ...