🧑‍🏫
liualexiang
  • Introduction
  • Azure
    • AKS Basic
    • AKS Spark
    • AZ ACR SYNC
    • Azure CMI SDWAN
    • Azure LB DeepDive
      • Azure LB DeepDive
    • Azure Service Principal Basic
    • Azure Internal VM Network Connectivity
      • Azure Internal VM Network Connectivity
    • Azure Cli Build
    • Azure Vm Memory Monitor
  • Blockchain
    • BTC
  • CRISPR
    • 使用Parallel_Cluster提升CRISPA效率
  • OpenSource
    • ElasticSearch
      • ES Get Started
      • ES Search Query
      • Kibana 可视化
      • Logstash配置
    • Ansible 基础
    • Infra As Code
      • Pulumi Get Started
      • Terraform Basic
    • ZooKeeper 基础
    • RPC与REST
    • 使用Python申请大量内存测试
    • 使用TPC_DS产生压测数据
    • Superset
      • Superset部署手册
    • 代码扫描
    • Git
      • Git Basic
      • Github Action Basic
      • Gitlab与AzureAD集成
      • Gitbook 基础教程
    • K8S
      • enter_node
      • K8s X509 Client Cert
      • K8s Basic
      • K8s Oidc
      • Docker 基础
      • helm基础
      • K8S_Secrets管理
      • 深入了解K8S
      • 混沌工程
      • Istio
      • 生态
      • CRD开发
      • k8s网络
    • Cloud_Custodian
    • Jenkins Basic
    • Nginx
    • ETCD
    • 正则
    • VictoriaMetrics
    • Kafka
  • MySQL
    • MySQL 调优
  • Linux
    • SSH Tunnel 上网
    • 内存管理
    • 在Linux系统中通过LUKS加密磁盘
    • 量子计算 Basic
    • IO多路复用
    • Iptables
    • tmux和screen
    • Systemd
    • OS 基础
    • jq基础
    • yum
    • neovim
  • Web
    • Html Css
    • Web部署
    • 缓存
  • Programming
    • 算法
      • 返回list中最大生序子序列长度
    • Python技巧
      • Python的语法糖
      • Python常用装饰器
      • AsyncIO基础
      • 自动化测试pytest
      • python中的下划线
      • 面向对象
      • Python的坑
      • Python配置文件管理
      • HTTP Stream Response
      • Python项目管理
    • 设计模式
      • 设计模式
      • 面向对象的思想
      • 编程概念
    • Go
      • Go 基础
      • Go常用功能
      • 结构体入门
    • 前端
    • Vue
    • NodeJS
  • Math
    • 多项式插值法
  • Security
    • HTTP常见攻击
    • 加密与签名
    • RSA
    • ECDSA
  • Solidity
    • Solidity基础
    • Blockchain Testnet Faucet
  • Tools
    • 视频处理ffmpeg
    • IDE配置
    • iTerm2美化
    • 密码管理
    • FRP配置
    • 工具集
由 GitBook 提供支持
在本页
  • 使用迭代遍历来返回
  • 生成100个数字,并观察计算时长
  1. Programming
  2. 算法

返回list中最大生序子序列长度

使用迭代遍历来返回

num=[1,5,2,4,3]

num = [1,5,2]

def L(num, i):

    if i == len(num) -1: # the last element
        return 1
    max_len = 1
    for j in range(i+1, len(num)):
        if num[i] < num[j]:
            max_len = max(max_len, L(num,j) + 1)
    return max_len


def length_of_LIS(num):
    return max(L(num,i) for i in range(len(num)))

print(length_of_LIS(num))

生成100个数字,并观察计算时长

import time,random

def timeit(f):
    """generate a timeit decorator for calculating the function time"""
    def cal(nums):
        s_time = time.time()
        result = f(nums)
        durations = time.time() - s_time
        print(durations)
        return result
    return cal


def generate_list(n):
    """Generates a list of N numbers which is between 100 and 999"""
    return [ num for num in random.choices(range(100,1000), k =n ) ]
nums = generate_list(100)
print(nums)

def L(nums, i):
    """ return the max length of current sub_list beginning from index i"""
    if i == len(nums)-1:
        return 1
    max_length = 1
    for j in range(i+1, len(nums)):
        if nums[j] > nums[i]:
            max_length = max(max_length, L(nums,j)+1)
    return max_length

@timeit
def get_LL(nums):
    """ return the max length of all sub_list"""
    # max_lengths=[]
    # for i in range(0,len(nums)):
    #     max_lengths.append(L(nums,i))
    # return max(max_lengths)
    return max(L(nums,i) for i in range(len(nums)))

print(get_LL(nums))
上一页算法下一页Python技巧

最后更新于3年前