博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
阅读量:7065 次
发布时间:2019-06-28

本文共 962 字,大约阅读时间需要 3 分钟。

 

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上的原题,请参见我之前的博客。

 

解法一:

int get_max_sum(vector
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
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 = nums[mid]; 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));}int get_max_sum(vector
nums) { return helper(nums, 0, nums.size() - 1);}

 

转载地址:http://gsxll.baihongyu.com/

你可能感兴趣的文章
Win2008学习(十一),解决Remote App Web访问的证书问题
查看>>
python 实现 自动oa 签到签退 发送邮件提醒
查看>>
今天打开阿里妈妈惊现 ¥50 元佣金
查看>>
Oracle 正确删除archivelog文件
查看>>
微信JS 关闭网页
查看>>
[AAuto]给百宝箱增加娱乐功能
查看>>
Tigase XMPP Server源码部署
查看>>
Intellij IDEA创建Maven Web项目
查看>>
java 7 入门书籍
查看>>
Android Pdf文档的生成、显示与打印
查看>>
SpringMVC三种异常处理方式
查看>>
w命令
查看>>
golang使用oracle碰到go/lib/time/zoneinfo.zip: no such file or directory
查看>>
quartz定时任务时间设置描
查看>>
ES6常用语法
查看>>
https://www.jianshu.com/p/dbffae16ba0b
查看>>
微信,QQ这类IM app怎么做——谈谈Websocket
查看>>
在Ubuntu 11.04中安装Openresty
查看>>
JAVA常见的面试题
查看>>
《Python高效开发实战》实战演练——建立应用2
查看>>