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的更多相关文章

  1. 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 ...

  2. 918. Maximum Sum Circular Subarray

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  3. [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 ...

  4. Maximum Sum Circular Subarray LT918

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  5. [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 ...

  6. 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 ...

  7. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. BTCU(高校区块链联盟)-联盟链第6讲作业

    // payment project main.go package main import ( "fmt" "strconv" "github.co ...

  2. B站实战第三天

    B站实战第三天 用了两天多的时间才把B站页面的头部写完,今天来写头部下面的导航栏部分和轮播图一些模块. 因为还没学js,轮播图部分用swiper来实现. 今天首先复习的知识点是弹性盒模型. 弹性盒模型 ...

  3. 什么是AWVS

    什么是AWVS Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测试你的网站安全,检测流行安全漏洞,现已更新到10.(下 ...

  4. hexo文章编写部分语法总结以及hexo使用

    一.hexo的使用 1.1 新建一篇文章 1 $ hexo new [layout] <title> 1.2. 生成静态文件 1 $ hexo generate 可简写为 1 $ hexo ...

  5. 基于arduino的红外传感系统

    一.作品背景 在这个科技飞速发展的时代,物联网已经成为了我们身边必不可少的技术模块,我这次课程设计做的是一个基于arduino+树莓派+OneNet的红外报警系统,它主要通过识别人或者动物的运动来判断 ...

  6. 基于Vue的机器学习平台前端

    项目演示地址:http://vidanao.com/ml>注意1:前端兼容性不太好,360浏览器比较兼容; >注意2:此vidanao.com也是我的个人博文主页,但目前还没部署 源码地址 ...

  7. OpenCV图像增强(python)

    为了得到更加清晰的图像我们需要通过技术对图像进行处理,比如使用对比度增强的方法来处理图像,对比度增强就是对图像输出的灰度级放大到指定的程度,获得图像质量的提升.本文主要通过代码的方式,通过OpenCV ...

  8. Ubuntu 系统下如何安装pip3工具

    一.[导读]Ubuntu 系统内置了 Python2 和 Python3 两个版本的开发环境,却没有内置相应的 pip3 管理工具,本文将介绍如何在Ubuntu下如何快速安装 pip3 工具,并升级到 ...

  9. TARS基金会:构建微服务开源生态

    导语 在20世纪60至70年代,软件开发人员通常在大型机和小型机上使用单体架构进行软件开发,没有一个应用程序能够满足大多数最终用户的需求.垂直行业使用的软件代码量更小,与其他应用程序的接口更简单,而可 ...

  10. Idea安装教程以及环境变量配置

    IDEA安装以及JDK环境变量 环境变量配置 下载jdk