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-2020-4 《Java 程序设计》第七周总结
2019-2020-4 <Java 程序设计>第七周知识总结 第八章:常用实用类 一.String类 String类的构造方法 public String(byte[] bytes); p ...
- centos7 防火墙相关命令
启动:systemctl start firewalld禁用:systemctl stop firewalld重新载入规则:firewall-cmd --reload查看所有打开的端口:firewal ...
- go语言的运算符
什么是运算符:运算符用于在程序运行时执行数学或逻辑运算 go语言的运算符如下: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 一,算数运算符 运算符 描述 实例 + 相加 A ...
- HTML标签有序标签和无序标签
1.<ul>标签定义无序列表,所谓无序,是指以●.○.▽.▲等开头的,没有顺序的列表项目 1.1 设置无序列表的类型—type 无序列表的默认符号是圆点(● ). ...
- Maven学习3(中央仓库)
Maven项目在运行的时候,会首先找本地仓库是否有需要的jar,如果没有则去调用远程仓库. 解读Maven在仓库中的存储路径: 1.基于groupId准备路径,将句点分隔符转成路径分隔符,就是将 & ...
- Delphi XE7试用记录1
Delphi XE7试用记录1 在网上看到XE7的一些新特征,觉得完整Unicode支持.扩展Pascal语法.更多功能的库都很吸引人,决定试试XE7. XE7官方安装程序很大,因此选择了lite版, ...
- win32控制台程序 宽字符与短字符转化
由于vs各版本之间存在字符设置不兼容问题,特总结char与tchar的互相转换函数,如下,在之后的工程中可以使用. void TcharToChar(const TCHAR * tchar, char ...
- Redhat7配置ali-yum源
1.删除所有包 rpm -qa|grep yum|xargs rpm -e --nodeps 2.下载相关文件 下载地址 https://mirrors.aliyun.com/centos/7/os ...
- 《HTTP权威指南》3-HTTP报文
报文流 HTTP报文是在HTTP应用程序之间发送的数据块,这些数据块以文本形式的元信息开头,这些信息描述了报文的内容及含义,后面跟着可选的数据部分.这些报文在客户端,服务器和代理之间流动. 报文的组成 ...
- Java 并发编程:核心理论
并发编程是Java程序员最重要的技能之一,也是最难掌握的一种技能.它要求编程者对计算机最底层的运作原理有深刻的理解,同时要求编程者逻辑清晰.思维缜密,这样才能写出高效.安全.可靠的多线程并发程序.本系 ...