原题链接在这里: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 SubsequenceLongest Continuous Increasing Subsequence 的进阶题.

跟上Minimum Window Subsequence.

LeetCode Number of Longest Increasing Subsequence的更多相关文章

  1. [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  2. [LeetCode] 673. Number of Longest Increasing Subsequence 最长递增序列的个数

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...

  5. LeetCode 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  6. [Swift]LeetCode673. 最长递增子序列的个数 | Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  7. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...

  8. 673. Number of Longest Increasing Subsequence最长递增子序列的数量

    [抄题]: Given an unsorted array of integers, find the number of longest increasing subsequence. Exampl ...

  9. Week 12 - 673.Number of Longest Increasing Subsequence

    Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...

随机推荐

  1. php分类树

    class Category{ public $sonName; public $parentName; public function __consturt($id,$parent_id){ $th ...

  2. 【鸟哥的Linux私房菜】笔记3

    正确地开机 最好不要使用root账号登陆!GNOME图形界面 View items as a list X WindowShell 文本交互界面bash是Shell的名称,Linux的默认壳程序就是b ...

  3. HDU 4783 Clumsy Algorithm

    题意不提. 我们可以发现,可以将最终序列分为对于第i个位置i-pi>=0与i-pi<0种两个子序列.且如果f[n]==g[n],则有两个子序列都递增. 原因是f[n]表示1-n这个排列的逆 ...

  4. get_called_class--后期静态绑定("Late Static Binding")类的名称

    get_called_class--后期静态绑定("Late Static Binding")类的名称 string get_called_class ( void ) 获取静态方 ...

  5. 基于Visual c++ 2012的php扩展开发 - 环境搭建

    软件准备 Apache2.4 php-5.6.20-Win32-VC11-x86 php-5.6.20-src mysql-5.5.45-win32 vcredist_x86.exe vs2012旗舰 ...

  6. Java Interface接口

    Java 中接口概念 接口可以理解为一种特殊的 类,由 全局常量 和 公共的抽象方法 所组成. 类是一种具体实现体,而接口定义了某一批类所需要遵循的规范,接口不关心这些类的内部数据, 也不关心这些类里 ...

  7. Luogu-4022 [CTSC2012]熟悉的文章

    广义后缀自动机+DP 对于作文库建出广义后缀自动机,广义自动机就是在每次添加一个字符串之前把\(last=0\),然后正常添加就好了 对于每个询问串,预处理出每个位置\(i\)能向前匹配的最长长度\( ...

  8. PHP(Zend Studio)入门视频

    视频地址: http://www.ev-get.com/article/2014/5/9/20962.html (去掉地址中的减号-:可以看视频) Zend Studio教学视频之Zend Studi ...

  9. CentOS 7(64位) 下Docker的安装

    系统要求是64位,内核版本至少3.10. 首先添加yum软件源: 之后更新yum软件源缓存,并安装docker-engine 查看docker 版本: Cannot connect to the Do ...

  10. libvirt/qemu特性之numa

    博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! Numa 查看主机node情况 使用virsh命令查看 virsh # c ...