需要排序的最短子数组长度

(六)需要排序的最短子数组长度 【题目】给定一个无序数组arr,求出需要排序的最短子数组的长度 【举例】arr = [1,5,3,4,2,6,7];返回4,只有 [5,3,4,2]需要排序 【要求】时间复杂度O(n),空间复杂度O(1) 【解题思路】 (1)先从右往左遍历,初始化变量noMinIndex = -1,用minNum记录遍历过程中右侧出现过的数的最小值。假设当前数为arr[i],如果arr[i]>minNum,说明如果要整体有序,minNum值必须挪到arr[i]的左边。用noMinIndex记录最左边出现这种情况的位置 阅读详情

题目很简单,求需要排序的最短子数组长度

假设数组为a b c d e f g h i j k l m  n

如果abc是有序的,mn是有序的,至于中间的defghijkl是无序的,我们可以得知,如果是正常升序序列,左边的一定是小于右边的任意数值,右边的一定大于左边的任意数值。

所以我们从后往前遍历,如果某个元素大于右边最小的元素,就标记,一直遍历到最左边

从前往后遍历,如果某个元素小于左边的最大的元素,则标记,一直遍历到最右边

程序如下

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

int a[]={1,5,3,4,2,6,7};
int num=7;
int minpos,maxpos;
int minnum=INT_MAX,maxnum=INT_MIN;
void findArray(int len)
{
 int i;
 for(i=len-1;i>=0;i--)
 {
	 if(a[i]>minnum)

		 minpos=i;
    	 minnum=std::min(a[i],minnum);
	
}
 for(i=0;i<len;i++)
 {
   if(a[i]<maxnum)
	   maxpos=i;
	   maxnum=max(a[i],maxnum);
  

 }

}

int main()
{
	findArray(7);
  return 0;
}


SRA数据库 简介 序列读取存档(sequence Read Archive,SRA)数据是可通过多个云提供商和NCBI服务器获得的,它是最大的可公开获得的高通量测序数据存储库。 SRA接受来自生活各个部门以及宏基因组学和环境调查的数据。 SRA存储原始测序数据和比对信息,以提高可重复性并通过数据分析促进新发现。 NCBI网站储存二代测序原始数据的数据库 SRA官网 从Entrez搜索结果中下载SRA序列 例如查找SRA Entrez中BALB / c小鼠淋巴结组织的RNA-Seq记录 获取搜索结果 SRA se 阅读详情

相关推荐

EM算法求解混合伯努利模型

本文记录了EM算法求解混合伯努利分布的推导,并提供了matlab实验代码。

霁月难逢 彩云易散 6983

求需要排序的最短子数组的长度

题解:方法是从后面往前遍历找出比已遍历的数的最小值大的位置1,再从前面遍历找出比已遍历数的最大值小的位置2,然后位置2减去位置1加上1就是长度。  eg; 1 5 4 2 3   ,位置1=2,位置2=5,所以长度=5-2+1=4 #include using namespace std; int a[100],n; int minLength() { int minNum, maxNum

dawn_after_dark的博客 952

安卓-折叠屏平行视界适配(多厂商兼容与优先级实战)

本文详细解析了安卓折叠屏平行视界适配的现状与挑战,重点探讨了多厂商兼容性策略和优先级冲突的实战解决方案。通过对比Google的Embedding方案和国内厂商的EasyGo标准,提供了检测设备支持方案、处理优先级冲突及双方案共存的最佳实践,帮助开发者高效完成折叠屏适配。

weixin_29190169的博客 180

【每日一题】需要排序的最短子数组长度

“ 给定一个无序数组 arr,求出需要排序的最短子数组长度。 【例如】arr=【1,5,3,4,2,6,7】返回 4,因为只有【5,3,4,2】需要排序。 ” 分析: 子数组一定是连续的,所以整个数组可以分为三部分,只有中间一部分是乱序的。我们需要找到乱序部分的起始位置和终止位置。 首先我们分析一下这三部分的特征:因为有序1 和有序3 不需要排序,那么有序1排序后一定在最前边一段(数据最小的一段),同理有序3排在最后一段(数据最大的一段):有序1 < 乱序2 < 有序3 那么: 有序1 &lt

junxinsiwo的专栏 487

Codesys电子凸轮从入门到应用:手把手教你用MC_CamIn/MC_CamOut实现主从轴同步

