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 ...
随机推荐
- JSON/xml、Processing 以及收集Java的设计模型
JSON简介: 1.基本介绍 JSON(JavaScriptObject Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于ECMAScript(欧洲计算机协会制定的js规范)的 ...
- JSP的调试
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/debugging.html: 一.使用System.out.println() System.out.p ...
- easyui webuploader 文件上传演示
webuploader 上传首页 webuploader 上传前页面 webuploader 上传中页面 图就不上传了,状态会编程上传中 webuploader 已上传页面
- neutron trouble shooting - ip can not ping
neutron创建了一个router后,显示列表如下: [root@controller01 keystone]# neutron router-port-list router +--------- ...
- C# .NET想要另存一个项目,sln文件丢了怎么办
如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到. 很简单的一个思路是反正sln是多 ...
- influxdb常用命令
创建数据库 create database DBName 删除数据库 drop database DBName 使用指定数据库 use DBName 显示所有表 SHOW MEASUREMENTS 删 ...
- hdu 4864 Task(贪心)
pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 大致题意:有n台机器和m个任务,都有两个參数工作时间time和难度le ...
- 99_leetcode_Best Time to Buy and sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- overwrite 复制
[root@myv xiaole_dl_img]# cp upfc/mainDEBUGmysqllogTEST.py online_package_test_/tmp/ cp: overwrite ‘ ...
- android判断正在使用的网络类型 0.不知道网络类型;1、2G;2、3G;3、4g;4、wifi
判断正在使用的网络类型 0.不知道网络类型:1.2G:2.3G:3.4g:4.wifi /** Unknown network class. {@hide} */ public static fina ...