17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum.

LeetCode上的原题,请参见我之前的博客Maximum Subarray

解法一:

int get_max_sum(vector<int> nums) {
int res = INT_MIN, sum = INT_MIN;
for (auto a : nums) {
sum = max(sum + a, a);
res = max(res, sum);
}
return res;
}

解法二:

int helper(vector<int> nums, int left, int right) {
if (left >= right) return nums[left];
int mid = left + (right - left) / ;
int lmax = helper(nums, left, mid - );
int rmax = helper(nums, mid + , right);
int mmax = nums[mid], t = nums[mid];
for (int i = mid - ; i >= left; --i) {
t += nums[i];
mmax = max(mmax, t);
}
t = mmax;
for (int i = mid + ; i <= right; ++i) {
t += nums[i];
mmax = max(mmax, t);
}
return max(mmax, max(lmax, rmax));
} int get_max_sum(vector<int> nums) {
return helper(nums, , nums.size() - );
}

CareerCup All in One 题目汇总

[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum (最大连续子序列之和)

    Given a sequence of K integers { N1, N2, ..., *N**K* }. A continuous subsequence is defined to be { ...

  2. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  3. zoj1003-Max Sum (最大连续子序列之和)

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  4. hdu1003 Max Sum【最大连续子序列之和】

    题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...

  5. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

  6. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. linux 用户之间的切换

    从root用户切换到普通用户fxm, 使用如下命令:su - fxm 从普通用户切换到root用户,使用如下命令:su - 或者 su, root可以省略不写.

  2. MySQL中的约束简单使用

    数据库约束是为了保证数据的完整性而实现的一套机制,它具体的根据各个不同的数据库的实现而有不同的工具.一般来说有以下几种实现方式:1.检查约束:通过在定义数据库表里,在字段级或者是在表级加入的检查约束, ...

  3. python学习第一天

    编码:encode 解码:decode 计算长度:len list:索引从0开始 -1可以索引最后一个元素 append添加元素 insert插入元素 pop删除末尾元素 替换的话直接赋值 list里 ...

  4. opengl常用函数

    glAccum 操作累加缓冲区   glAddSwapHintRectWIN 定义一组被 SwapBuffers拷贝的三角形   glAlphaFunc允许设置alpha检测功能   glAreTex ...

  5. 获取APK签名

    获取apk签名工具类 import android.content.Context; import android.content.pm.PackageInfo; import android.con ...

  6. 解决android expandablelistview 里面嵌入gridview行数据重复问题

    最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...

  7. C#中Invoke的用法()

    invoke和begininvoke 区别 一直对invoke和begininvoke的使用和概念比较混乱,这两天看了些资料,对这两个的用法和原理有了些新的认识和理解. 首先说下,invoke和beg ...

  8. windows修改虚拟内存

    右键电脑属性: 高级系统设置: 性能设置: 虚拟内存更改:

  9. hdu3496 二维01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3496 //刚看题目以为是简单的二维01背包,but,,有WA点.. 思路:题中说,只能买M ...

  10. JavaScript笔试必备语句

    1. document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >(head,bod ...