动态规划-Maximum Subarray-Maximum Sum Circular Subarray
2020-02-18 20:57:58
一、Maximum Subarray
经典的动态规划问题。
问题描述:

问题求解:
public int maxSubArray(int[] nums) {
int res = nums[0];
int n = nums.length;
int[] dp = new int[n];
dp[0] = nums[0];
for (int i = 1; i < n; i++) {
if (dp[i - 1] < 0) dp[i] = nums[i];
else dp[i] = dp[i - 1] + nums[i];
res = Math.max(res, dp[i]);
}
return res;
}
二、Maximum Sum Circular Subarray
问题描述:

问题求解:

唯一的边界条件是如果全部为负数,那么minsubarray = sum,这个时候直接返回max即可。
public int maxSubarraySumCircular(int[] A) {
int n = A.length;
int sum = 0;
for (int num : A) sum += num;
int max = A[0];
int[] dp = new int[n];
dp[0] = A[0];
for (int i = 1; i < n; i++) {
if (dp[i - 1] < 0) dp[i] = A[i];
else dp[i] = A[i] + dp[i - 1];
max = Math.max(max, dp[i]);
}
int min = A[0];
for (int i = 1; i < n; i++) {
if (dp[i - 1] > 0) dp[i] = A[i];
else dp[i] = A[i] + dp[i - 1];
min = Math.min(min, dp[i]);
}
return max > 0 ? Math.max(max, sum - min) : max;
}
动态规划-Maximum Subarray-Maximum Sum Circular Subarray的更多相关文章
- LC 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- 918. Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- [Swift]LeetCode918. 环形子数组的最大和 | Maximum Sum Circular Subarray
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- Maximum Sum Circular Subarray LT918
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和
Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...
- Leetcode Week5 Maximum Sum Circular Subarray
Question Given a circular array C of integers represented by A, find the maximum possible sum of a n ...
- LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...
- 862. Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
- [Swift]LeetCode862. 和至少为 K 的最短子数组 | Shortest Subarray with Sum at Least K
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K. If there ...
随机推荐
- App自动化测试方案
App自动化测试方案 1.1 概述 什么是App自动化?为什么要做App自动化? App自动化是指给 Android或iOS上的软件应用程序做的自动化测试. 手工测试和自动化测试的对比如下: 手工测 ...
- 关于struct stat
需要使用struct stat 类型时如果编译不过,修改Makefile: ##CFG_INC := -I$(MPI_DIR)/api/so/##CFG_INC += -I$(BASE_DIR)/pu ...
- Java2变量和运算符
课后作业:[必做题] 1√AB互换 已知a,b均是整型变量,写出将a,b两个变量中的值互换的程序.(知识点:变量和运算符综合应用) [必做题] package com.two; public clas ...
- Qt_QChart的使用记录(小白)
主要是记录柱状图的数值显示,散点图的点坐标显示(防止后续忘记,把文件都贴出来,方便复查) 资源库: WarehouseInputOrOutput.pro QT += core gui QT += ch ...
- Typora[MarkDown编辑器]+(PicGo+Github+JsDelivr)[个人图床] ,开启你的高效创作
使用Typora搭配Picgo开启你的高效创作 0x00 一切都要从MarkDown说起 富文本语言的弊端 平常我们最常用的写作工具,无非是富文本编辑器中的代表--微软家的Office Word.这种 ...
- python之迭代器 生成器 枚举 常用内置函数 递归
迭代器 迭代器对象:有__next__()方法的对象是迭代器对象,迭代器对象依赖__next__()方法进行依次取值 with open('text.txt','rb',) as f: res = f ...
- 第八章、小节三keep-alive
主要缓存的是ajax中的json 我的路由中的内容被加载过一次,我就把路由中的内容放到内存中,下次再进入这个路由的时候,不需要重新加载页面,直接从内存中获取数据. 切换不同城市,调用不同城市数据 但是 ...
- 李宏毅深度学习与人类语言处理-introduction
深度学习与人类语言处理(Deep learning for Human Language Processing) 李宏毅老师深度学习与人类语言处理课程笔记,请看正文 这门课会学到什么? 为什么叫人类语 ...
- linux最常用命令记录(一)
一.vim个人最常用设置: vim .vimrc 然后添加以下内容 set nu set tabstop=4 set encoding=utf-8 二.查看磁盘空间相关命令 1.df -h 查看硬 ...
- 使用 ALSAlib 播放 wav
在 ARM 2440 开发板上正常播放 16bit 44100 采样率的wav , 为了程序简单,没有判断返回值. 补充,在 ubunto 上也能正常播放. 编译方法: arm-linux-gcc ...