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 ...
随机推荐
- Django~1
一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有 ...
- 016_笼统概述MapReduce执行流程结合wordcount程序
数据传输<key,value> File--> <key,value> -->map(key,value) --> mapResult<k ...
- 【leetcode刷题笔记】Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- P3794 签到题IV
题目 P3794 签到题IV 来切道水题放松一下吧 做法 或是单调不下降的,\(gcd\)是单调不上升的 \(a_i≤5×10^5\)分成权值不同的块数应该很小,所以随便乱搞就出来了 My compl ...
- Vue.js学习笔记 第四篇 列表渲染
遍历数组和对象 和条件选择一样,循环也和其他语言类似,也尝试着用一个例子解决问题 <!DOCTYPE html> <html> <head> <meta ch ...
- HMM代码实现
按照网上的代码,自己敲了一下,改了一点点,理解加深了一下. 还有训练HMM的EM算法没看懂,下次接着看: 参考连接:http://www.cnblogs.com/hanahimi/p/4011765. ...
- java深入探究15-SpringMVC
测试代码:链接:http://pan.baidu.com/s/1c1QGYIk 密码:q924 回顾spring+struts web.xml配置;struts核心过滤器;spring监听器-> ...
- 使用iView时报"Parsing error: x-invalid-end-tag"错误的解决方案
一. 问题日志 二. 问题原因iView将标签渲染为原生html标签时,由于这些标签是自闭合的,所以有end标签会报错. 三. 解决方案修改配置文件,忽略该项检查: 根目录下 - .eslintrc. ...
- Windows 配置Apache以便在浏览器中运行Python script的CGI模式
打开httpd.conf,找到”#ScriptInterpreterSource Registry “,移除前面的注释# (如果找不到这行,就自己添加进去) 找到“Options Indexes Fo ...
- Android 报错Android - Performing stop of activity that is not resumed
[原文] FROM STACKOVERFLOW: Just giving my 50 cents on the issue. Catching the exception is indeed one ...