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 ...
随机推荐
- 016_笼统概述MapReduce执行流程结合wordcount程序
数据传输<key,value> File--> <key,value> -->map(key,value) --> mapResult<k ...
- springboot-数据库
Spring-data-jpa jpa定义了一系列持久化的标准,比如hibernate就实现了这一标准. Springboot 的jpa就是hibernate的整合. 在pom文件中增加配置: < ...
- Redis之数据存储结构
今天去中关村软件园面试,被问到:你做项目用到的Redis处理数据用的什么结构?顿时石化,”用到的结构,不就是key-value嘛,还有什么结构?“.面试官说:“平时除了工作,要加强学习,下面的面试我觉 ...
- 转移灶,原发灶,cfDNA的外显子测序得到的突变点的关系
文章名称:Exome Sequencing of Cell-Free DNA from Metastatic Cancer Patients IdentifiesClinically Actionab ...
- Android LCD(一):LCD基本原理【转】
本文转载自:http://blog.csdn.net/longxiaowu/article/details/24787597 关键词:Android LCD TFT 液晶 偏光片 彩色滤光片 背光 ...
- 【转载】关于C++中cin的几点说明性总结
转载地址:http://www.07net01.com/program/289153.html 学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结: 1.cin 2.cin.get() 3.ci ...
- WEB开发中常见漏洞
1.sql注入 SQL注入在黑客领域是一种非常常见的攻击手段,大家应该都听说过很多数据泄漏的案例,其中大部分都是采用SQL注入来获取数据的. SQL注入一般是前端向后台提交数据的时候,在数据中加入SQ ...
- Java properties配置文件工具类
/* * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved */ import org.apache.log4j.Logger; impor ...
- easyui控件使用例子
1.easyui之dataGrid分页加载数据例子 注意:1)分页表格通过url获得数据会提交page,rows两个参数:后台需要获取这两个参数并且由此得到 int pageSize=rows/pag ...
- java基础(6)-集合类2
泛型 泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型,参数化类型,把类型当做参数一样的传递 好处: 1)把运行时期的问题提前到了编译器期间 2)避免了强制类型转换 3 ...