求连续子数组的最大和

【算法拾遗】三种方法求连续子数组的最大和 这是一道考的烂的不能再烂的题目,但是依然有很多公司乐于将这样的题目作为笔试或面试题,足见其经典。 问题是这样的:一个整数数组中的元素有正有负,在该数组中找出一个连续子数组,要求该子数组中各元素的和最大,这个子数组便被称作最大子数组。比如数组{2,4,-7,5,2,-1,2,-4,3}的最大子数组为{5,2,-1,2},最大子数组的和为5+2-1+2=8。 下面按照时间复杂度逐步优化的顺序依次给出这三种算法。 阅读详情

一般考数组的题目,重在挖掘数字时间的规律,只要找到规律就可以写递推关系式,很容易编写基于循环的

或者是动态规划的程序。

 

第一种解法:基于sum更新的解法

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        if (array.size()==0)
            return 0;
        int summax,sum;
        for(int i=0;i<array.size();++i)
        {
            if (sum<= 0)
                sum = array[i];
            else
                sum = sum + array[i];
            if(sum > summax)
                summax = sum;
        }
    return summax;
    }
};

第二种解法:自低向上的递归

使用动态规划

F(i):以array[i]为末尾元素的子数组的和的最大值,子数组的元素的相对位置不变

F(i)=max(F(i-1)+array[i] , array[i])

res:所有子数组的和的最大值

res=max(res,F(i))

如数组[6, -3, -2, 7, -15, 1, 2, 2]

初始状态:

    F(0)=6

    res=6

i=1:

    F(1)=max(F(0)-3,-3)=max(6-3,3)=3

    res=max(F(1),res)=max(3,6)=6

i=2:

    F(2)=max(F(1)-2,-2)=max(3-2,-2)=1

    res=max(F(2),res)=max(1,6)=6

i=3:

    F(3)=max(F(2)+7,7)=max(1+7,7)=8

    res=max(F(2),res)=max(8,6)=8

i=4:

    F(4)=max(F(3)-15,-15)=max(8-15,-15)=-7

    res=max(F(4),res)=max(-7,8)=8

   以此类推  ,,,,,,,,,,,,

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        int res = array[0]; //记录当前所有子数组的和的最大值
        int maxi=array[0];   //包含array[i]的连续数组最大值
        for (int i = 1; i < array.size(); i++) {
            maxi= max(maxi+array[i], array[i]);
            res= max(maxi, res);
        }
        return res;
    }
};

上边的也可以开辟 vector<int>  dp(size,0)

然后 每次计算结果都保存在 dp[i]中,最后返回dp数组中的最大值就好!

第三种解法:分治策略

       分治策略,旨在将原先的大问题不断分解成规模小的问题,比如求最大连续子数组之和,先将原数组以中间界线分为三部分,左边是中界线左边的元素,右边是中界线右边的元素,第三部分是两边都有分布的元素的集合,因为最大连续子数组之和一定是这三部分之其中的一个,所以最后比较这三部分中最大的,就是最大连续子数组之和,也是该函数的返回值。而左边、右边的计算都继续递归分治函数,直到只有一个元素为止,而第三部分是以中间界线分别向左、向右连续相加,找出最大连续子数组之和,这是最关键的一步。

     迭代次数为n=(mid-low+1)+(high-mid),也是将原本O(n^2)算法通过分治降为O(nlogn)的主要原因。


#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int hebing(int A[],int low,int mid,int high)
{
    int sum_left=A[mid];
    int temp=0;
    for(int i=mid;i>=low;i--){
        temp+=A[i];
        if(temp>sum_left)
            sum_left=temp;
    }
    int sum_righ=A[mid];
    temp=0;
    for(int i=mid+1;i<=high;i++){
        temp+=A[i];
        if(temp>sum_righ)
           sum_righ=temp;
    }
   return sum_left+sum_righ;
}

