LeetCode300. Longest Increasing Subsequence

【Python】torch_npu Ascend AI处理器核心计算范式与PyTorch的桥接艺术:深入探索torch_npu的基石要真正理解,我们必须首先超越其作为PyTorch后端库的表面认知,深入其所服务的硬件——华为昇腾(Ascend)AI处理器的核心计算范式。这不仅仅是关于如何在NPU上运行PyTorch模型,更是关于NPU如何设计其计算,以及如何巧妙地将这种硬件特性映射到PyTorch的抽象层之上。1. 昇腾AI处理器的达芬奇架构:极致算力的源泉昇腾AI处理器并非传统的CPU或GPU。它采用了华为独创的达芬奇(Da Vinci)架 阅读详情
题目链接:

https://leetcode.com/problems/longest-increasing-subsequence/

题目描述:

求最长增长子序列包含数字个数。

Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4.

思路:

方法1:
时间复杂度O(n^2)。
dp[i]记录以nums[i]为结尾的最长增长子序列长度。
序列长度由1扩展到n。

如果最长增长子序列以nums[i]结尾,那么在nums[0..i-1]中所有比nums[i]小的数都可以作为倒数第二个数。在这么多倒数第二个数的选择中,以哪个数结尾的最长增长子序列更大,就选那个数作为倒数第二个数。
所以有dp[i]=max(dp[i],dp[j]+1);(j < i)

方法2:
时间复杂度O(nlogn)。
数组ends[0..right]。
ends[j]记录长度为j+1时的增长子序列的最小结尾元素。
ritgh代表当前最长增长子序列的长度-1。
初始化时ends[0]=nums[0],right=0。
序列长度由1到n扩展(相当于i由1遍历到n-1,不断新加入元素nums[i],看是否满足条件来判断是否增加最长增长子序列的长度)。每当新加入元素nums[i],就在ends[0..right]数组里面找,看是否有大于等于nums[i]的数,如果有,把最左边的大于或等于nums[i]的数,假设数字为ends[b],把ends[b]更新为nums[i],right不变。但是如果没有,那么证明此时的最长增长子序列长度又可以增加来,ritgh=right+1,ends[right]=nums[i]。
查找ends最左边的大于或等于nums[i]的数,可以用二分查找优化。

代码:
//方法1:

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int len=nums.size();
        if(len==0){
            return 0;
        }
        vector<int> dp(len,1);
        int longest=1;
        for(int i=1;i<len;i++){
            for(int j=0;j<i;j++){
                if(nums[j]<nums[i]){
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            longest=max(longest,dp[i]);
        }
        return longest;
    }
};

22 / 22 test cases passed.

Status: Accepted

Runtime: 116 ms

//方法2:
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int n=nums.size();
        if(n==0){
            return 0;
        }
       vector<int> ends(n);
       ends[0]=nums[0];
       int right=0;
       int low,high,mid;
       int longest=1;
       for(int i=1;i<n;i++){
          low=0;
          high=right;
          while(low<=high){
              mid=(high+low)/2;
              if(nums[i]>ends[mid]){
                 low=mid+1; 
              }
              else{
                  high=mid-1;
              }
          }
          right=max(right,low);
          ends[low]=nums[i];
       }
       return right+1;
    }
};
KNX实战—ETS5_智能开关从零到一的配置全流程 本文详细介绍了使用ETS5软件配置KNX智能开关的完整流程。从创建项目、构建建筑结构,到导入产品数据库、添加并配置开关模块与智能面板,再到设置延时开关、闪烁报警等进阶功能,并深入解析了群组地址的配置与链接方法。最后指导完成项目校验与程序下载,帮助用户实现从零到一的智能灯光基础控制功能配置。 阅读详情

相关推荐

EduCoder 计算机网络实验 IP协议分析

