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求和了,如果小于 ...
随机推荐
- 第二周工作总结——NWNU李泓毅
注:因作业未全部提交完毕,故评分细则和千帆图等评分事项推后 1.助教博客链接:https://www.cnblogs.com/lmcmha/ 2.本周点评作业: https://www.cnblogs ...
- Liang-Barsky直线段裁剪算法
Liang-Barsky直线段裁剪算法 梁友栋与Barsky提出的裁剪算法以直线的参数方程为基础,把判断直线段与窗口边界求交的 二维裁剪问题转化为求解一组不等式,确定直线段参数的一维裁剪问题.设起点为 ...
- Unity3D中默认函数的执行顺序
直接用一张图来说明各个默认函数的执行顺序: FixedUpdate以固定的物理时间间隔被调用,不受游戏帧率影响.一个游戏帧可能会调用多次FixedUpdate.比如处理Rigidbody的时候最好用F ...
- Python 虚拟环境 pyenv、venv(pyvenv)、virtualenv之间的区别
请参考连接 https://blog.zengrong.net/post/2167.html https://blog.csdn.net/lanonjj/article/details/5105021 ...
- Day10 (黑客成长日记) Urllib库的使用
什么是Urllib: Urllib是python内置的HTTP请求库包括以下模块urllib.request 请求模块urllib.error 异常处理模块urllib.parse url解析模块ur ...
- Maven搭建SSH框架
工具:Eclipse(Maven管理工具)+Tomcat+Mysql. 1.新建一个Maven工程(maven-archetype-webapp). 打开File ——>new——>Mav ...
- Spring aop框架使用的jar包
除了前两个jar包,后面的jar包spring框架包中都有,前两个jar包的下载地址:https://pan.baidu.com/s/1L-GLGT1c8vnwFwqLxzzZuw
- 微软develop apps在QQ上部分功能的实现
最近我对微软的develop apps的文档进行了简读,在感叹UWP在支持服务上的全面的同时,我不禁在在常用的APP上对于这些功能支持进行了部分的寻找对应.而我进行功能对照的,就是平时很常用的一款手机 ...
- Python序列结构--集合
集合:元素之间不允许重复 集合属于Python无序可变序列,元素之间不允许重复 集合对象的创建与删除 直接将值赋值给变量即可创建一个集合 >>> a = {3,5}>>& ...
- shell 命令 创建/删除 软连接 ln -s
软链接的作用是, 1. 节省复制造成的空间浪费 2. 保证两个文件的内容同时修改 所以,可以把软连接理解为给文件/文件夹创建了别名,当访问别名时,实际访问的是链接的文件/文件夹 软链文件 ln -s ...