LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/
题目:
Given an unsorted array of integers, find the number of longest increasing subsequence.
Example 1:
Input: [1,3,5,4,7]
Output: 2
Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].
Example 2:
Input: [2,2,2,2,2]
Output: 5
Explanation: The length of longest continuous increasing subsequence is 1, and there are 5 subsequences' length is 1, so output 5.
Note: Length of the given array will be not exceed 2000 and the answer is guaranteed to be fit in 32-bit signed int.
题解:
len[i] reqpresents到i的LIS长度. count[i] represents 到i的LIS个数.
Base Case 都是1.
For all j from 0 to i, if nums[j] < nums[i], there is a chance to update longest length ending at i.
If there is an update, then update len[i] with len[j]+1 and frequency = count[j].
If there is no update, but len[j]+1 == len[i] means there is other paths to construct LIS ending at i, thus accumlate frequency.
After iterating all j, if longest LIS got update, then update max length, and its frequency.
If max length stays the same, that means globally there is other LIS with the same length, accumate frequency.
Time Complexity: O(n^2), n = nums.length.
Space: O(n).
AC Java:
class Solution {
public int findNumberOfLIS(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int n = nums.length;
// Longest length ending at i
int [] len = new int[n]; // Frequency of longest length ending at i
int [] count = new int[n];
int max = 1;
int res = 0; for(int i = 0; i<n; i++){
len[i] = 1;
count[i] = 1;
for(int j = 0; j<i; j++){
if(nums[j] < nums[i]){
if(len[j]+1 == len[i]){
// Same longest length ending at i, accumlate frequency
count[i] += count[j];
}else if(len[j]+1 > len[i]){
// There is longer subsequence ending at i, update its longest length and frequency
len[i] = len[j]+1;
count[i] = count[j];
}
}
} if(len[i] > max){
// Globally, this one is longer, update global maximum length and its requency
max = len[i];
res = count[i];
}else if(len[i] == max){
// Globally, this one has the same maximum length, accumlate its frequency to res
res += count[i];
}
} return res;
}
}
是Longest Increasing Subsequence, Longest Continuous Increasing Subsequence 的进阶题.
LeetCode Number of Longest Increasing Subsequence的更多相关文章
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- LeetCode 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- 673. Number of Longest Increasing Subsequence最长递增子序列的数量
[抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...
- Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...
随机推荐
- iOS 统计项目代码行数
最近去面试 对面的"他" 问我其中一个问题 是 "你的项目代码量是多少?" 当时的确有点蒙圈, 我可以从整个项目打包的角度考虑项目大小,我还真没想过到底我的项目 ...
- javascript;Dom相关笔记
document.ondblclick 页面双击事件document.title.charAt(0) 取标题第1个字符串window.alert 弹出消息对话框window.confirm 显示确定 ...
- Linux权限管理 chattr命令、lsattr命令、sudo命令
chattr命令 chattr命令用来修改文件系统的权限属性 chatrr 只有 root 用户可以使用,用来修改文件系统的权限属性,建立凌驾于 rwx 基础权限之上的授权. chatrr 命令格式如 ...
- GP DBA基本操作
1.查看队列情况 SELECT * FROM gp_toolkit.gp_resqueue_status; 如果出现了资源占用大于1.1 e+11SQL,则可能存在不合理执行计划的SQL, 基本跑不出 ...
- SDWebImage第三方库学习
1.基本使用方法 //异步下载并缓存 - (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT; //使用占位图片,当 ...
- iOS_AFNetWorking框架分析
网络 — 你的程序离开了它就不能生存下去!苹果的Foundation framework中的NSURLConnection又非常难以理解, 不过这里有一个可以使用的替代品:AFNetworking.A ...
- Python的format函数
Python的format函数 自python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串. 语法 它通过{}和:来代替%. 请看下面的示例,基本上总 ...
- RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)
. I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...
- Spring之rmi实例演示
环境介绍:本文中服务端客户端使用的都是ssm框架,配置文件分为spring_servlet.xml,spring_service.xml,mybatis.xml 在spring里面使用rmi完成远程调 ...
- 算法总结之 在数组中找到出现次数 > N/K的数
题目1 给定一个整型数组arr, 打印其中出现次数大于一半的数, 如果没有这样的数,打印提示信息 进阶 给定一个整型数组arr, 再给定一个整数K, 打印所有出现次数大于 N/K的数,如果没有这样的 ...