题目描述:

方法一:动态规划 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. 0xC0000005: 写入位置 0x00000000 时发生访问冲突的解决办法(转)

    上面的意识就是你吧值付给了不该赋给的变量,或者说你把值付给了不能付给的变量(或者常量) ()最简单也最直接的错误可能就是scanf()的问题,我们都知道输入的时候都是scanf("%格式&q ...

  2. jquery实现左侧菜单 效果

    html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...

  3. word--->pdf资料转载..

    https://blog.csdn.net/dsn727455218/article/details/80667927

  4. c++后台开发面试常见知识点总结(五)场景设计

    搜索引擎的实现,会用到哪些重要的数据结构 设计实现一个HTTP代理服务器 / web服务器 / FTP服务器/ 设计实现cache缓存web服务器的网页访问记录 把一个文件快速下发到100w个服务器 ...

  5. Python自动补全缩写意义

    自动补全的变量的类别p:parameter 参数 m:method 方法(类实例方法)调用方式classA aa.method()或者classA().method() c:class 类 v:var ...

  6. static 关键字详解(用途、误区、面试题)

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  7. 【leetcode】701. Insert into a Binary Search Tree

    题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...

  8. Redis端口配置

    redis.host=192.168.200.128redis.port=6379redis.pass=redis.database=0redis.maxIdle=300redis.maxWait=3 ...

  9. 【Flutter学习】事件处理与通知之事件处理

    一,概述 移动应用中一个必不可少的环节就是与用户的交互,在Flutter中提供的手势检测为GestureDetector. Flutter中的手势系统分为二层: 第一层是触摸原事件(指针) Point ...

  10. mysql事件(定时任务)处理超时失效订单

    MySQL事件(定时任务) https://blog.csdn.net/pan_junbiao/article/details/86489237 UPDATE wz_mer_goods_spec as ...