第1关:跟踪抓包 任务描述 相关知识 IPV4数据包 截获数据报 分析截获结果 使用git将本地文件推送到远程仓库永久保存 操作要求 任务描述 本关任务:使用 Ping 命令发送数据报,用 Wireshark 截获数据报,分析 IP 数据报的格式,理解 IPV4 地址的编址方法,加深对 IP 协议的理解。 相关知识 为了完成本关任务,你需要掌握: 截获数据报; 简单分析数据包格式。 IPV4数据包 网际协议第 4 版(Internet Protocol version 4

qq_41975868的博客 1万+

Leetcode 300 Longest Increasing Subsequence

思路一: brute force + memorization。一开始没用memorization,就是单纯用recursion来把所有的lis给求了出来,这样做导致tle。后来加了一个数组来memorization就accepted了。这个思路其实也不难,这边直接上代码: class Solution { public int lengthOfLIS(int[] nums) { int res = 0; int currentRes = 0; in.

GoodJobJasper的博客 310

扩散模型在工业缺陷检测中的应用与优化

扩散模型作为生成式AI的重要分支,通过逐步去噪的逆向过程实现高质量数据生成。其核心U-Net架构结合残差连接和时间步嵌入,特别适合处理工业场景中的细微缺陷检测。在计算机视觉领域,这种技术突破了传统深度学习模型对小样本数据的限制,通过域自适应预训练和小样本微调两阶段框架,显著提升金属表面、纺织品等复杂场景的检测准确率。关键技术实现涉及噪声调度优化、多任务损失设计等工程实践,最终在MVTec AD等工业数据集上达到90%以上的检测精度,为智能制造提供可靠的自动化质检方案。

weixin_30512043的博客 497

LeetCode 300. Longest Increasing Subsequence 题解

本题是动态规划的经典问题,主要考察对状态定义和状态转移方程的理解。动态规划:代码简洁易懂,逻辑清晰,但时间复杂度较高,适用于小规模数据。贪心算法 + 二分查找:时间复杂度较低,适用于大规模数据,但理解起来相对复杂。在实际应用中,对于一般规模的数组,两种方法都可以使用。但对于大规模数组,贪心算法 + 二分查找的方法更为高效。无论使用哪种方法,核心思想都是找到最长的严格递增子序列,这对于解决许多实际问题都有帮助,例如最长递增子序列的应用包括:最长递增子串、最长递减子序列、最长不下降子序列等。

cannonjinx的博客 2105

手把手教你学Simulink--单相逆变器模糊逻辑控制策略仿真

本文介绍了在Simulink中设计单相逆变器模糊逻辑控制策略的完整流程。主要包括:1)构建单相逆变器模型;2)使用FuzzyLogicToolbox设计模糊控制器,定义输入输出变量和模糊规则;3)将控制器集成到Simulink模型;4)添加干扰源和示波器;5)配置仿真参数并分析结果。该方法利用模糊控制处理非线性系统的优势,可实现输出电压的精确跟踪,具有良好的鲁棒性。文中提供了详细的步骤说明和示例代码,强调准确设计隶属函数和规则库对控制性能的关键作用。该仿真设计为电力电子控制系统的开发提供了有效支持。

MHD0815的博客 200

基于YOLO的印章检测及文字识别

针对印章自动化检测与文字识别需求,提出了一种融合改进YOLO模型与OCR技术的端到端解决方案,旨在解决复杂背景干扰、印章残缺变形及环形文字排列等难题

商务合作 / 项目定制 / 学习交流。个人vx:lovely_wml 8219

【LeetCode】300. Longest Increasing Subsequence

300. Longest Increasing Subsequence Description: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanati...

Microstrong 1501

YOLOv11改进策略【Neck】| BiFPN:双向特征金字塔网络-跨尺度连接和加权特征融合

可扩展的高效物体检测BiFPN(加权双向特征金字塔网络)

Limiiiing的博客 5494

LeetCode 300. Longest Increasing Subsequence

分析 难度 中 来源 https://leetcode.com/problems/longest-increasing-subsequence/ 思路 tails is an array storing the smallest tail of all increasing subsequences with length i+1 in tails[i]. For example, say we ...

ov流岚vo的专栏 487

吊打BEVFormer!GaussianBeV实战:用3D高斯开启BEV分割新范式

GaussianBeV是一种基于3D高斯建模的BEV分割新方法,在自动驾驶感知任务中实现了SOTA性能。其核心创新在于将场景元素表示为3D高斯分布,通过均值、协方差和权重精确描述物体位置、形状和置信度,相比传统BEV方法具有更强的空间建模能力。文章详细介绍了GaussianBeV的原理、环境配置、训练推理流程及其在自动驾驶和城市3D重建中的应用,并讨论了优化方向和未来发展趋势。该方法在nuScenes等数据集上表现出色,为BEV分割任务提供了新的解决方案。

m0_71589036的博客 106

深度解析与实战:长短时记忆网络(LSTM)在股票走势预测中的应用(基于Python + PyTorch)

本文全面探讨了LSTM在股票价格预测中的应用,从理论基础到实践操作。文章首先强调了股票市场预测的重要性,并介绍了LSTM网络结构及其在处理时间序列数据中的优势。接着,详细说明了数据预处理的关键步骤,包括数据收集、清洗、特征选择和归一化。文章还讨论了LSTM模型的构建、训练和评估,并提供了Python及PyTorch的实操案例。最后,总结了LSTM在股票预测的成果,并提出了未来研究的方向,如模型优化和特征工程,旨在帮助读者掌握LSTM在股票价格预测中的应用,并探索提升模型性能的方法。