int fenzhi(int A[],int low,int high)
{
    int max = 0;
   if(low==high){
      return A[low];
   }
   else{
       
        int mid=(low+high)/2;
        int left_max=fenzhi(A,low,mid);
        int righ_max=fenzhi(A,mid+1,high);
        int mid_max=hebing(A,low,mid,high);
        if(left_max>righ_max&&left_max>mid_max){
            return left_max;
        }

        else if(righ_max>left_max&&righ_max>mid_max){
            return righ_max;
        }
        else{
            //cout<<mid_max<<endl;
            return mid_max;
        }
       
   }

}

int mysum(vector<int> a)
{
    int size = a.size();
    vector<int> dp(size);
    dp[0] = a[0];
    for(int i=1;i<size;++i)
    {
        if(dp[i-1]<0)
            dp[i] = a[i];
        else
            dp[i] =dp[i-1] +a[i];
    }
  
    return *max_element(dp.begin(),dp.end());
}


int main()
{
    /*
    for(int i=0;i<=4;i++){
        cin>>A[i];
    }*/
    int A[] ={-1,2,-3,4,9,3};

    cout<<fenzhi(A,0,5)<<endl;
    vector<int> vec(A,A+6);
    cout<<mysum(vec)<<endl;
}

   分制算法的代码也比较好理解,只是找三个最大值,同样是基于递归进行分,然后合并的时候找中间的最大值!

 

 

python实现求连续子数组最大和 问题描述:例如:[6,-3,-2,7,-15,1,2,2]求连续子数组中的最大和,此数组中最大和为8,从arr[0]到arr[3]。其余位置都比这个要小。最大连续子数组的特点:(1)第一个不为负数(2)如果前面数的累加加上当前数小于当前数,说明这次累加对总体的结果是无效的;如果前面数的累加加上当前数大于当前数,说明这次累加对结果是具有促进效果的,结果在考虑的范围内。python实现代码如下:def... 阅读详情

相关推荐

JAVA算法:求连续子数组的最大和(详细讲解)

JAVA算法:求连续子数组的最大和(详细讲解) 给定一个一维整型数组,请这个数组中连续子数组的最大和。 这个题目很常见,可以参考我的另外两篇博文 https://blog.youkuaiyun.com/seagal890/article/details/88616329 https://blog.youkuaiyun.com/seagal890/article/details/89062176 这里说明一下K...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 3649

leetcode-42.连续子数组的最大和

1. 问题描述 输入一个整型数组,数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)。 示例1: 输入: nums = [-2,1,-3,4,-1,2,1,-5,4] 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 leetcode 42.连续子数组的最大和 2. 问题分析 1. 动态规划 假设 nums\textit{nums}nums 数组的长度是 nnn,下标从 0 到 n−1。我们用 f(i)f(i)f(i) 代表以第 iii 个数

qq_45069496的博客 1030

c/c++ 算法之求连续子数组的最大和

