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. 解密JDK8 枚举

    写一个枚举类 1 2 3 4 5 6 public enum Season { SPRING, SUMMER, AUTUMN, WINTER } 然后我们使用javac编译上面的类,得到class文件 ...

  2. array, matrix, list and dataframe

    总结一下"入门3R"(Reading, 'Riting, 'Rrithmetic)中的读和写,不同的数据结构下的读写还是有点区别的. vector 命名 12 month.days ...

  3. Codeforces Round #612 (Div. 2)C. Garland

    第四次写题解,请多指教! http://codeforces.com/contest/1287/problem/C题目链接 题目大意是有一个数字串挂有1-n n个数字,现在上面缺失了一些数字,让你找出 ...

  4. SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

  5. JDK 1.8 新特性之Date-Time API

    来源:请点击查看 1.8之前的日期类: 线程不安全:java.util.Date 这个类线程不安全,而且所有日期类都是可变的. 时间处理麻烦:默认的开始日期从1900年,不支持国际化,不提供时区支持, ...

  6. MyBatis配置文件中config与mapper的约束

    本文链接:https://blog.csdn.net/gaoxin_gx/article/details/100183455 Config的约束: <?xml version="1.0 ...

  7. 【Django】接收照片,储存文件 前端代码

    后端: from rest_framework.views import APIView from car import settings from django.shortcuts import r ...

  8. C++ 命令行窗口打印二叉树(图形)

    写这个程序的目的是学习数据结构的时候方便调试,学习起来也比较直观. 这个是我测试SplayTree时候的gif STEP 1 新建一个头文件,命名为DrawATree.hh, 将以下内容复制进去 #i ...

  9. JS中面向对象中的继承(常用写法)---核心部分

    1.基本概念 子类继承父类,但是不能影响父类.包括1.混合继承(构造函数+原型) 2.ES6新增class的继承. 接下来介绍,面向对象中继承的两种常用写法.即混合继承(构造函数+原型)和class继 ...

  10. 树莓派3b+ 交叉编译 及升级 kernel

    安装 gcc pkg 等工具sudo apt-get install build-essential git 官方介绍 https://www.raspberrypi.org/documentatio ...