leetcode — maximum-subarray
/**
*
* Source : https://oj.leetcode.com/problems/maximum-subarray/
*
* Created by lverpeng on 2017/7/18.
*
* Find the contiguous subarray within an array (containing at least one number)
* which has the largest sum.
*
* For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
* the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*
* More practice:
*
* If you have figured out the O(n) solution, try coding another solution using
* the divide and conquer approach, which is more subtle.
*
*/
public class MaxSubarray {
/**
* 找到所有子数组中最大的和
*
* 数组从前向后遍历,针对每个数组元素有两个选择,要么加入已经存在子数组,如果该元素的值大于该元素和前面数组总和的和还要大,
* 那就重新开始一个新的子数组,遍历完数组找到最大的和
*
*
* @param arr
* @return
*/
public int maxSubarray (int[] arr) {
int loopCount = 0;
int[] sum = new int[arr.length];
int max = 0;
sum[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
sum[i] = Math.max(arr[i], sum[i - 1] + arr[i]);
max = Math.max(max, sum[i]);
loopCount ++;
}
System.out.println("maxSubarray-->" + loopCount);
return max;
}
public int maxSubarray1 (int[] arr) {
int loopCount = 0;
// 只记录上一个和
int sum = 0;
int max = 0;
for (int i = 1; i < arr.length; i++) {
if (sum < 0) {
sum = 0;
}
sum += arr[i];
max = Math.max(max, sum);
loopCount ++;
}
System.out.println("maxSubarray1-->" + loopCount);
return max;
}
/**
* 使用分治法
*
* @param arr
* @return
*/
int loopCount1 = 0;
public int maxSubarray2 (int[] arr) {
loopCount1 = 0;
return divide(arr, 0, arr.length - 1);
}
private int divide (int[] arr, int low, int high) {
if (low == high) {
return arr[low];
}
if (low == high - 1) {
return Math.max(arr[low] + arr[high], Math.max(arr[low], arr[high]));
}
int mid = (low + high) / 2;
int lmax = divide(arr, low, mid - 1);
int rmax = divide(arr, mid + 1, high);
int mmax = arr[mid];
int temp = mmax;
for (int i = mid - 1; i > 0; i--) {
temp += arr[i];
if (mmax < temp) {
mmax = temp;
}
loopCount1 ++;
}
temp = mmax;
for (int i = mid + 1; i < high; i++) {
temp += arr[i];
if (temp > mmax) {
mmax = temp;
}
loopCount1 ++;
}
System.out.println("maxSubarray2-->" + loopCount1);
return Math.max(mmax, Math.max(lmax, rmax));
}
public static void main(String[] args) {
MaxSubarray maxSubarray = new MaxSubarray();
int[] arr = new int[]{-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubarray.maxSubarray(arr));
System.out.println(maxSubarray.maxSubarray1(arr));
System.out.println(maxSubarray.maxSubarray2(arr));
int[] arr1 = new int[]{-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4,-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubarray.maxSubarray(arr1));
System.out.println(maxSubarray.maxSubarray1(arr1));
System.out.println(maxSubarray.maxSubarray2(arr1));
}
}
leetcode — maximum-subarray的更多相关文章
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...
- LeetCode——Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
- 53. [LeetCode] Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- Python3解leetcode Maximum Subarray
问题描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- LeetCode Maximum Subarray (最大子段和)
题意: 给一个序列,求至少含一个元素的最大子段和? 思路: 跟求普通的最大子段和差不多,只不过需要注意一下顺序.由于至少需要一个元素,所以先将ans=nums[0].接下来可以用sum求和了,如果小于 ...
随机推荐
- 2019.03.09 bzoj5371: [Pkusc2018]星际穿越(主席树)
传送门 题意简述: 给一个序列,对于第iii个位置,它跟[limi,i−1][lim_i,i-1][limi,i−1]这些位置都存在一条长度为111的无向边. 称dist(u,v)dist(u,v) ...
- BJOI2018 简要题解
二进制 序列上线段树维护DDP好题. 题解可以看这篇 代码: #include<bits/stdc++.h> #define ri register int using namespace ...
- 如何在已安装Python解释器的Linux上更新Python
在Linux环境下升级Python (附:解决pip报错 subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned ...
- 解决 Files 的值"<<<<<<< HEAD"无效。路径中具有非法字符
通常我们使用版本控制后会出现诸如此类的问题,此时从vs工具找错误和调试是无法找到问题的,也不影响项目的运行,但是有错误就是得解决.原因是版本控制导致文件的路径出现问题. 解决 Files 的值&quo ...
- IO模型的介绍
Stevens 在文章中的一种IO Model: ****blocking IO #阻塞 IO (系统调用不返回结果并让当前线程一直阻塞,只有当该系统调用获得结果或者超时出错才返回) *** ...
- N!中末尾有多少个0
问题:先从100!的末尾有多少零 => 再推广到 任意N!的末尾有多少个零 分析:首先想到慢慢求解出100!或N!,但计算机表示数有限,且要防止溢出. 则从数学上分析:一 ...
- JQuery 操作数组 each、map、grep、filter
Jquery中对数组的操作大致有一下几种形式:1.$.each( collection, callback(indexInArray, valueOfElement) )$.each()函数和$(se ...
- Linux shell脚本学习(一)
一.shell脚本简介 shell脚本是用shell脚本语法将shell命令组织起来形成的文件形式.Shell脚本与Windows/Dos 下的批处理相似,主要为了将大量命令通过一个纯文本文件一次执行 ...
- SSAS 后端数据库访问模块中存在错误。 为绑定指定的大小太小,导致一个或多个列值被截断。
在处理AS的过程中报错如上,经排查发现原因为数据库 “工号” 字段长度过长导致. 因为我的字段内容基本是人名加工号:张三/1001 不曾想有用户录入非正常数据 :张三/100/1001 这样导致我截取 ...
- tcpdump完全指南
先从第一个最简单的抓包指令开始 抓经过本主机上的所有网络接口的所有ARP.ICMP.IGMP.IP.TCP.UDP等所有网络包(以下简称“所有网络包”) tcpdump -i any -vnn (注: ...