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求和了,如果小于 ...
随机推荐
- dede网站安全要做的四件事
1,把安装install的文件夹删除:2,关闭member系统,禁止member文件写入:3,把data文件外移到网站根目录之外:4,把included和plus目录的写入权限关闭. 转载自:http ...
- VS2017 Debug断点后显示UTF8字符串
断点后跟踪字幕文件文本,因为国内字幕一般是UTF8的,VS默认显示不出来,在变量上双击,加入 ,s8就可以了 默认 修改后 其他 ,数字 将变量拆分为数组显示, 数字是要显示多少位, 此法对cons ...
- CentOS 7 rabbitmq 安装
OS版本:CentOS 7.2Rrlang:19.2RabbitMQ:3.6.6 1.1 erlang安装 1. http://www.erlang.org/下载erlang,解压缩,进入目录,检查环 ...
- Unity使用代码动态给按钮赋值各个状态下的图片
一个小知识点,怕忘记,所以记录下.废话不多说,直接上代码: 未赋值之前: 使用下面代码赋值: using UnityEngine; using UnityEngine.UI; public class ...
- MySQL 多表结构的创建与分析
=====================多对一===================== create table press( id int primary key auto_increment, ...
- 网页偶现性崩溃-chrome
简介: 项目前台框架:Angular2 + Bootstrap(日期等组件) + Echarts + 响应式(包括页面.字体缩放:rem) chrome版本:多个版本测试均有此问题. 表现: 订单详情 ...
- android-基础编程-ViewPager
ViewPager android 提供的基础V4包,android studio 导入gradle compile 'com.android.support:support-v4:25.0.0' 1 ...
- OOP随笔
父类为普通类: 内部可声明虚方法(virtual),并包含代码实现,子类中可以重写(override),也可以不重写直接用. 父类为(不可实例化的)抽象类: 可声明虚方法,同上. 也可以声明抽象方法( ...
- Codeforces831D Office Keys
D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 转Centos7.0进入单用户模式修改root密码
Centos7.0进入单用户模式修改root密码 启动Centos7 ,按空格让其停留在如下界面. 按e进行编辑 在UTF-8后面输入init=/bin/sh 根据提示按ctrl+x 得如下图 输 ...