leetcode题记:Maximum Subarray

STM32+OpenCV云台PID调参实战:从震荡到丝滑跟踪的进阶之路 本文详细介绍了基于STM32和OpenCV的跟踪云台PID调参实战经验。从硬件搭建、PID控制原理到调参技巧,系统讲解了如何通过调整比例、积分和微分参数解决系统震荡问题,实现从震荡到丝滑跟踪的进阶过程,特别适合电赛和嵌入式视觉控制开发者参考。 阅读详情

编程语言:JAVA

题目描述:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

解题思路:

这道题题目非常简单,但是做起来就没那么简单了。博主首先想到的也是唯一想到的方法就是暴力穷举法,时间复杂度很高O(3),果不其然,提交的时候超时。


Kadane Algorithm主要思想是,假设,数组中前i-1项的和为sum[i-1],对于第i个数字,则由公式


这个公式的直观解释为:当前i-1个数组的和加上arr[i]还没有arr[i]大的时候,就不考虑前i-1个数字,从当前数字开始新的计算。

保留一个max保存最大的值,每次都和max比较。

代码:

class Solution {
    public int maxSubArray(int[] nums) {
        int res = Integer.MIN_VALUE, curSum = 0;
        for (int num : nums) {
            curSum = Math.max(curSum + num, num);
            res = Math.max(res, curSum);
        }
        return res; 
    }
}

解法二:

这道题题目中的提示是要按照分制法来求解的,显然之前的算法不太符合题目本意。

电子图像增稳(EIS)原理详解——从抖动补偿到代码实现 电子图像增稳(EIS)是一种通过软件算法实现视频稳定的技术,广泛应用于GoPro、手机和无人机等设备。与光学防抖(OIS)和机械云台不同,EIS通过图像裁切和运动补偿来消除抖动,具有成本低、补偿范围大的优点,但会损失10-30%的画幅。其核心流程包括运动检测、抖动分离、运动补偿和图像裁切四个步骤。相比硬件方案,纯软件实现的EIS更适合消费级电子产品,已成为移动设备视频拍摄的重要技术。 阅读详情

相关推荐

COMSOL仿真教程—激光烧蚀

本例使用二维模型,将来自激光的入射热通量模拟为金属表面上空间分布的热源,从而得到金属的烧蚀过程和瞬态温度分布。

qq_40977332的博客 2万+

Leetcode之Maximum Average Subarray I

问题描述: Given an array consisting of n integers, find the contiguous subarray of given lengthk that has the maximum average value. And you need to output the maximum average value. 示例: Input: [1,12,-

漫漫学习路 2975

Excel药品说明书数据库:从零开始搭建医药数据查询系统(附完整模板)

本文详细介绍了如何从零开始搭建Excel药品说明书数据库,提供完整的医药数据查询系统模板。涵盖数据库架构设计、数据清洗标准化、智能查询系统搭建及维护优化,帮助医药从业者高效管理药品信息,提升查询速度和准确性。

weixin_28327051的博客 234

LeetCode 53/152 Maximum Subarray/Maximum Product Subarray(动态规划)

LeetCode 53/152 Maximum Subarray/Maximum Product Subarray(动态规划)

花事未了_喃喃语 436

autodl云服务器使用中的conda源报错

通过find path -name filename找到.condarc文件,在autodl服务器中,一般在/root/.condarc。为了利用jypyter打开conda虚拟环境运行,需要下载ipykenel。这个报错是源的问题,在通过源获取包元数据时出现了HTTP 429错误。通过conda换源解决。修改.condarc文件,命令: vim .condarc。但这样还不能解决问题,得把报错中有问题的源注释掉。通过以下命令添加清华镜像源。把报错的链接源注释如下。

Ty_kilig的博客 1584

LTspice仿真实战:共模电感差模/共模插入损耗测试全流程(附WE 744821201参数)

本文以Würth Elektronik 744821201型号共模电感为例,详细介绍了在LTspice中搭建差模与共模插入损耗测试电路的全流程。通过从规格书提取关键参数、构建SPICE模型,到执行仿真并与官方数据对比,旨在帮助硬件工程师在PCB设计前期精准预测EMC性能,弥合器件选型与系统设计之间的鸿沟。

info6的博客 761

leetcode刷题记录-53. Maximum Subarray

leetcode刷题记录-53. Maximum Subarray 题目要求:Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], ...

zhaizhan501的博客 292

Python 爬虫教程:美食网站 + 社交平台餐馆评论抓取(评分提取 + 情感分析)

在当今数字化时代,数据无处不在,而爬虫技术则是获取数据的强大工具之一。无论是市场调研、学术研究还是个人兴趣探索,爬虫都能帮助我们从海量的网络信息中提取有价值的数据。本文将通过一个实战案例,带领大家学习如何使用 Python 爬虫技术抓取美食网站和社交平台上的餐馆评论,并进行评分提取与情感分析。通过这个项目,你将掌握爬虫的基本原理、数据解析、以及如何结合情感分析技术来挖掘数据背后的洞察。

u014481728的博客 311

【LeetCode】Maximum Subarray 和 Maximum Product Subarray

53. Maximum Subarray 152. Maximum Product Subarray

夏至亦韵 296

基于YOLOv11的手写数字与符号识别系统设计与实现

