题目描述:

方法一:动态规划 O(N)

class Solution:
def maxSumDivThree(self, nums: List[int]) -> int:
dp = [0, -1,-1]
for n in nums:
tmp = dp[:]
for i in range(3):
if dp[i] != -1:
tmp[(i+n)%3] = max(tmp[(i+n)%3], dp[i]+n)
dp = tmp
return dp[0]

方法二:暴力 O(nlogn)

class Solution:
def maxSumDivThree(self, nums: List[int]) -> int:
n1, n2 = [], []
ans = 0
for num in nums:
if num % 3 == 0:
ans += num
elif num % 3 == 1:
n1.append(num)
ans += num
else:
n2.append(num)
ans += num
n1.sort(reverse=True)
n2.sort(reverse=True) if len(n1) % 3 == len(n2) % 3:
return ans if len(n1) % 3 > len(n2) % 3:
n1, n2 = n2, n1 if len(n1) % 3 == 0 and len(n2) % 3 == 1:
if not n1: return ans - n2[-1]
return ans - min(n2[-1], n1[-1] + n1[-2])
if len(n1) % 3 == 0 and len(n2) % 3 == 2:
if not n1: return ans - n2[-1] - n2[-2]
return ans - min(n1[-1], n2[-1] + n2[-2])
if len(n1) % 3 == 1 and len(n2) % 3 == 2:
if len(n1) == 1: return ans - n2[-1]
return ans - min(n2[-1], n1[-1] + n1[-2])

leetcode-163周赛-1262-可被3整除的最大和的更多相关文章

  1. Leetcode 1262. 可被三整除的最大和

    题目:给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 1: 输入:nums = [3,6,5,1,8] 输出:18 解释:选出数字 3, 6, 1 和 8,它们的和是 18( ...

  2. LeetCode 5365. 可被三整除的最大和 Greatest Sum Divisible by Three

    地址 https://www.acwing.com/solution/leetcode/content/6340/ 题目描述给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 : ...

  3. LeetCode 163. Missing Ranges (缺失的区间)$

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  4. Leetcode 1013. 总持续时间可被 60 整除的歌曲

    1013. 总持续时间可被 60 整除的歌曲  显示英文描述 我的提交返回竞赛   用户通过次数450 用户尝试次数595 通过次数456 提交次数1236 题目难度Easy 在歌曲列表中,第 i 首 ...

  5. [leetcode]163. Missing Ranges缺失范围

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  6. [LeetCode] 163. Missing Ranges 缺失区间

    Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...

  7. LeetCode双周赛#35

    1589. 所有排列中的最大和 #差分 #贪心 题目链接 题意 给定整数数组nums,以及查询数组requests,其中requests[i] = [starti, endi] .第i个查询求 num ...

  8. 【LeetCode】974. 和可被 K 整除的子数组

    974. 和可被 K 整除的子数组 知识点:数组:前缀和: 题目描述 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目. 示例 输入:A = [4,5,0,-2,-3, ...

  9. ✡ leetcode 163. Missing Ranges 找出缺失范围 --------- java

    Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], ...

  10. [LeetCode#163] Missing Ranges

    Problem: Given a sorted integer array where the range of elements are [lower, upper] inclusive, retu ...

随机推荐

  1. Vue路由组件vue-router

    一.路由介绍 Creating a Single-page Application with Vue + Vue Router is dead simple. With Vue.js, we are ...

  2. 2018/8/26学习Mysql笔记

    SELECT * FROM product; #.基本增删改查 #新增 #需求:添加一条数据到产品表 产品名称为苹果手机 卖价为5000 ); #删除 #需求:删除产品表中id=20的数据 ; #需求 ...

  3. hibernate 1 对1

    举例:部门departments   -------部门经理managers 映射 有两种方式 1:外键映射.类似于多对1.但是设置了unique唯一. 带外键的: package com.hiber ...

  4. Qt对话框部分学习

    一.对话框部分常用内容 颜色对话框.文件对话框.字体对话框.输入对话框.消息对话框.进度对话框.错误对话框.向导对话框. 二.代码部分   //widget.h #ifndef MYWIDGET_H ...

  5. implements Serializable有什么作用

    转自 http://blog.csdn.net/dinghqalex/article/details/46009911

  6. BZOJ 2839: 集合计数(二项式反演)

    传送门 解题思路 设\(f(k)\)为交集元素个数为\(k\)的方案数.发现我们并不能直接求出\(f(k)\),就考虑容斥之类的东西,容斥首先要扩大限制,再设\(g(k)\)表示至少有\(k\)个交集 ...

  7. (55)C# windows 消息

    窗体捕获消息 namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeC ...

  8. 研究一下phpspider

    官方文档 1.下载 官方github下载地址: https://github.com/owner888/phpspider 下载地址可能无法访问,这里提供一个网盘下载地址: 链接: https://p ...

  9. 【转】理解JMX之介绍和简单使用

    原文链接:https://blog.csdn.net/lmy86263/article/details/71037316 近期在项目上需要添加一些功能,想把一个开源工程整合进来,虽说是整合,但是觉得跟 ...

  10. webstorm 插件安装

    1.打勾的表示已经安装 2.没有安装的插件,可以在plugins搜索,在右边搜索结果里点install,然后重启webstorm 3.这里有常用插件 http://blog.csdn.net/xs20 ...