leetcode先刷_Maximum Subarray
dp创始人级精英赛的冠军。最大的部分和。
扫从左至右,保持一个最佳值而当前部分和,在这一部分,并成为负值什么时候。再往下的积累后,也起到了负面作用,所以,放弃直销,然后部分和初始化为阅读的当前位置。
class Solution {
public:
int maxSubArray(int A[], int n) {
int mmax = A[0], tpsum = A[0];
for(int i=1;i<n;i++){
if(tpsum<0) tpsum = A[i];
else tpsum += A[i];
if(tpsum > mmax)
mmax = tpsum;
}
return mmax;
}
};
版权声明:本文博客原创文章。博客,未经同意,不得转载。
leetcode先刷_Maximum Subarray的更多相关文章
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- [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 ...
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- leetcode-Maximum Subarray
https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (contai ...
随机推荐
- Android版本铎A梦幻连连看游戏源代码完整版
我写主代码,没有版权问题,它少量小游戏和应用源代码稍后会陆续上线!哆啦A梦连连看包含了2种游戏模式和60关卡,并能够通过改动配置文件非常方便的实现自行添加新关卡.採用andengine游戏引擎开发,内 ...
- sgu 286. Ancient decoration(最小环覆盖)
给你一个n个点,每个点度为k(k为偶数)的无向图,问是否能将图中的n条边染色,使得每个点都拥有两条被染色的边.也就是说,是否存在拥有原图中n条边的子图,使得每个点的度为2?仔细想想,每个点的度为2,实 ...
- STL源代码分析——STL算法remove删除算法
前言 因为在前文的<STL算法剖析>中,源代码剖析许多.不方便学习,也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的remove删除算法. ...
- FREESWITCH SEESION
SESSION SESSION为FS的核心概念之一,所以需要拿出来专门的分析下. 从以下几个方面进行分析,结构类型,资源的管理,对于呼叫的意义,规格. 1.结构类型 每一次呼叫会申请一个session ...
- 【Web优化】Yslow优化法则(四)启用Gzip压缩
Yslow的第4个经验法则指出:启用gzip压缩功能,能够降低HTTP传输的数据和时间,从而降低client请求的响应时间. 本篇是Yslow法则的第四个,主要包含三个方面的内容: 1. 什 ...
- lua简洁的功能(两)
Lua中的函数带有词法定界的第一类值. 第一类值: 在Lua中,函数和其它值(数值,字符串)一样,函数能够被存放在变量中,也存放在表中, 能够作为函数的參数,还能够作为函数的返回值. 词法定界:被嵌套 ...
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- Socket通信原理
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- LoaderManager使用具体解释(二)---了解LoaderManager
了解LoaderManager 这篇文章将介绍LoaderManager类,这是该系列的第二篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四:实例: ...
- repeater操作
protected void rpRole_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ...