题目描述:

方法一:动态规划 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. ivew 修改排序号的逻辑

    排序号修改的逻辑 1.构建修改排序号传递的参数 formInline2:{ recommendType:4, //产品类型 merchantCodes:[], //产品code discountRec ...

  2. ubuntu oracle数据库18c安装

    一.官网下载linux两个zip包 二.byqKx8a2tWcgBHb

  3. ubuntu下oracle 数据库安装

    环境:腾讯云 一. 由于腾讯云直接下载oracle太慢,先安装docker 1.sudo apt update 2.接下来,使用apt安装一些允许通过HTTPS才能使用的软件包: sudo apt i ...

  4. 部署多个tomcat

    当需要部署多个tomcat的时,为了避免启动tomcat时出现冲突, 修改tomcat中的某些参数,编辑bin/startup.bat,避免启动路径错误,默认会启动CATALINA_HOME所指向的t ...

  5. OS---磁盘存储器

    1.概述 1.1 磁盘存储器  不仅  容量大.存取速度快  而且  可以随机存取: 现代计算机都配置了  磁盘存储器,以  它  为主  存放文件: 对文件 的操作,都将涉及对磁盘的访问: 1.2 ...

  6. Hands Off for Mac如何卸载?完全卸载Hands Off的方法

    Hands Off for Mac如何卸载?hands off是一款超好用的防火墙软件,在Mac系统上强大且易用,能够控制所有应用的网络连接和文件系统访问,保护我们的隐私数据和系统安全性,如果不需要了 ...

  7. centos7 安装PHP5.3 报错undefined reference to symbol '__gxx_personality_v0@@CXXABI_1.3'

    系统:centos 7 原有PHP版本:5.6.27,5.4.45 试着安装nginx+多php版本,首先安装了5.6和5.4的版本,一帆风顺,但是在安装5.3.29版本时,出现问题了,configu ...

  8. 【Shiro】三、Apache Shiro认证

    配置好并获取到SecurityManager,代表Shiro正常运行起来了,可以使用Shiro的其它功能. 1.认证流程(API的使用流程) 认证的数据: Principals:标识 ·识别Subje ...

  9. QTP学习笔记---datatable应用

    DataTable应用1.定位数据行 DataTable.GetSheet() 2.获取当前行 GetCurrentRow3.获取指定行的值 getValueByRow = DataTable.Get ...

  10. PHP-文件和目录操作

    目录操作 创建目录:mkdir(目录地址, 权限, 是否递归创建 = false); 删除目录:rmdir(目录地址);(仅仅可以删除空目录,不支持递归删除) 移动(改名):rename(旧地址, 新 ...