LeetCode--689_Maximum_Sum_of_3_NonOverlapping_Subarrays
原题链接:点击这里
一道很水很水的背包问题? 大概算不上背包吧QAQ 自己的dp 真的是太差劲啦,以后每天一道LeetCode 备战秋招!
package leetcode;
public class a689_Maximum_Sum_of_3_NonOverlapping_Subarrays {
public static int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int [] ans = new int [3];
int [][] dp = new int [3][nums.length+1];
int [] sum = new int [nums.length+1];
for(int j=1;j<=nums.length;j++) {
if(j==1) {
sum[j]=nums[j-1];
}else {
sum[j]+=sum[j-1]+nums[j-1];
}
}
int mmx =0;
for(int j=0;j<3;j++) {
int max = 0;
for(int i = k*j+1;i<=nums.length-k+1;i++) {
if(j==0) {
dp[0][i] = sum[i+k-1]-sum[i-1];
}else {
max = Math.max(max, dp[j-1][i-k]);
dp[j][i] = max+sum[i+k-1]-sum[i-1];
mmx = Math.max(mmx, dp[j][i]);
}
}
}
for(int j=2;j>=0;j--) {
for(int i=1;i<=nums.length-k+1;i++) {
if(dp[j][i]==mmx) {
ans[j]=i-1;
mmx -= sum[i+k-1]-sum[i-1];
break;
}
}
}
return ans;
}
public static void main(String[] args) {
int [] nums = {1,2,1,2,6,7,5,1};
int k = 2;
maxSumOfThreeSubarrays(nums,k);
}
}
LeetCode--689_Maximum_Sum_of_3_NonOverlapping_Subarrays的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Windows有点腻了?不如试试Ubuntu.
最近在接触Python. 因为担心环境会向Java一样,很容易影响当前的工作电脑. 所以准备搭建一台虚拟机,不过Windows的尺寸是在太大了.所以,选择安装Ubuntu. Ubuntu官方网站地址: ...
- 浅谈AndroidGPU过度绘制、GPU呈现模式分析及相关优化
在真机设备下有一个开发者选项,这个大家都知道,我们最常用的就打开'USB调试'功能,方便真机调试. 在这开发者选项中还有个选项,'调试GPU过度绘制' 这里选择第二个选项'显示过度绘制区域' 可以看到 ...
- 安卓开发:初识Android Studio
配置:Android Studio3.2.0,gradle-4.6 ,windows10 一.Android Studio安装 在http://www.android-studio.org/完成下载 ...
- 章节十、3-CSS Selector---用CSS Selector - ID定位元素
一.如果元素的 ID 不唯一,或者是动态的,或者 name 以及 linktext 属性值也不唯一,对于这样的元素,我们 就需要考虑用 xpath或者css selector 来查找元素了,然后再对元 ...
- (办公)git入门
git版本库(分布式版本控制系统),可以记录每次文件的改动,是程序开发的好帮手. 1.创建版本库: repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文 ...
- Java导出Excel的Springmvc实例
@RequestMapping(value = "downloadExcel", method = RequestMethod.GET) public String downl ...
- django csrf token添加
#views.py from django.shortcuts import render_to_response, RequestContext from django.views.decorato ...
- Python开发者现实版养成路线:从一无所知到无所不知
初级开发者学Python容易陷入茫然,面对市面上种类众多的编程语言和框架,重要的是坚持自己的选择,宜精不宜杂.本文是一篇指路文,概述了从编程基础.引导.文档阅读.书籍和视频.源代码等学习和积累环节,值 ...
- mysqlbinlog 工具分析binlog日志
MySQL的binlog 日志对于生产环境非常有用,任何时间对数据库的修改都会记录在binglog中:当数据发生增删改,创建数据库对象都会记录到binlog中,数据库的复制也是基于binlog进行同步 ...
- ORACLE 查询某表中的某个字段的类型,是否为空,是否有默认值等
最近写的功能中有这样一个小功能,根据数据库查询此库中是否有某表,如果有,查询某表下面的某个字段的详细信息 其中一种是... select ATC.OWNER, atC.TABLE_NAME, ATC. ...