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求和了,如果小于 ...
随机推荐
- Linux编程基础——GDB(设置断点)
启动GDB后,首先就是要设置断点,程序中断后才能调试.在gdb中,断点通常有三种形式: 断点(BreakPoint): 在代码的指定位置中断,这个是我们用得最多的一种.设置断点的命令是break,它通 ...
- duilib窗口从任务栏恢复问题
关闭.最大最小化和恢复等消息由WM_SYSCOMMAND和OnSysCommand()进行处理,需要在HandleMessage()中添加处理.
- (PMP)第5章-----项目范围管理
产品范围:所具有的特征和功能 项目范围:必须完成的工作. 5.1 规划范围管理 输入 工具与技术 输出 1.项目章程 2.项目管理计划 (质量管理计划, 项目生命周期描述, 开发方法) 3.事业环境因 ...
- Ubuntu之sudo权限管理/etc/sudoers文件
网易云音乐翻车记 系统安装的Ubuntu18.04桌面版,安装网易云客户端后,还没来得及夸奖,发现点击图标打不开后网上找到教程:Ubuntu网易云音乐无法打开 感觉挺靠谱的,照着最下边的教材修改了一波 ...
- Android Studio 3.1.2 修改字体(font)大小(size) 及老版本修改主题、字体、颜色 参照地址
Android Studio 3.1.2 修改字体(font)大小(size) 步骤:File-Settings-Editor-Color Scheme-Color Scheme Font-Size ...
- ES6 generators in depth 一(译)
今天在学习redux-saga时,外部链接推荐了这篇文章ES6 generators in depth,所以翻译的同时也可以加深一下对Generator的理解. 这里对原文一些只能在高版本现代浏览器使 ...
- 初识大数据(二. Hadoop是什么)
hadoop是一个由Apache基金会所发布的用于大规模集群上的分布式系统并行编程基础框架.目前已经是大数据领域最流行的开发架构.并且已经从HDFS.MapReduce.Hbase三大核心组件成长为一 ...
- VS中Debug与Release、_WIN32与_WIN64的区别
一.Debug与Release 1. 区别 Debug——调试版,生成的.exe中包含很多调试信息,若直接发包,比较大: Release——发布版 2. 如何区分是Debug编译还是Release ...
- BZOJ4720-换教室
题目很长,是一道概率dp题,一般需要逆推,但这题结局不确定所以要顺推. 用f[i][j][k],i表示第i段时间,j表示用了j次申请,k就表示这轮是否用申请. 那么要求min(f[n][0~m][0] ...
- Matlib
>>> name1=input('请输入第一个名字;') 请输入第一个名字;陈汉彬 >>> name2=input('请输入第二个名字;') 请输入第二个名字;钟宇 ...