手写数字与符号识别系统需要丰富多样的训练数据才能达到理想的泛化性能。MNIST:包含70,000个手写数字样本(0-9),28x28灰度图像优点:干净整齐,适合初学者缺点:多样性不足,只有数字EMNIST:MNIST的扩展,包含数字、字母(大小写)和特殊符号样本量:814,255张图像覆盖62个类别(10数字+26小写字母+26大写字母)HASYv2:专门的手写符号数据集包含369个不同符号类别约168,000个标注实例包括数学符号、箭头、括号等:英文手写文本数据库。

YOLO 291

lammps构建高熵合金模型+结构优化初步筛选能量最小的结构

lammps构建高熵合金模型+结构优化初步筛选能量最小的结构

qq_45834835的博客 2604

leetcode -- Maximum Subarray -- 经典问题常考

https://leetcode.com/problems/maximum-subarray/参考http://fisherlei.blogspot.hk/2012/12/leetcode-maximum-subarray.html思路1:假设A(0, i)区间存在k,使得[k, i]区间是以i结尾区间的最大值, 定义为Max[i], 在这里,当求取Max[i+1]时,Max[i+1] = Ma

xyqzki的专栏 1091

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Leetcode 53. Maximum Subarray 最大子序和

Leetcode 53. Maximum Subarray 最大子序和 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/maximum-subarray/ 题目描述 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],...

CoderWangSon 306

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

[算法分析与设计] leetcode 每周一题: 053. Maximum Subarray

题目链接:  53. Maximum Subarray 题目大意: 给定整型数组 A, 找出 A 的(连续)子数组的和的最大值 ; 例如: 给定数组 [-2,1,-3,4,-1,2,1,-5,4], 则输出应为: 6 ; ( 对应子数组 [4,-1,2,1] ) 解题过程: (注: 因为这种类型的题目以前不知道什么时候做过, 所以以下尽量回忆一下当时的思

sinat_37893507的博客 258

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode 152. Maximum Product Subarray(动态规划)

题目来源:https://leetcode.com/problems/maximum-product-subarray/ 问题描述 152.Maximum Product Subarray Medium Given an integer arraynums, find the contiguous subarray within an array (containing at leas...

da_kao_la的博客 308

记录Leetcode第九天(53. Maximum Subarray)

记录Leetcode第九天(53. Maximum Subarray) 1. 题目 Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 2. 例子 Input: [-2,1,-...

qq_42472150的博客 174

初刷leetCode--数组系列--Maximum Subarray(连续最大子序列)

前言 今天正式进入到leetCode的刷题中,先根据数据结构划分,从与数组且Easy的问题开始。对于选题,我们没必要每一题都做,可以先阅读题目,觉得思路清晰简单的就可以直接跳过,例如开始的 Two Sum, Remove Duplicates from Sorted Array,Remove Element, Search Insert Position思路和实现上都没有什么难度,可以直接跳过...

weixin_41726080的博客 511

Leetcode 152. Maximum Product Subarray

Leetcode 152. Maximum Product Subarray if there’s no zero in the array, then the subarray with maximum product must start with the first element or end with the last element. 例子: 关于0: [0, 100, 0] return 100; 关于负号: [-1, 2, 3] [1, 2, -3] [-1, 2, -3] class So

weixin_43876597的博客 161

Leetcode 3277. Maximum XOR Score Subarray Queries

Leetcode 3277. Maximum XOR Score Subarray Queries

codename_cys的博客 1120

LeetCode Maximum Subarray

LeetCode Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous su

Coding for Dreams 2276

Maximum Subarray -- LeetCode

原题链接: http://oj.leetcode.com/problems/maximum-subarray/  这是一道非常经典的动态规划的题目,用到的思路我们在别的动态规划题目中也很常用,以后我们称为”局部最优和全局最优解法“。 基本思路是这样的,在每一步,我们维护两个变量,一个是全局最优,就是到当前元素为止最优的解是,一个是局部最优,就是必须包含当前元素的最优的解。接下来说说动态规划的递

Code Ganker 2万+

LeetCode 523. Continuous Subarray sum和 525. Contiguous Array和560. Subarray Sum Equals K

523. Continuous Subarray Sum Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multi

老夫编代码就是一把梭 714

[LeetCode] 644. Maximum Average Subarray II 二分法求平均值

644.Maximum Average Subarray II(题目链接) Hard Given an array consisting ofnintegers, find the contiguous subarray whoselength is greater than or equal tokthat has the maximum average value. And you need to output the maximum average value. Example 1:...

热爱编程,热爱技术 435

LeetCode—53.最大子序和(Maximum Subarray)——分析及代码(C++)

LeetCode—53.最大子序和[Maximum Subarray]——分析及代码[C++]一、题目二、分析及代码1. 动态规划(1)思路(2)代码(简洁)(3)代码(高效)(4)结果三、其他 一、题目 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续...

江南土豆的博客 349

leetcode python3 简单题53. Maximum Subarray

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and retur...

从此做一个俗人 330

【Leetcode】Maximum Product Subarray

题目链接:https://leetcode.com/problems/maximum-product-subarray/ 题目: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, give

业精于勤,荒于嬉 3314
上一篇: leetcode题记:Count and Say
下一篇: leetcode题记:Length of Last Word
your_blue_sky
博客等级 码龄17年 20粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值