第八周:[Leetcode]300. Longest Increasing Subsequence

300. Longest Increasing Subsequence(Leetcode每日一题-2020.03.14) Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Note: There may be more than one LIS combination, it is only necessary for you to return the length. You... 阅读详情

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], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.
Your algorithm should run in O(n2) complexity.
Follow up: Could you improve it to O(n log n) time complexity?


第一种方法:根据老师上课讲的第一种方法,O(n^2)时间复杂度。


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

第二种方法:使用二分查找不断更新对应路径值中最小的结束节点,也是按照老师上课讲的方法,O(nlogn)时间复杂度


class Solution {
public:
    int binary_search(vector<int> a,int target){
    int l = 0,r = a.size() - 1,mid;
    while(r - l > 1){
        mid = l + ((r - l)>>1);
        if(target > a[mid])
            l = mid;
        else if(target == a[mid])
            return mid;
        else
            r = mid;
    }
    return l;
}

int lengthOfLIS(vector<int>& nums) {
    if(nums.size() == 0)
        return 0;
    vector<int> a;
    a.push_back(nums[0]);
    for(int i = 1;i<nums.size();i++){
        if(nums[i] > a[a.size() - 1])
            a.push_back(nums[i]);
        else if(nums[i] < a[a.size() - 1]){
            int index = binary_search(a, nums[i]);
            if(index == 0 && nums[i] < a[0])
                a[0] = nums[i];
            else if(index < a.size() - 1 && a[index] != nums[i])
                a[++index] = nums[i] < a[index] ? nums[i] : a[index];
        }
    }
    return a.size();
}
};
gnss变形监测基础(四) 监测站的数据采样率指的是监测站在单位时间内收集数据的频率,通常以赫兹(Hz)为单位。在GNSS变形监测中,数据采样率决定了监测系统能够捕捉到的变形动态变化的详细程度和时间分辨率。例如,一个采样率为1Hz的GNSS监测站意味着它每秒钟收集一次位置数据;而一个采样率为10Hz的监测站则每秒钟收集10次位置数据。较高的采样率能够提供更细致的时间序列数据,有助于分析快速变化的动态过程,如地震活动或建筑物的振动响应。然而,较高的采样率也意味着数据量更大,可能需要更多的存储空间和更强大的数据处理能力。 阅读详情

相关推荐

CDH Hue连接Hbase报错TSocket read 0 bytes

问题描述 安装好CDH集群后,在Hue中查看HBase信息时,Hue界面报错如下: Api 错误:TSocket read 0 bytes Hue日志报错: exceptions_renderable ERROR Potential trace: [('/opt/cloudera/parcels/CDH-6.3.2-1.cdh6.3.2.p0.1605554/lib/hue/apps/hbase/src/hbase/api.py', 46, 'query', 'return getattr(self

邢为栋 3497

LeetCode 300. Longest Increasing Subsequence

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 subseque...

Spade_的博客 1万+

OpenClaw(小龙虾) 安装部署教程

OpenClaw(原名Clawdbot)是一个开源的、可本地部署的个人AI助理,它能接入飞书、钉钉等即时通讯工具,帮你处理邮件、管理文件,甚至控制电脑。

qq_40668700的博客 1409

算法4.25

1、 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 incre...

qq_45841239的博客 236

win10 离线安装.Net Framework3.5

起因,安装软件比如3dmax2014等需要.Net Framework3.5支撑,那么需要安装低版本的.Net Framework3.5 闲话少说。 1.下载 NetFx3.cab 链接: https://pan.baidu.com/s/124to7F3BOy4yAlY61OjbDg 提取码: kixu 2.将NetFx3.cab 放入 系统盘的windows目录下(示例 C:\Wi...

m0_37615458的博客 7769

不会ps,你也可以成为ps高手,最强傻瓜式人像一键磨皮美白瘦脸美妆PS插件

比如:美肤是用来去斑点的,如果效果不太理想,就加大百分比,反之就减小。瘦脸如果脸胖的就打开,正常的就关闭。所谓的一键调节,要不只能调节一种效果,要不就是个预设,然后让你自己再调。同时,该软件还加入了贴心的批量处理功能,能同时处理上千张照片,让您喝着茶也能将工作做完。、自动检测照片中的人脸,包括鼻子,嘴巴,耳朵,眼睛和眉毛。、依托强大的脸部识别功能,可以高效的对人物全身皮肤进行智能识别,处理,使肤质平滑、过渡均匀。,这是网上下载的模糊照片,经过简单处理,你可以发现,眼睛变的更亮,皮肤更白,眼袋消失。

心语家园 - www.xinyucn.cc 2909

Longest Increasing Subsequence

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], therefor...

leetcode 解题思路 680

图像处理之边缘检测[微分算子、Canny算子和LOG算子]

LOG算子是在经典算子的基础上发展起来的边缘检测算子,根据信噪比求得检测边缘的最优滤波器。边缘总是以强度突变的形式出现,可以定义为图像局部特性的不连续性,如灰度的突变和纹理结构的突变等。图像的边缘包含了物体形状的重要信息,它不仅在分析图像时大幅度地减少了要处理的信息量,而且还保护了目标的边界结构。对于复杂的图像,采用Roberts算子不能较好地得到图像的边缘,而需要采用更加复杂的3X3的算子比如Prewitt算子。对于离散的图像f(x,y),边缘检测算子是用图像的垂直和水平差分来逼近梯度算子,即。

qq_44111805的博客 9852

300.leetcode-Longest Incresing Subsequence最长递增子序列

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], ther

