动态规划-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 ...
随机推荐
- Mac系列萎靡 大棒能否敲醒苹果?
大棒能否敲醒苹果?" title="Mac系列萎靡 大棒能否敲醒苹果?"> iPhone在智能手机市场中的一骑绝尘,不断将苹果推向神坛位置.即使新品更新幅度 ...
- Centos7.X 搭建Prometheus+node_exporter+Grafana实时监控平台
Prometheus简介 什么是 Prometheus Prometheus是一个开源监控报警系统和时序列数据库 主要功能 多维数据模型(时序由 metric 名字和 k/v 的 labels 构成) ...
- H5新增特性之语义化标签
H5新增特性之语义化标签 语义化标签顾名思义标签有自己的含义,浏览器或者程序员一看就知道是什么.在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式 ...
- 4——PHP比较&&复制运算符
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- list转map,set,使用stream进行转化
#list转map,set,使用stream进行转化 函数式编程: 场景: 从数据库中取出来的数据,经常是list集合类型,但是list转map这种场景虽然不常见,但是有时候也会遇到,最常见的还是转为 ...
- Python列表倒序输出及其效率
Python列表倒序输出及其效率 方法一 使用Python内置函数reversed() for i in reversed(arr): pass reversed返回的是迭代器,所以不用担心内存问题. ...
- PhalApi 2.7 开发快速上手
PhalApi是一款国人制作的PHP纯后端框架.它的开发相当简单,同时也具备文档生成等特色功能.下面,我通过简单的几点,让你可以快速入门使用该框架的开发. 建议使用PHPStorm作为IDE,代码提示 ...
- Java中如何更优雅的处理空值
经常看到项目中存在到处空值判断的情况,这些判断,会让人觉得摸不着头绪,它的出现很有可能和当前的业务逻辑并没有关系.但它会让你很头疼.有时候,更可怕的是系统因为这些空值的情况,会抛出空指针异常,导致业务 ...
- webstorm 提示 "scanning files to index..." 一直不能编译的问题
先说一下我的操作过程吧: 下载公司的vue项目后,要用到webpack打包工具,需要按照package.json安装一些依赖,我使用了镜像后,npm install模块时候生成了一个 node_mod ...
- vijos 1011 清帝之惑之顺治
背景 顺治帝福临,是清朝入关后的第一位皇帝.他是皇太极的第九子,生于崇德三年(1638)崇德八年八月二ten+six日在沈阳即位,改元顺治,在位18年.卒于顺治十八年(1661),终24岁. 顺治即位 ...