当前位置:网站首页>Li Kou 300 longest increasing subsequence dynamic programming
Li Kou 300 longest increasing subsequence dynamic programming
2022-07-24 09:22:00 【Tension √】
Title Description
Give you an array of integers nums , Find the length of the longest strictly increasing subsequence .
Subsequence Is a sequence derived from an array , Delete ( Or do not delete ) Elements in an array without changing the order of the rest . for example ,[3,6,2,7] It's an array [0,3,1,6,2,2,7] The subsequence .
Their thinking
Create a new one dp Array , Initialize to 1;
dp[ i ] It means the first one i A sequence of elements ending , The length of the largest increasing subsequence , So it's initialized to 1;
i You need to compare the previous elements every time , If there is a ratio in front i Small elements , Need to put dp[ i ] to update , Update the equation to ( State transition equation ):
dp[i] = max(dp[i], dp[j] + 1) for j in [0, i)
- New variable max To hold dp Maximum value of array , Finally back to max that will do .
Below is the picture of the boss :










I / O example

Code
// Dynamic programming
class Solution {
public int lengthOfLIS(int[] nums) {
int len = nums.length;
int[] dp = new int[len];
int max = 0;
Arrays.fill(dp,1);
for(int i = 0; i < len; i++){
for(int j = 0; j < i; j++){
if(nums[j] < nums[i]){
dp[i] = Math.max(dp[i],dp[j]+1);
}
}
max = Math.max(dp[i],max);
}
return max;
}
}边栏推荐
- Open source summer interview | learn with problems, Apache dolphin scheduler, Wang Fuzheng
- 【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)
- js定位大全获取节点的兄弟,父级,子级元素含robot实例
- [example of URDF exercise based on ROS] use of four wheeled robot and camera
- [don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?
- Why does TCP shake hands three times instead of two times (positive version)
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- How should tiktok account operate?
- web安全入门-开源防火墙Pfsense安装配置
- Three tips for finding the latest trends on tiktok
猜你喜欢

One click openstack single point mode environment deployment - preliminary construction

Discuz论坛搭建详细过程,一看就懂

Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter

Build practical product help documents to improve user satisfaction

Why does TCP shake hands three times instead of two times (positive version)
![[translation] integration challenges in microservice architecture using grpc and rest](/img/88/53f4c2061b538b3201475ba51449ff.jpg)
[translation] integration challenges in microservice architecture using grpc and rest

gnuplot软件学习笔记

Android system security - 5.3-apk V2 signature introduction

Leetcode94 detailed explanation of middle order traversal of binary tree

TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
随机推荐
What are the 6% annualized products?
[Luogu p5829] [template] mismatch tree (string) (KMP)
Asyncdata cross domain error after nuxt route switching
【汇编语言实战】(二)、编写一程序计算表达式w=v-(x+y+z-51)的值(含代码、过程截图)
DP longest common subsequence detailed version (LCS)
Dorissql syntax Usage Summary
C#/VB. Net: convert word or EXCEL documents to text
Code random notes_ Linked list_ Turn over the linked list in groups of 25K
[don't bother to strengthen learning] video notes (II) 2. Write a small example of Q learning
& 和 &&、| 和 || 的区别
[don't bother with intensive learning] video notes (III) 1. What is SARS?
Xtrabackup realizes full backup and incremental backup of MySQL
[don't bother to strengthen learning] video notes (IV) 1. What is dqn?
Introduction to common ansible modules
科目1-2
How do tiktok merchants bind the accounts of talents?
Leetcode question brushing series -- 174. Dungeon games
DSP development, using CCS software to establish engineering and burning
Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro
代码随想录笔记_链表_25K个一组翻转链表