【leetcode】1262. Greatest Sum Divisible by Three
题目如下:
Given an array
numsof 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^41 <= 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 ...
随机推荐
- Windows 10 搭建 Django 环境(详细教程)
Windows 10 搭建 Django 环境(详细教程) https://blog.csdn.net/HappyLittleMouse/article/details/81360272#1.%20P ...
- PHP Excel导入
public function importFile() { $file = request()->file('file'); $params = $this->request->p ...
- JSON与对象,集合之间的转换
https://www.cnblogs.com/xiatc/p/8952739.html
- Centos7 安装多版本php 并添加swoole拓展
服务器默认安装了php7 直接使用lnmp工具包安装php5.6 使用之前的lnmp安装包,切换到root sudo su - 运行 选择5.6 安装完成 没有安装swoole拓展 由官方https: ...
- Durable NAND flash memory management
词条积累 1.NAND flash memory http://www.searchstorage.com.cn/whatis/word_6052.htm http://baike.baidu.com ...
- 必须要掌握的linux目录路径知识
一./etc/sysconfig/network-scripts/ifcfg-en33 配置网络地址及GW等. 下面图片是一些常用的linux命令: nmtui(setup)---------- ...
- inner join on会过滤掉两边空值的条件
前两天工作过程中,遇到一个问题,关于join on查询的,对于查出来的结果一直都很疑惑,这里记录一下. 1.首先看下面这条sql查询语句: 查询出来的结果是25053 2.加个 o.lat = n.l ...
- 将多个jar包重新打包成一个jar包
我介绍的方法是使用java命令来操作的,所以首先的安装jdk,这个就自己搞定吧. 提取jar包 为了将多个jar包打包成一个jar包,首先要将每个jar包的内容提取出来放到一个文件夹下,具体的操作命令 ...
- 牛客小白月赛14 -A (找规律+除数取模)
题目链接:https://ac.nowcoder.com/acm/contest/879/A 题意:有n个城市,编号1~n,k天,第一天位于城市1,要求最后一天在城市1,且相邻两天不在同一个城市,求方 ...
- 【Linux 网络编程】常用TCP/IP网络编程函数
(1)函数socket /**************************************************************** ** 功能:创建一个套接字用于通信 ** 参 ...