Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
haobinaa
DataStructure-DesignPattern
Repository navigation
Code
Issues
1
(1)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
DataStructure-DesignPattern
/
src
/
main
/
java
/
com
/
haobin
/
leetcode
/
dp
/
LongestSubArray.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
50 lines (47 loc) · 1.41 KB
master
Breadcrumbs
DataStructure-DesignPattern
/
src
/
main
/
java
/
com
/
haobin
/
leetcode
/
dp
/
LongestSubArray.java
Copy path
Top
File metadata and controls
Code
Blame
50 lines (47 loc) · 1.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.haobin.leetcode.dp;
import java.util.Arrays;
/**
* @Author HaoBin
* @Create 2020/2/24 10:56
* @Description: 最长上升子序列
*
* 给定一个无序的整数数组,找到其中最长上升子序列的长度。
*
* 示例:
*
* 输入: [10,9,2,5,3,7,101,18]
* 输出: 4
* 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4。
*
**/
public class LongestSubArray {
/**
* 动态规划解法
* 定义状态: dp[i] 表示以 num[i] 结尾的最长上升子序列长度
* 状态转移:
* 遍历到 nums[i] 时,考虑把索引 i 之前的所有的数都看一遍,只要当前的数 nums[i] 严格大于之前的某个数,
* 那么 nums[i] 就可以接在这个数后面形成一个更长的上升子序列。
* 因此,dp[i] 就等于索引 i 之前严格小于 nums[i] 的状态最大者 +1。
*
*/
public int lengthOfLIS(int[] nums) {
int len = nums.length;
if (len < 2) {
return len;
}
int[] dp = new int[len];
Arrays.fill(dp, 1);
for (int i = 1; i < len; i++) {
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j]+1);
}
}
}
int res = 0;
for (int i = 0; i < len; i++) {
res = Math.max(res, dp[i]);
}
return res;
}
}
You can’t perform that action at this time.