[LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum.
Notice
The subarray should contain at least one number.
Given the array [−2,2,−3,4,−1,2,1,−5,3]
, the contiguous subarray [4,−1,2,1]
has the largest sum = 6
.
Can you do it in time complexity O(n)?
LeetCode上的原题,请参见我之前的博客Maximum Subarray。
解法一:
class Solution { public: /*** @param nums: A list of integers* @return: A integer indicate the sum of max subarray*/int maxSubArray(vector<int> nums) {int res = INT_MIN, curSum = 0;for (int num : nums) {curSum += num;curSum = max(curSum, num);res = max(res, curSum);}return res;} };
解法二:
class Solution { public: /*** @param nums: A list of integers* @return: A integer indicate the sum of max subarray*/int maxSubArray(vector<int> nums) {if (nums.empty()) return 0;return helper(nums, 0, (int)nums.size() - 1);}int helper(vector<int>& nums, int left, int right) {if (left >= right) return nums[left];int mid = left + (right - left) / 2;int lmax = helper(nums, left, mid - 1);int rmax = helper(nums, mid + 1, right);int mmax = nums[mid], t = mmax;for (int i = mid - 1; i >= left; --i) {t += nums[i];mmax = max(mmax, t);}t = mmax;for (int i = mid + 1; i <= right; ++i) {t += nums[i];mmax = max(mmax, t);}return max(mmax, max(lmax, rmax));} };
[LintCode] Maximum Subarray 最大子数组相关推荐
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray(最大子数组乘积)
Find the contiguous subarray within an array (containing at least one number) which has the large ...
- LintCode Python 简单级题目 41.最大子数组 - 44.最小子数组和
题目1 最小子数组 描述: 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 样例 给出数组[1, -1, - ...
- [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray
41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...
- lintcode 最大子数组III
题目描述 给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大.每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [-1 ...
- Lintcode42 Maximum Subarray II solution 题解
[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...
- [经典面试题][淘宝]求首尾相连数组的最大子数组和
题目 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是相连的.数组中一个或多个连续元素可以组成一个子数组,其中存在这样的子数组arr[i],-arr[n- ...
- Leetcode52.Maximum Subarray(贪心与分治)
题目描述: Given an integer array nums, find the contiguous subarray (containing at least one number) whi ...
- LeetCode Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
最新文章
- keras 的 example 文件 mnist_cnn.py 解析
- 从无头单链表中删除节点 结构之法 4
- 四种可变交流swap方法
- 教程丨亿万网友一起换头像欢庆新中国成立70周年,就差你了!
- kdj买卖指标公式源码_通达信指标公式源码MACD背离KDJ背离指标
- matlab使用tic 和 toc记录程序执行时间
- matlab常用的代码,matlab常用代码
- build openni with gcc 7
- 会话技术——Cookie和Session
- 读《概率论与数理统计(陈希孺)》关于几何概率与伯特兰悖论的随笔
- 地图标识符号大全_资源小结:分省地图查询(9.1版)
- 按键消抖的Verilog实现
- win10计算器rsh_如何打开win10计算器 教你打开win10计算器的方法
- mysql lambda查询_SqlSugar常用查询实例-拉姆达表达式
- 可以插卡的ipad_可以插卡的ipad是几代
- HTTP请求以及接收的方式
- html 的(热点区域的建立、框架的建立)
- 战地3皓月服务器win10系统,战地3配置
- CANoe.DiVa操作指南——基于DoIP使用CANoe.DiVa用于UDS一致性测试
- 《图解TCP/IP》阅读笔记(第九章)—— 网络安全相关
热门文章
- Python 是否是下一个 PHP?为什么?
- 用memcache.php监测memcache的状况
- 提供前进、后退功能及其他JAVASCRIPT速成秘诀
- request.getSession(false)到底返回什么
- Netbeans使用maven下载源码
- gtest使用初级指南
- VS2013中Image Watch插件的使用(OpenCV)
- 卢京潮自动控制原理ppt_独家资料|29套输配电相关视频教程+PPT课件 ,全方位讲解电气传动自动控制系统、供配电实用技术、自动控制原理,免费领!...
- ajax和map返回数据类型,ajax请求后台返回map类型并如何展示
- java 数组越界异常_数组越界异常 求解决!!!