LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray
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.
方法一
首先想到的就是比较暴力,没有技巧性可言的时间复杂度为O(\(n^{2}\))的方法。显而易见,这个方法重复计算了很多,非常没有效率。
int maxSubArray(int* nums, int numsSize) {
int max = nums[0];
for (int i = 0; i < numsSize; i++) {
int sum = nums[i];
if (sum >= max) max = sum;
for (int j = i+1; j < numsSize; j++) {
if (max <= sum+nums[j])
max = sum+nums[j];
sum += nums[j];
}
}
return max;
}
方法二
这道题目还可以利用动态规划的算法,通过维护全局最优变量和局部最优变量,局部最优是一定要包含当前元素,local = (local < 0) ? nums[i] : local+nums[i];。有了当前一步的局部最优,那么全局最优就是当前的局部最优或者还是原来的全局最优,global = (local > global) ? local : global;。
这个动态规划方法的时间复杂度可以降低到O(n),可以说是非常acceptable了。
int maxSubArray(int* nums, int numsSize) {
int global = nums[0], local = nums[0];
for(int i = 1; i < numsSize; i++) {
local = (local < 0) ? nums[i] : local+nums[i];
global = (local > global) ? local : global;
}
return global;
}
LN : leetcode 53 Maximum Subarray的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 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 ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- C#解leetcode 53.Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- 2014ACM/ICPC亚洲区西安站现场赛 F color(二项式反演)
题意:小球排成一排,从m种颜色中选取k种颜色给n个球上色,要求相邻的球的颜色不同,求可行的方案数,答案模1e9+7.T组数据,1<= n, m <= 1e9, 1 <= k < ...
- hdu3756(三分)
题意:三维坐标轴,有以原点为圆心,底面在xoy平面上,顶点在z轴上的圆锥,问圆锥的最小体积为多少才能完全覆盖空间里的所有点(n<=10000) 分析: 很容易想到转成二维问题,将其投影到xoz平 ...
- Justinmind使用教程(2)——计算表达式及条件用法
Justinmind的计算表达式以及条件condition的使用对于刚開始学习的人而言比較麻烦. 结合网上了一个教程本文主要针对计算器演示样例进行计算表达式以及条件的使用. 实现目标:依据单位价格(静 ...
- Cookies 初识 Dotnetspider EF 6.x、EF Core实现dynamic动态查询和EF Core注入多个上下文实例池你知道有什么问题? EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)
Cookies 1.创建HttpCookies Cookie=new HttpCookies("CookieName");2.添加内容Cookie.Values.Add(&qu ...
- Android系统之路(初识MTK) ------ OTA打包ROM安装系统img等到ZIP
在做OTA升级包的时候,我编译了好多次都没过.老是IO异常.刚開始以为是我 make 的错误.后来多次检查 Error 发现是我的配置信息写错了,与驱动project师一起检查源代码, 改动配置信息后 ...
- Rust hello world 语法解说
Rust的hello world代码例如以下: fn main() { println!("Hello, world!"); } 1.fn main() fn main(){ ...
- the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
the largest value you actually can transmit between the client and server is determined by the amoun ...
- 【OI】指针线段树&指针
对于线段树,我们一般需要n*4的空间去存储线段树,然后有一种玄学操作是用指针来实现线段树. #include <inttypes.h> #include <algorithm> ...
- 【LIS】Luogu P1020 导弹拦截
昨天晚上看蓝书,看到了LIS问题的优化解法. 是比O(n方)更快的解法,实际上是一个常数优化. 先讲一下朴素的解法: 一个集合a,a[i]是第i个元素.设dp[i]为以编号为i的元素结尾的最长不上升子 ...
- YTU 2504: 蚂蚁感冒
2504: 蚂蚁感冒 时间限制: 1 Sec 内存限制: 128 MB 提交: 273 解决: 118 题目描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右.每只蚂蚁都只能沿 ...