本文详细介绍了如何在Codesys平台上使用MC_CamIn和MC_CamOut功能块实现电子凸轮的主从轴同步控制。从环境配置、轴使能到凸轮表创建与动态切换,提供了完整的程序编写指南和调试技巧,帮助工程师快速掌握工业自动化中的高精度运动控制技术。

weixin_26878995的博客 291

手把手教你用YOLOv11-seg-LSCD搞定树脂件瑕疵检测(附数据集与代码)

本文详细介绍了如何使用YOLOv11-seg-LSCD模型进行工业级树脂件瑕疵检测,包括环境配置、数据集构建、模型训练与调优、部署优化等全流程。通过LSCD模块的创新设计,有效提升了气泡、裂纹等微小缺陷的检测精度,适用于各类树脂制品的质量检测场景。

weixin_28677005的博客 427

需要排序的最短子数组的长度

求出需要排序的最短子数组的长度

FanFF的博客 1807

从零开始用飞牛fnas搭建集成DeepSeek-R1的远程AI工作台

从零开始用飞牛fnas搭建集成DeepSeek-R1的远程AI工作台

AI+全栈,AI+应用,实操落地,前沿技术 3844

LeetCode刷题---长度最小的子数组

使用两层for循环,i为起始位置,j为终止位置,每次j都要遍历到数组最后一个下标,并且逐个累加。当sum大于等于target时,比较获取最小的长度,直至循环结束。i指向起始位置,j始终指向终止位置,当sum大于等于target的时候,不断的(while)前进i,使得获取到最短的符合条件的长度。要点:该题属于滑动窗口类型的题目。

weixin_47109902的博客 630

spring boot 集成cas(一) 服务端搭建

简单点就是统一服务认证,多个系统一次登录。俗称单点登录。

慕孑晨的博客 1965

Copula函数实战:如何用MATLAB计算气象干旱与农业干旱的联合概率?

本文提供了一份基于MATLAB的Copula函数实战指南,详细阐述了如何利用该多变量联合分布函数计算气象干旱与农业干旱的联合概率及条件概率。通过从数据预处理、边缘分布拟合到Copula模型选择与检验的完整流程,帮助环境科学与农业气象研究者量化干旱事件的关联风险,为风险评估提供定量依据。

weixin_29213655的博客 444

【简单】需要排序的最短子数组长度-Java

package live.every.day.ProgrammingDesign.CodingInterviewGuide.ArrayAndMatrix; /** * 需要排序的最短子数组长度 * * 【题目】 * 给定一个无序数组arr,求出需要排序的最短子数组长度。 * 例如:arr=[1,5,3,4,2,6,7]返回4,因为只有[5,3,4,2]需要排序。 * * 【难度】 * 简单 * * 【解答】 * 解决这个问题可以做到时间复杂度为O(N)、额外空间复杂度为O(1)。

了解➔熟悉➔掌握➔精通 606

【模型】OpenClaw 接入阿里云永久免费模型方案

Qwen2.5-7B-Instruct 模型为例。

海兰 4496

8.6需要排序的最短子数组长度

题目 给定一个无序数组arr,要使数组排序成递增的,求出需要排序的最短子数组长度。 代码实现 public class GetMinLength { public int getMinLength(int[] arr) { if (arr == null || arr.length < 2) { return 0; } ...

Forat 247

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

【搞定算法】需要排序的最短子数组长度

题目:给定一个无序数组 arr,求出需要排序的最短子数组长度。例如:arr = [1,5,3,4,2,6,7] 返回 4,因为只有 [5,3,4,2] 需要排序。 要求:时间复杂度 O(N)、额外空间复杂度O(1)。 假设数组为 [a b c d e f g h i j k l m n],如果 abc 是有序的,mn是 有序的,至于中间的 defghijkl 是无序的,我们可以得知,如果...

Java学习 1480

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

待排序的最短子数组长度(C++版)

题目: 思路: 分别从 左->右:记录最大元素值 比较最大元素值和当前遍历元素的大小 如果 最大值 > 当前元素 记录当前元素下标, 直至数组遍历完成。  —— index1 和 右->左:记录最小元素值 比较最小元素值和当前遍历元素的大小 如果 最小值 最后返回 (index1 - index2 + 1) 注释:如果原数组是有序的 那么返回0即可。 贴代

decode 607

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

上一篇: 未排序数组中累加和为给定值的最长子数组
下一篇: 二维矩阵(杨氏矩阵)查找
lycommand
博客等级 码龄14年 31粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址