专注于人工智能、软件开发、工控自动化、工厂数字化及智能化等领域,希望和大家共同进步! 2100

300. Longest Increasing Subsequence【力扣】

题意理解 给定一个字符串,问最长的递增子序列有多长,(可以不连续)。 问题分析 用动规,状态量,状态转移方程 状态量 DP[i] 前i个字符(带第i个字符)的递增子序列最长长度 状态转移方程 DP[i] = max( DP[j] ) + 1, 0 &lt;= j &lt;= i; 这次的规律不是i个状态和i-1个状态的关系,而是i个状态和0..i-1个状态的关系。 最终的最大值...

xiexie1357的专栏 201

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode300. Longest Increasing Subsequence-动态规划/二分查找

Leetcode300. Longest Increasing Subsequence-动态规划/二分查找题目思路复杂度动态规划O(n2)O(n^2)O(n2)时间动态规划+二分查找O(nlogn)O(nlogn)O(nlogn)时间DP+二分查找(bisect库) 题目 题目链接 思路 见leetcode题解 复杂度 见leetcode题解 动态规划O(n2)O(n^2)O(n2)时间 class Solution: def lengthOfLIS(self, nums): if

qq_17065591的博客 303

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

老版QQ原生表情gif动态

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

【Leetcode】300. Longest Increasing Subsequence

题目地址: https://leetcode.com/problems/longest-increasing-subsequence/ 题目大意是,给定一个数组nums,返回最长(严格)上升子序列的长度。此题有一个时间复杂度O(n2)O(n^2)O(n2)的动态规划算法,例如,可以令f(i)f(i)f(i)为以a[i]a[i]a[i]为结尾的最长子序列的长度,那么有f(i+1)={max⁡k≤i{...

数学、算法爱好者的博客 262

WINCC7.3破解文件

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

LeetCode 300.Longest Increasing Subsequence【Java】

题目描述 LeetCode 300.Longest Increasing Subsequence AC代码 /* 第一种解法:动态规划 f[i]表示以nums[i]结尾的最长上升子序列的长度。 遍历从[0,i)这一段区间,我们定义j∈[0,i). 如果nums[j]<nums[i]那么符合上升规则,此时上升子序列长度是f[j]+1 如果nums[j]>=...

NayelyA的博客 220

[leetcode]300. Longest Increasing Subsequence

[leetcode]300. Longest Increasing Subsequence Analysis raining outside…—— [每天刷题并不难0.0] Given an unsorted array of integers, find the length of longest increasing subsequence. Explanation: Inspire...

weixin_32135877的博客 244

leetcode 300: Longest Increasing Subsequence

Longest Increasing Subsequence Total Accepted: 53 Total Submissions: 172 Difficulty: Medium Given an unsorted array of integers, find the length of longest increasing subsequence. For

西施豆腐渣 4457

[LeetCode 300] Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,1...

ExcitedZhang的博客 219

LeetCode300——Longest Increasing Subsequence

LeetCode300——Longest Increasing Subsequence 题意: 最长上升子串,LIS问题,经典的动态规划问题。据说有用到二分的方法,这个还没有仔细研究,先说说朴素的方法: 动归方程可以如下描述: dp[i] 表示以 nums[i]结尾的最长上升子串的长度。 为了得到dp[i],我们首先要找出最大的dp[j]   其中 j 的范围是 [0,i-1] 当nu

上善若水 588

【LEETCODE】300-Longest Increasing Subsequence [Python]

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], theref

aliceyangxi1987的博客 2331

Leetcode300. Longest Increasing Subsequence

给定一个无序的整数数组,找到其中最长上升子序列的长度。

magic_jiayu的博客 147

LeetCode 300 Longest Increasing Subsequence

题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing sub...

乌鲁木齐001号程序员 125

leetcode 300. Longest Increasing Subsequence

/nums[0,i]表示从第0个数一直到第i个数(包含第i个数)的子数组,dp[i]表示子数组nums[0,i]中的最长严格递增子序列的长度。//所有的dp[i]都初始化为1,含义是nums[i]这一个数自身一定是一个子序列。//所有的dp[i]都初始化为1,含义是nums[i]这一个数自身一定是一个子序列。j的遍历顺序从小到大或者从大到小都可以,起止范围是[0,i-1]。给定i,需要对对所有0<=j<i的nums[j]逐个考察。起止范围是[0,n-1]。

weixin_41554427的博客 649
上一篇: LeetCode274. H-Index
下一篇: LeetCode198. House Robber
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值