Leetcode 1262. 可被三整除的最大和
题目:给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和。
示例 1:
输入:nums = [3,6,5,1,8]
输出:18
解释:选出数字 3, 6, 1 和 8,它们的和是 18(可被 3 整除的最大和)。
这道题是第163周竞赛的一道题目,难度中等,边学别人的解题方法,边记录吧!
方案一、不妨设dp[i] 代表 选取的数字累加和 模3 = i 的数字和
假定nums[i] % 3 = 1 ,那么,和 前面选取的数字和模 3 = 2 的数相加,就可以模3为 0 ,表达起来就是 dp[0] = max(dp[0], nums[i] + dp[2])依次类推,只要不断更新 dp 数组即可,注意一点,更新的时候要保存上一个状态的值,避免后续更新的时候重复影响。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; int maxSumDivThree(vector<int>& nums) {
int dp[] = { , , };
int i;
cout << "┌───────┬───────┬───────┐" << endl;
for (i = ; i < nums.size(); i++)
{
//依次判断nums[i]
int mod = nums[i] % ;
int a = dp[( + - mod) % ];//保留上一步的值,避免后续相加影响
int b = dp[( + - mod) % ];
int c = dp[( + - mod) % ]; if (a||mod == ) dp[] = max(dp[], a + nums[i]);
if (b||mod == ) dp[] = max(dp[], b + nums[i]);
if (c||mod == ) dp[] = max(dp[], c + nums[i]);
cout << "│\t" << dp[] << "\t│\t" << dp[] << "\t│\t" << dp[] << "\t│" << endl;
if(i<nums.size()-)
cout << "├───────┼───────┼───────┤" << endl;
else
cout << "└───────┴───────┴───────┘" << endl;
} return dp[];
} vector<int> vc;
int main()
{
int n;
cin >> n;
vc.resize(n);
int i;
for (i = ; i < n; i++)
cin>>vc[i];
cout << endl;
cout << "能被3整除的最大和为:" <<maxSumDivThree(vc) << endl;
return ;
}
理解:(当然这是看的别人的想法,作者https://leetcode-cn.com/u/igamegum/)
先看下运行结果吧,就用上边的实例进行解释:
1、a,b,c是干什么用的?
a,b,c应该是保留上一步的值,a、b、c的取值如下:
每次均记录了上一步的dp中的值,下一步在计算dp[0]时,dp[0]会发生变化,此时,在计算dp[1]时,用到的dp[0]不再是原始的dp[0],从而造成数据的不一致问题!
2、if(b || mod == 1)中,为什么还要判断b的值?
这里的b我理解的是,当它不为0的时候,不过mod值是不是1,我都需要去更新dp[1],因为存在mod是2,需要考虑(2+2)%3=1的情况,同样,mod为0,需要考虑(1+0)%3=1的情况;这里我们去掉试一试,看下结果:
显然,当计算第4层时,数字1%3=1,此时,dp[0]只考虑了mod=0,所以dp[0]没有进行更新,但是数字1+dp[2]也可以构成模3余0的情况,并且和为15,显然比9大!!!
方案二、国外神代码,没解释,等待理解
class Solution {
public:
int maxSumDivThree(vector<int>& A) {
vector<int> dp = {, INT_MIN, INT_MIN};
for (int a : A) {
vector<int> dp2 = {, , };
for (int i = ; i < ; ++i)
dp2[(i + a) % ] = max(dp[(i + a) % ], dp[i] + a);
dp = dp2;
}
return dp[];
}
};
理解了之后回来更新~~~
Leetcode 1262. 可被三整除的最大和的更多相关文章
- LeetCode 5365. 可被三整除的最大和 Greatest Sum Divisible by Three
地址 https://www.acwing.com/solution/leetcode/content/6340/ 题目描述给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和. 示例 : ...
- 【JavaScript】Leetcode每日一题-最大整除子集
[JavaScript]Leetcode每日一题-最大整除子集 [题目描述] 给你一个由 无重复 正整数组成的集合 nums ,请你找出并返回其中最大的整除子集 answer ,子集中每一元素对(an ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- LeetCode 15. 3Sum(三数之和)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- LeetCode第二天&第三天
leetcode 第二天 2017年12月27日 4.(118)Pascal's Triangle JAVA class Solution { public List<List<Integ ...
- [LeetCode] Self Dividing Numbers 自整除数字
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...
- [LeetCode] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
随机推荐
- Xamarin vs React Native vs Ionic vs NativeScript: Cross-platform Mobile Frameworks Comparison
CONTENTS Reading time: 14 minutes Cross-platform mobile development has long been a viable alternati ...
- 构建简单Windows Service示例
示例源码:WindowsServiceSample ServiceHelper源码:ServiceHelper 1. 创建Windows Service项目,如图: 2. 配置服务参数 3. 安装,启 ...
- Message "'OFFSET' 附近有语法错误。\r\n在 FETCH 语句中选项 NEXT 的用法无效。" 解决办法 EntityFrameworkCore
由于新版的EntityFrameworkCore默认使用的是SqlServer2012或以上版本的Sql语法分页,来提高性能. 所以使用数据库的版本如果低于2012(如Sqlserver2008)需要 ...
- SpringBoot源码解析:创建SpringApplication对象实例
上篇文章SpringBoot自动装配原理解析中,我们分析了SpringBoot的自动装配原理以及@SpringBootApplication注解的原理,本篇文章则继续基于上篇文章中的main方法来分析 ...
- NIO的整体认识
目录 1.Java NIO简介 2.java NIO和IO的主要区别 3.缓冲区buffer和通道channel 3.1.缓冲区buffer 3.2.channel 4.文件通道fileChannel ...
- LabWindows/CVI第一章:基本规则
一. #include<stdio.h> //头文件,#号是预处理指令,standard input output header的缩写. void main() ...
- 如果再聘请一位会css的美工,那要你还有什么用?
老板正为网站不好看而烦恼. 会议结束后. 我对我的前辈说:要是能找个会css美工就好了! 前辈说:如果再聘请一位会css的美工,那要你还有什么用? 我一时语塞. 后来回答:我可以专心后端代码,可以写 ...
- django framework插件使用1
安装 REST框架要求以下内容: Python(3.5.3.6.3.7) Django(1.11.2.0.2.1.2.2) pip install djangorestframework pip in ...
- The Preliminary Contest for ICPC Asia Shenyang 2019 H. Texas hold'em Poker
题目链接:https://nanti.jisuanke.com/t/41408 题目意思很简单,就是个模拟过程. #include <iostream> #include <cstr ...
- 前端性能优化 css和js的加载与执行
一个网站在浏览器端是如何进行渲染的? html本身首先会被渲染成 DOM 树,实际上 html 是最先通过网址请求过来的,请求过来之后,html 本身会由一个字节流转化成一个字符流,浏览器端拿的就是字 ...