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.

Approach #1: C++. [DFS]

class Solution {
public:
int findNumberOfLIS(vector<int>& nums) {
int n = nums.size();
if (n == 0) return 0; c_ = vector<int>(n, 0);
l_ = vector<int>(n, 0); int max_len = 0;
for (int i = 0; i < n; ++i)
max_len = max(max_len, len(nums, i)); int ans = 0;
for (int i = 0; i < n; ++i)
if (len(nums, i) == max_len)
ans += count(nums, i); return ans;
} private:
vector<int> c_;
vector<int> l_; // find the total number of increasing subsequence from i to n of the index.
int count(const vector<int>& nums, int n) {
if (n == 0) return 1;
if (c_[n] > 0) return c_[n]; int total_count = 0;
int l = len(nums, n); // find the number of increasing subsequence which is short than current subsquence.
for (int i = 0; i < n; ++i)
if (nums[n] > nums[i] && len(nums, i) == l-1)
total_count += count(nums, i); if (total_count == 0)
total_count = 1; return c_[n] = total_count;
} // find the max length of increasing subsequence from i to n of the index.
int len(const vector<int>& nums, int n) {
if (n == 0) return 1;
if (l_[n] > 0) return l_[n]; int max_len = 1; for (int i = 0; i < n; ++i)
if (nums[n] > nums[i])
max_len = max(max_len, len(nums, i) + 1); return l_[n] = max_len;
} };

  

Appraoch #2: Interation. [Java]

class Solution {
public int findNumberOfLIS(int[] nums) {
int n = nums.length;
if (n == 0) return 0; int[] c = new int[n];
int[] l = new int[n]; Arrays.fill(c, 1);
Arrays.fill(l, 1); for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j])
if (l[j] + 1 > l[i]) {
l[i] = l[j] + 1;
c[i] = c[j];
} else if (l[j] + 1 == l[i]){
c[i] += c[j];
}
}
} int max_len = 0;
for (int i = 0; i < n; ++i)
if (l[i] > max_len)
max_len = l[i]; int ans = 0;
for (int i = 0; i < n; ++i) {
if (l[i] == max_len)
ans += c[i];
} return ans;
}
}

  

Analysis:

The idea is to use two arrays l[n] ans c[n] to record the maximum length os Incresing Subsequence ans the coresponding number of there sequence which ends with nums[i], respectively. That is:

l[i]: the lenght of the Longest Increasing Subseuqence which ends with nums[i].

c[i]: the number of the Longest Increasing Subsequence which ends with nums[i].

Then, the result is the sum of each c[i] while its corresponding l[i] is the maximum length.

Reference:

https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/107293/JavaC%2B%2B-Simple-dp-solution-with-explanation

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-673-number-of-longest-increasing-subsequence/

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

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

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

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

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

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

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

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

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

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

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

  6. LeetCode 673. Number of Longest Increasing Subsequence

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

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

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

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

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

  9. LeetCode Number of Longest Increasing Subsequence

    原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...

随机推荐

  1. springmvc中@RequestMapping的使用

    通过RequestMapping注解可以定义不同的处理器映射规则. 1.1 URL路径映射 @RequestMapping(value="/item")或@RequestMappi ...

  2. springmvc使用包装的pojo接收商品信息的查询条件

    1.包装对象定义如下: 定义Items对象,并对其定义set和get方法. public class QueryVo { private Items items; public Items getIt ...

  3. Python高级用法篇——笔记

    1.Python3字典中items()和python2.x中iteritems()的区别 在Python2.x中,items( )用于 返回一个字典的拷贝列表[Returns a copy of th ...

  4. macos安装postman

    安装命令 brew cask install postman brew 是从下载源码解压然后./configure && make install,同时会包含相关依存库.并自动配置 好 ...

  5. myeclipse 中 svn 更新 提交 同步资源库 详细解释下他们的功能

    原理是这样的 svn服务器一般放在公共的服务器上,大家连这个服务器,在MyEclipse上使用svn控件 可以下载svn上的项目至本地,所以很多公司将开发要用到的软件都放在svn上,有同事来只要连上s ...

  6. 使用delphi 开发多层应用(二十四)KbmMW 的消息方式和创建WIB节点

    KbmMW 中支持基于UDP的消息广播,也支持TCP/IP hub/spoke 方式,还有 基于UDP或者TCP/IP 的点对点的消息传输. 1.基于UDP的消息广播

  7. mysql图文安装教程(win7 32位 亲测)

    一.下载mysql:http://www.mysql.com/downloads/ 弹出: 你需要有一个 Oracle Web 帐户,没有的话,注册一个: 勾选许可: 输入搜索条件: 下载MySQL ...

  8. 怎样使用word2013发布csdn博客

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  9. 硬盘坏道检测工具对比(DiskGenius/HdTunePro/MHDD等)

    说到硬盘检测软件,大家肯定会想到MHDD,但是MHDD真的好用?反正我觉得太难用了,只能在DOS下运行,不能在Win系统下运行:最重要的是只支持IDE硬盘模式,现在的主板几乎全部默认都是AHCI模式, ...

  10. 理解Defer、Panic和Recover

    刚开始的时候理解如何使用Defer和Recover有一点怪异,尤其是使用了try/catch块的时候.有一种模式可以在Go中实现和try/catch语句块一样的效果.不过之前你需要先领会Defer.P ...