【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53)
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.
简要解析:
本道题主要考查最大连续子序列和的求解方式。
我们可以这样考虑:当我们从头到尾遍历这个数组的时候,对于数组里的一个整数,它有两种选择:1. 加入之前的SubArray;2. 自己另起一个 SubArray。
考虑到有这两种情况,如果之前SubArray的总体和大于0,则认为其对后续结果有益,选择加入之前的SubArray
如果之前SubArray的总体和为0或者小于0,则认为其对后续结果无益,甚至是有害(小于0时),这种情况下选择以这个数字开始,另起一个 SubArray。
设状态为f[j],表示以 S[j] 结尾的最大连续子序列和,则状态转移方程如下:
f[j] = max{f[j −1] + S[j],S[j]}, 其中1 ≤ j ≤ n
target = max{f[j]}, 其中1 ≤ j ≤ n
n 情况一:S[j] 不独立,与前面的某些数组成一个连续子序列,则最大连续子序列和为 f[j −1] + S[j]。
n 情况二:S[j] 独立划分成为一段,即连续子序列仅包含一个数 S[j],则最大连续子序列和为 S[j]。
实现代码:
// LeetcodeTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std; class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
int result = nums[0], count=0;
for(int i=0; i<n; i++){
count +=nums[i];
result = max(result,count);
if(count<0)
count=0;
}
return result;
}
}; int main(){
vector<int>nums;
for(int i=0; i<6; i++){
nums.push_back(i);
}
Solution s;
cout<<s.maxSubArray(nums)<<endl;
system("pause");
return 0;
}
【leetcode】Maximum Subarray (53)的更多相关文章
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 【转】Java面试题全集2.2(上)
146.什么是IoC和DI?DI是如何实现的? 答:IoC叫控制反转,是Inversion of Control的缩写,DI(Dependency Injection)叫依赖注入,是对IoC更简单的诠 ...
- 基于Spring4+Hibernate4的通用数据访问层+业务逻辑层(Dao层+Service层)设计与实现!
基于泛型的依赖注入.当我们的项目中有很多的Model时,相应的Dao(DaoImpl),Service(ServiceImpl)也会增多. 而我们对这些Model的操作很多都是类似的,下面是我举出的一 ...
- python word
代码: #coding=utf-8 __author__ = 'zhm' from win32com import client as wc import os import time import ...
- cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
首先,是个敲代码的,基本上都知道程序的入口是main函数,显然,就算在cocos2d-x框架中也一样 我们看看main函数做了什么 #include "main.h" #inclu ...
- 变量改变时PHP内核做了些什么?
引言 内容来自于<Extending and Embedding PHP>- Chaper 3 - Memory Management,加上自己的理解,对php中变量的引用计数.写时复制, ...
- MySQL索引背后的数据结构及算法原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- Example: Encoded SNMP Message - SNMP Tutorial
30.11 Example Encoded SNMP Message The encoded form of ASN.1 uses variable-length fields to represen ...
- winform 多线程编程
参考资料: WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件) c# 使用定时器Timer
- LYDSY模拟赛day1 Tourist Attractions
/* 假设路径是 a − b − c − d,考虑枚举中间这条边 b − c,计 算有多少可行的 a 和 d. 设 degx 表示点 x 的度数,那么边 b − c 对答案的贡献为 (degb − 1 ...
- 04OC之分类Category,协议Protocol,Copy,代码块block
一.Protocol协议 我们都知道,在C#有个规范称之为接口,就是规范一系列的行为,事物.在C#中是使用Interface关键字来声明一个接口的,但是在OC中interface是用来声明类,所以用了 ...