LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-,,-,,-,,,-,],
Output:
Explanation: [,-,,] has the largest sum = .
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
我的解法:两层循环 第一层循环通过i控制进度,第二层循环计算从i 开始的子数组的和的最大值
C#版解法:
public class Solution {
public int MaxSubArray(int[] nums) {
if(nums.Length == )
return ;
int max = int.MinValue;
for(int i = ; i < nums.Length; i++){
int tempSum=;
for(int j =i; j < nums.Length; j++){
tempSum += nums[j];
if(tempSum > max)
max = tempSum;
}
}
return max;
}
}
看题目描述,可以使用divide and conquer(分而治之)思想去实现,时间复杂度会大幅度降低:这里先将解法贴出来,具体的分而治之(动态规划)的学习放到下一个文章中
static int maxCrossingSum(int[] arr, int l,
int m, int h)
{
// Include elements on left of mid.
int sum = ;
int left_sum = int.MinValue;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
} // Include elements on right of mid
sum = ;
int right_sum = int.MinValue; ;
for (int i = m + ; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
} // Return sum of elements on left
// and right of mid
return left_sum + right_sum;
} // Returns sum of maxium sum subarray
// in aa[l..h]
static int maxSubArraySum(int[] arr, int l,
int h)
{ // Base Case: Only one element
if (l == h)
return arr[l]; // Find middle point
int m = (l + h) / ; /* Return maximum of following three
possible cases:
a) Maximum subarray sum in left half
b) Maximum subarray sum in right half
c) Maximum subarray sum such that the
subarray crosses the midpoint */
return Math.Max(Math.Max(maxSubArraySum(arr, l, m),
maxSubArraySum(arr, m + , h)),
maxCrossingSum(arr, l, m, h));
} /* Driver program to test maxSubArraySum */
public static void Main()
{
int[] arr = { -, , , -, };
int n = arr.Length;
int max_sum = maxSubArraySum(arr, , n - ); Console.Write("Maximum contiguous sum is " +
max_sum);
Console.ReadKey();
}
分而治之的链接:算法学习 分而治之思想
LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习的更多相关文章
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
随机推荐
- aiohttp上报:Got more than 8190 bytes (10160) when reading Status line is too long.错误的解决办法
通过浏览器向web服务传递base64码的图片时遇到参数过长的问题? 解决办法:查看aiohttp的源码:aiohttp/http_parser.py下找到: class HeadersParser: ...
- notepad++编辑软件
官网:http://notepad-plus-plus.org/ 官网截图(2015/4/29): 版本: Notepad++ 6.7.7 下载地址:http://notepad-plus-plus. ...
- Java compiler level does not match the version of the installed Java project facet错误
出现问题情景:从其他地方导入一个项目的时候报错:Java compiler level does not match the version of the installed Java project ...
- 【记录】Redis 基础
Redis可以存放五种类型 1:String(字符串) 2:List(列表) 3:Hash(字典) 4:Set(集合) 5:ZSet(有序集合) String (字符串) redis 127.0.0. ...
- Codeforces 1195E OpenStreetMap 单调队列套单调队列
题意:给你一个n * m的矩阵,问所有的a * b的子矩阵的最小的元素的和是多少.题目给出了矩阵中的数的数据生成器. 思路:如果这个问题是1维的,即求所有区间的最小元素的和,用单调队列O(n)就可以做 ...
- C++ 虚函数和多重继承的内存布局初探
C++ 对象的内存布局 一切以事实说话: 代码: 1: #include <stdio.h> 2: 3: class A { 4: public: 5: int a; 6: int b; ...
- jsp获取url路径的方法
如果你请求的URL是 http://localhost:8080/demo/Index.jsp request.getScheme() //输出:http request.getServerName ...
- JavaSE---多线程---线程的创建、启动
1.概述 1.1 Java中使用Thread类表示线程: 所有的线程对象必须是Thread类 或 其子类的实例: 每条线程的作用:完成一定的任务: Java中使用run方法来封装线程执行体 ...
- v-bin:href 绑定链接
<div id="app06"> <a v-bind:href="URL">我是百度</a> </div> 调用 ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...