这个题对我来说真的是相当难的题目了,严格来讲可能不算是个动态规划的题目,但这个题目对类似的划分多个非重叠连续子区间的问题提供了一个很好解决方案

这个题目需要找三个非重叠的连续子区间,通过维护两个数组将第一个和第三个子区间可能的开始pos记录下来,在中间那个子区间开始的pos遍历时限制其边界范围,根据长度能恰到好处的将三个子区间划分成非重叠的,还使用了集合相减代替累加这样比较简单好用的技巧

下面提供代码:

 class Solution {
public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
int len = nums.length;
int[]sum = new int[len+1];
for(int i = 0;i<len;i++)
sum[i+1] = sum[i]+nums[i];
int[]posLeft = new int[len];
int[]posRight = new int[len];
posLeft[k-1] = 0;
int total = sum[k]-sum[0];
for(int i = k;i<len;i++) {
if(total<sum[i+1]-sum[i+1-k]) {
total = sum[i+1]-sum[i+1-k];
posLeft[i] = i-k+1;
}else posLeft[i] = posLeft[i-1];
}
posRight[len-k] = len-k;
total = sum[len]-sum[len-k];
for(int i = len-k-1;i>=0;i--) {
if(total<sum[i+k]-sum[i]) {
total = sum[i+k]-sum[i];
posRight[i]= i;
}else posRight[i] = posRight[i+1];
}
int l = 0,r = 0,max = 0;
int[]arr = new int[3];
for(int i = k;i<=len-2*k;i++) {
l = posLeft[i-1];
r = posRight[i+k];
total = (sum[l+k]-sum[l])+(sum[i+k]-sum[i])+(sum[r+k]-sum[r]);
if(total>max) {
arr[0] = l;arr[1] = i;arr[2] = r;
max = total;
}
}
return arr;
}
}

动态规划——Maximum Sum of 3 Non-Overlapping Subarrays的更多相关文章

  1. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  2. [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  3. [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

    Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping ...

  4. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  5. 689. Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  6. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  7. LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...

  8. [LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  9. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

随机推荐

  1. SQL server 数据库中插入中文变???格式乱码的问题另一种容易忽略的情况(C#操作dapper)

    1.先查查 VS2015 中的XXX.cs页面中编码格式,记事本打开另存一下,编码格式可能是ANSI改为unioncode. (中文前面加N或者改排序规则解决不了的情况有可能是以上原因.)

  2. Power BI行级别安全性(数据权限管理)

    自从PowerBI 的DAX 函数 支持username() 或 userprincipalname()的函数后,我们就可以在Power BI中实现根据用户的行级数据权限的控制. username() ...

  3. 项目Alpha冲刺(1/10)

    1.项目燃尽图 2.今日进度描述 项目进展 熟悉Android Studio的基本使用,阅读代码规范 问题困难 学习中存在许多问题. 心得体会 应该选择一个自己熟悉的平台进行开发. 3.会议照片 4. ...

  4. Operation System

  5. 51nod1236 序列求和 V3

    这题炒鸡简单,只要第一步想对了后面顺风顺水QWQ(然鹅我没想到) 前置芝士: 斐波那契数列通项公式 等比数列求和公式 二项式定理 这题要求的就是 \(\sum_{i=1}^n Fib(i)^k\) , ...

  6. Python教学课程分享

    一.Python基本语法 1.1整数,实数,复数 前言: Python编程环境及方法 在python底层环境中唤醒python对话,直接在python中输入指令对系统进行命令编程 随意打开一个记事本或 ...

  7. 基于Https协议返回Jason字符串

    一:代码结构 二:框架结果: spring+springMvc+springJdbc 三:源代码 1:Ctrl 层 package com.todaytech.yth.gdsd.base.DataIn ...

  8. AJAX的原理

    AJAX 什么是AJAX AJAX= Asynchronous JavaScript and XML(异步的 JavaScript 和 XML): AJAX 不是新的编程语言,而是一种使用现有标准的新 ...

  9. 解决:win7右键打开方式添加应用程序无法设置和删除多余的打开方式

    win7右键打开方式添加应用程序无法设置 点击“开始”—“运行”,输入“regedit”打开注册表,在“HKEY_CLASSES_ROOT\Applications\”中找到无法添加的程序 ( 比如“ ...

  10. openpyxl使用sheet.rows或sheet.columns报TypeError: 'generator' object is not subscriptable解决方式

    解决方案: 因为新版本的openpyxl使用rows或者columns返回一个生成器所以可以使用List来解决报错问题 >>> sheet.columns[0] Traceback ...