一个整型数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和,求所有子数组的和的最大值,例如输入的数组为1,-2,3,10,-4,7,2,-5,那么最大的子数组为3,10,-4,7,2,因此输出为该子数组的和18 int MaxSum(int* a,int n) { int MaxSum=a[0]; int TempSum=0; f

hjkhjk007的专栏 2059

【LeetCode】最大子数组和 [M](动态规划)

LeetCode 53:返回一个数组中,子数组最大累加和。数组中存储的是整数。采用动态规划求解。

小七的博客 233

odoo19/18集成reportbro 打印插件 完美支持静默打印

ReportBro设计器 一个用于创建PDF和XLSX报告模板的javascript插件。ReportBro Designer可以轻松集成到您的Web应用程序中。每个人都可以设计和编辑文档模板,并直接在浏览器中预览它们。可以使用服务器上的 (有关完整的文档和演示,请参阅上的ReportBro项目网站。reportBro官方没有提供。测试环境:odoo17社区版。2.产品标签打印设计。

zxltwins的博客 2773

IC设计效率翻倍!Verilog-mode的AUTOWIRE/AUTOINST组合使用技巧

本文详细介绍了在Emacs的verilog-mode插件中,如何组合使用AUTOWIRE与AUTOINST功能,实现Verilog代码的智能自动化连接与维护。通过声明式编程思想,该组合能自动生成模块实例化连接和线网声明,并配合AUTO_TEMPLATE处理复杂映射,从而大幅提升IC顶层设计的效率和可靠性,减少手动连线的错误。

weixin_28826545的博客 320

31. 连续子数组的最大和

题目描述 输入一个整型数组,数组里有正数也有负数。数组中一个或连续的多个整数组成一个子数组。求所有子数组的和的最大值。要求时间负责度为O(n)。 解析 1. 解法一: 看到这个题目,我们首先想到的是求出这个整型数组所有连续子数组的和,长度为n的数组一共有 n(n+2)/2个子数组,因此要求出这些连续子数组的和最快也需要O(n^2)的时间复杂度。但是题目要求的O(n)的时间复杂度,因此上述思路不

蜗牛漫步。。。我要一步一步往上爬。。。 521

在 Windows 上使用 Anaconda 创建 配置 YOLOv8虚拟环境,并更改 pip 源以及以 可编辑模式 安装ultralytics

前往 NVIDIA cuDNN 下载页面,下载与您安装的 CUDA 版本匹配的 cuDNN 版本。:根据您的系统和 CUDA 版本,选择适当的 whl 文件。如下12.7,表示最高可装12.7版本CUDA,与上图中的支持的最高cuda版本一致。,无需重新安装,从而大幅提升开发效率,适用于项目开发、调试或依赖本地未发布的版本时使用,但需确保当前目录包含。:访问 NVIDIA 官方网站,下载与您的显卡和操作系统兼容的 CUDA 版本。首先,确保NVIDIA 显卡支持 CUDA,并安装相应版本的驱动程序。

weixin_44115575的博客 1893

JZ85 连续子数组的最大和(二)

JZ85 连续子数组的最大和(二) 描述 输入一个长度为n的整型数组array,数组中的一个或连续多个整数组成一个子数组,找到一个具有最大和的连续子数组。 1.子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组 2.如果存在多个最大和的连续子数组,那么返回其中长度最长的,该题数据保证这个最长的只存在一个 3.该题定义的子数组的最小长度为1,不存在为空的子数组,即不存在[]是某个数组的子数组 4.返回的数组不计入空间复杂度计算 数据范围: 1&

Juwenzhe_HEBUT的博客 417

PCB布线避坑指南:晶振布局的5个致命错误(附正确示例图)

本文深入解析PCB布线中晶振布局的5个致命错误,包括底层布线、电容布局、地平面处理等关键问题,并提供实战解决方案。通过真实案例和量化数据,帮助硬件开发者避免常见陷阱,提升电路稳定性和性能。特别针对晶振这一高敏感元件,给出布线参数黄金法则和三维避让原则。

weixin_29215391的博客 428

ZYNQ软件复位重启、程序跳转的实现方法(Multiboot)

目录一、增加FSBL的调试信息 一、增加FSBL的调试信息 启动过程的调试信息如下: Xilinx First Stage Boot Loader Release 2016.4 Jan 19 2020-10:35:00 Devcfg driver initialized Silicon Version 3.1 Boot mode is QSPI Single Flash Informat...

weixin_41922484的博客 2万+

程序员编程艺术:第七章、求连续子数组的最大和

 程序员面试题狂想曲:第七章、求子数组的最大和作者:July出处:前奏    我更愿意更多的人和我一样,把本狂想曲系列中的任何一道面试题当做一道简单的编程题或一个实质性的问题来看待,在阅读本狂想曲系列的过程中,希望你能尽量暂时放下所有有关面试的一切包袱,潜心攻克每一道“编程题”,在解决编程题的过程中,好好享受编程带来的无限乐趣,与思考带来的无限激情。第一节、求子数组的最大和3.求子数组的最大和题目描述:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求

结构之法 算法之道 7万+

数字IC设计技能篇之如何在RTL级降低功耗?

以上就是本次降低芯片功耗的部分心得和收获,由于是在芯片设计阶段还没办法后端直接给出power的报告,前端一般使用spyglass和fsdb来进行功耗分析,后续会更新如何使用spyglass在前仿阶段进行power的分析,以及持续更新有关RTL级降低功耗的相关内容。

mzynk的博客 1202

python求连续子数组的最大值_Python语言描述连续子数组的最大和

题目描述HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至...

weixin_39625586的博客 228

【华为OD】华为性格测试(综合测试)高分策略

1、性格测试非常重要,是一次否决制,18 个月有效期,所以态度一定要认真对待,不要随便乱选;2、做题原则:正向原则,积极乐观向上3、时间为 25-30 分钟,会频繁出现重复选项重复出现的题目注意一致性。

笑着的程序员的博客 3万+

TripoSR: Fast 3D Object Reconstruction from a Single Image 论文阅读

模型的主要参数,如变换器的层数、三平面的维度、NeRF模型的具体细节和主要训练配置,在表1中详细说明。TripoSR的算法流程结合了先进的数据处理、模型设计和训练技术,使其能够在不到0.5秒的时间内从单张图像中快速生成详细的3D网格。总体而言,TripoSR是一个强大的3D重建工具,它在速度和质量上都表现出色,但也需要考虑到其对计算资源的需求和在处理极端复杂场景时可能遇到的挑战。:通过精心策划的训练数据集和多样化的数据渲染技术,TripoSR提高了模型的泛化能力和对真实世界图像的适应性。

Christo的博客 1946

python求连续子数组的最大值_Python连续子数组的最大和

关键的问题在于成功分析整个过程。对于连续子数组,可以用一个数值来存储当前和,如果当前和小于零,那么在进行到下一个元素的时候,直接把当前和赋值为下一个元素,如果当前和大于零,则累加下一个元素,同时用一个maxNum存储最大值并随时更新。也可以利用动态规划解决。'''输入一个整型数组,数组里有整数也有负数。数组中一二或连续的多个整数组成一个子数组。求所有子数组的和的最大值。要求时间复杂度为O(n)''...

weixin_39536728的博客 566

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

求连续子数组最大和问题的两种解法_PHP实现

问题描述 求子数组的最大和 题目描述: 输入一个整形数组,数组里有正数也有负数。 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。 求所有子数组的和的最大值。要求时间复杂度为O(n)。 关于连续子数组最大和这个问题,有两种解法,一种是动态规划 解法如下: function getMaxSubSum($arr){ $curSum = $arr[0]; $

会呼吸的鱼 1515

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

求数组中连续子数组的最大和

LeetCode的一道算法题: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4] 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 分析: 我们可以假设数组的第一个元素认为是最大和maxSum,定义数组求和变量int sum = 0,则可以依次向后累加数组中其他元素sum += nums[i],如果sum < 0,则将sum = nums[i],因为sum &l

薄红的博客 2594

深度学习工具包 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")

面试趣题:求不连续子数组最大和的值

作者:小飞 原文地址:http://yishan.cc/blogs/nickledson/archive/2008/12/29/997.aspx 《编程之美》一书上有一道题:给定一个由N个整数元素组成数组a,写一个函数在其中找出连续子数组和的最大值。例如给定数组为{1, -2, 3, 5, -1, 2},则和最大的连续子数组是{3, 5, -1, 2},函数返回值是9。 ...

weixin_30376509的博客 567
上一篇: 算法分析之动态规划三
下一篇: 基于merge的归并排序
runner668
博客等级 码龄11年 161粉丝 137原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值