当前位置:网站首页>53 LeetCode 】 【. Most architectural array and
53 LeetCode 】 【. Most architectural array and
2022-07-29 15:04:00 【Crispy~】
给你一个整数数组 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
题解:
动态规划,dpThe array stores the maximum value of each bit,那么对于每个i来说,dp(i) = max{ dp(i-1)+nums[i], nums[i] },Then the maximum sum isdp数组中的最大值
//C++
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int len = nums.size();
vector<int> dp(len);
dp[0] = nums[0];
for(int i=1;i<len;i++)
{
dp[i] = max(dp[i-1]+nums[i],nums[i]);
}
return *max_element(dp.begin(),dp.end());
}
};
class Solution(object):
def maxSubArray(self, nums):
n = len(nums)
dp = [0 for i in range(n)]
dp[0] = nums[0]
for i in range(1,n):
dp[i] = max(dp[i-1]+nums[i],nums[i])
return max(dp)
边栏推荐
猜你喜欢
测试日报怎么写 ?
软件测试架构师的工作日常
基于Flink CDC打通数据实时入湖
全面质量管理理论
如何编辑CAD图库里的图纸
图斑自上而下,自左而右顺序编码,按照权属单位代码分组,每组从1开始编码
ArcGIS Pro与ArcGis区别
我裁完兄弟们后,辞职了,转行做了一名小职员
Google Cloud X Kyligence|如何从业务视角管理数据湖?
Chinese Internet technology companies were besieged by wolves. Google finally suffered a severe setback and its profits fell sharply. It regretted promoting the development of Hongmeng...
随机推荐
升级openssl1.1.1(mix2s哪个版本不断流)
LeetCode_494_目标和
【微服务】(十六)—— 分布式事务Seata
用Asm生成Class字节码文件
基于JSP&Servlet实现的众筹平台系统
如何使用SparkSQL做一些简单的数据分析和可视化展示?
关于内部类
换掉 UUID,更快、更安全!
WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1
How to return all prime factors of a number?
Programmers are a group with a high incidence of occupational diseases. Don’t be naive to think that it’s just as simple as being bald.
求教一下 现在最新版的flinkcdc能获取到oracle的ddl变更信息吗?
Linux installation of MySQL (super detailed)
uni 的下拉式筛选菜单的功能/图片懒加载
全球级的分布式数据库 Google Spanner原理 热:报错
如何返回一个数字的所有质因数?
第4章_3——索引的使用
上个厕所的功夫,就把定时任务的三种调度策略说得明明白白
威纶通触摸屏制作自定义欢迎界面的几种方法介绍
RAMAN CONFIGURE 命令都能实现哪些功能