xiepengcheng11的博客 477

从零开始学习CANoe(三)—— 系统变量的创建和使用

从零开始学习CANoe(三)—— 系统变量的创建和使用

蚂蚁小兵 1万+

如何利用gretna软件计算脑网络全局属性(聚类系数,小世界属性,最短路径),局部属性指标(度,度分布,节点效率),如何理解网络稀疏度

grena工具箱被设计用于fMRI数据的图论网络分析。它是一套MATLAB函数和基于MATLAB的接口,用于常规的fMRI预处理,用于计算和统计分析最常用的网络指标,如小世界参数、效率、度、间性、关联性、层级性、同步性和模块化。......

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 9179

LeetCode300: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,10...

卷卷萌的博客 275

第 10 篇:深度学习的“军火库”——CNN、RNN与Transformer,AI如何看懂世界?

深度学习的革命,不是依赖单一的通用大脑,而是为不同任务打造了专属“军火库”:CNN用“局部感知”看懂图像,RNN用“循环记忆”理解序列,而Transformer则用“全局注意力”开启了AIGC时代。理解这些架构,不是记忆孤立的技术,而是洞察AI从“专才”走向“通才”的进化脉络。

maqh_csdn的博客 792

Longest Increasing Subsequence -- Leetcode

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], ther

elton_xiao的专栏 378

VB摄像头监控

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

精雕软件(JDpaint) 5.5 绿色版

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

Leetcode - Longest Increasing Subsequence

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]

roamer_nuptgczx的专栏 414

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

Leetcode: Longest Increasing Subsequence

 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], th

YMHH的专栏 401

LeetCode之300—-Longest Increasing Subsequence

题目: 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, ...

defs6497的博客 163

leetcode300---Longest Increasing Subsequence(最长递增子序列)

问题描述: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], therefor

爱橙子的OK绷的专栏 1669

第十周:[Leetcode]486. Predict the Winner

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a

line02的专栏 496

第十六周:[Sicily]1321. Robot

Description Karell Incorporated has designed a new exploration robot that has the ability to explore new terrains, this new robot can move in all kinds of terrain, it only needs more fuel to move in r

line02的专栏 462

第五周:[Leetcode]55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you

line02的专栏 422

第八周:[Leetcode]5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: “babad” Output: “bab” Note: “aba” is also a valid answer. Exampl

line02的专栏 376

第七周:[hihocoder]#1050 : 树中的最长路

时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一棵二叉树!还可以拼凑成一棵多叉树——好吧,其实就是更为平常的树而已。 但是不管怎么说,小Ho喜爱的玩具又升级换代了,于是他更加爱不释手(其实说起来小球和木棍有什么好玩的是吧= =)。小Ho手中

line02的专栏 376
上一篇: 第八周:[Leetcode]516. Longest Palindromic Subsequence
下一篇: 第九周:[Leetcode]93. Restore IP Addresses
line02
博客等级 码龄15年 3粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值