【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 ...
随机推荐
- webdriervAPI(By声明定位方法)
from selenium import webdriver from selenium.webdriver.common.by import By 导入By方法 driver = w ...
- yum tenxun ntpdate 时间同步
centos7 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base ...
- NXP-PN511-antenna-design-quide
NXP-PN511-antenna-design-quide 文库有下载 C1 C2
- .Net Core Web应用加载读取Json配置文件
⒈添加Json配置文件并将“复制到输出目录”属性设置为“始终复制” { "Logging": { "LogLevel": { "Default&quo ...
- Tomcat解析XML和反射创建对象原理
Tomcat解析XML和反射创建对象原理 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Me ...
- Android渐变色xml配置
这里渐变色: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&quo ...
- Android新版xUtils3工具类相关debug
首先出现问题是 build.gradle中的csayısıom.lidroid.xutils:xutils:2.6.13报错了,所以想到是版本的问题,github上搜了xutils发现有新版xutil ...
- Tomcat 设置80端口
1:修改tomcat配置 vi /usr/local/tomcat/conf/server.xml 找到 Connector port="8080" protocol=" ...
- 部署etcd集群
部署etcd集群 第一步:先拉取etcd二进制压缩包 wget https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2- ...
- java读取blob全身乱码
一.BLOB操作 .入库 ()JDBC方式 //通过JDBC获得数据库连接 Class.forName("oracle.jdbc.driver.OracleDriver"); Co ...