当前位置:网站首页>C语言力扣第53题之最大子数组和。动态规划与分治
C语言力扣第53题之最大子数组和。动态规划与分治
2022-07-24 01:02:00 【管二狗绝不摆烂】
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
提示:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-subarray
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法一:动态规划
其实这道题可以这么想:
1.假如全是负数,那就是找最大值即可,因为负数肯定越加越大。
2.如果有正数,则肯定从正数开始计算和,不然前面有负值,和肯定变小了,所以从正数开始。
3.当和小于零时,这个区间就告一段落了,然后从下一个正数重新开始计算(也就是又回到 2 了)。而 dp 也就体现在这个地方。
动态规划的是首先对数组进行遍历,当前最大连续子序列和为 sum,结果为 ans
如果 sum > 0,则说明 sum 对结果有增益效果,则 sum 保留并加上当前遍历数字
如果 sum <= 0,则说明 sum 对结果无增益效果,需要舍弃,则 sum 直接更新为当前遍历数字
每次比较 sum 和 ans的大小,将最大值置为ans,遍历结束返回结果
时间复杂度:O(n)
方法二:分治法
分治法的思路是这样的,其实也是分类讨论。
连续子序列的最大和主要由这三部分子区间里元素的最大和得到:
第 1 部分:子区间 [left, mid];
第 2 部分:子区间 [mid + 1, right];
第 3 部分:包含子区间 [mid , mid + 1] 的子区间,即 nums[mid] 与 nums[mid + 1] 一定会被选取。
对这三个部分求最大值即可。
说明:考虑第 3 部分跨越两个区间的连续子数组的时候,由于 nums[mid] 与 nums[mid + 1] 一定会被选取,可以从中间向两边扩散,扩散到底 选出最大值,
以下给出动态规划的代码:
int maxSubArray(int* nums, int numsSize) {
int pre = 0, maxAns = nums[0];
for (int i = 0; i < numsSize; i++) {
pre = fmax(pre + nums[i], nums[i]);
maxAns = fmax(maxAns, pre);
}
return maxAns;
}
边栏推荐
- Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
- Tutorial on principles and applications of database system (042) -- MySQL query (4): using wildcards to construct query conditions
- PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)
- Sword *offer04 rebuild binary tree
- Solve the problem that MySQL inserts Chinese garbled code into the table
- [QNX Hypervisor 2.2用户手册]9.1 配置变量
- docker mysql
- QT入门篇(2.1初入QT的开始第一个程序)
- LVS load balancing scheduling principle and configuration method
- 爬虫请求库的使用2
猜你喜欢

Off screen rendering & FBO

Small farmers also have big goals in the test, and the latest big bat interview summary (constantly updating...)

Linx link, first level directory, redirection, CP and MV

PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)

MySQL exercise: all employees reporting to the CEO

黑馬程序員-接口測試-四天學習接口測試-第四天-Postman讀取外部數據文件,讀取數據文件數據,iHRM項目實戰,員工管理模塊,添加員工,批量運行測試用例,生成測試報告,

SkyWalking分布式系统应用程序性能监控工具-上

Focus on microservices

T-seda code

Establishment of static route
随机推荐
IDEA 热部署(热加载)
vim常用命令
[the 83rd fortnight of leetcode]
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
Scroll View implémente la mise à jour déroulante (empêche onload d'entrer dans la page de mise à jour initiale du Shell Triggered pour déclencher la mise à jour déroulante pour True)
Create a self signed certificate to digitally sign exe files
ACL——net
Tutorial on the principle and application of database system (046) -- MySQL query (VIII): group by
[untitled]
IDEA设置 自动导包删无用包
WinVerifyTrust调用返回80096005错误,时间戳签名或证书无法验证或已损坏
OSPF experiment
Creo 9.0 mouse button operation for model observation
测试小码农也有大目标,最新BAT大厂面试题大总结(持续更新中...)
Static extension configuration
Dark horse programmer - interface test - four day learning interface test - day 4 - postman reads external data files, reads data files, IHRM project practice, employee management module, adds employe
项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error
The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is
freemarker
Notes: middle order traversal of binary trees (three solutions - recursion, iteration, Morris)