【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
标签(空格分隔): LeetCode
题目地址: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.
题目大意
这个题是最长递增子序列的变种。求最长的子序列有多少个。
解题方法
首先肯定还是使用dp去求。不过,我们得对dp的数组进行改进,我们在每个位置记录当前的LIS和能得到该LIS长度的子序列数目。在对每个位置进行计算的时候,我们都要找到该位置的LIS长度,并对能得到该长度的子序列的个数进行求和。
最后,我们需要对所有位置等于LIS长度的进行统计。
代码:
class Solution(object):
def findNumberOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp, longest = [[1, 1] for _ in range(len(nums))], 1
for i in range(1, len(nums)):
curr_longest, count = 1, 0
for j in range(i):
if nums[j] < nums[i]:
curr_longest = max(curr_longest, dp[j][0] + 1)
for j in range(i):
if dp[j][0] == curr_longest - 1 and nums[j] < nums[i]:
count += dp[j][1]
dp[i] = [curr_longest, max(count, dp[i][1])]
longest = max(longest, curr_longest)
return sum([item[1] for item in dp if item[0] == longest])
日期
2018 年 4 月 4 日 ———— 清明时节雪纷纷~~下雪了,惊不惊喜,意不意外?
【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)的更多相关文章
- [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
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- Week 12 - 673.Number of Longest Increasing Subsequence
Week 12 - 673.Number of Longest Increasing Subsequence Given an unsorted array of integers, find the ...
- 【LeetCode】673. Number of Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...
- 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 ...
- 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- LeetCode Number of Longest Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Give ...
随机推荐
- Linux内存管理和寻址详解
1.概念 内存管理模式 段式:内存分为了多段,每段都是连续的内存,不同的段对应不用的用途.每个段的大小都不是统一的,会导致内存碎片和内存交换效率低的问题. 页式:内存划分为多个内存页进行管理,如在 L ...
- MapReduce07 Join多种应用
目录 1 Join多种应用 1.1 Reduce Join 1.2 Reduce Join实例实操 需求 需求分析 Map数据处理 Reduce端合并(数据倾斜) 代码实现 JoinBean类 Joi ...
- 大数据学习day36-----flume02--------1.avro source和kafka source 2. 拦截器(Interceptor) 3. channel详解 4 sink 5 slector(选择器)6 sink processor
1.avro source和kafka source 1.1 avro source avro source是通过监听一个网络端口来收数据,而且接受的数据必须是使用avro序列化框架序列化后的数据.a ...
- 源码分析-Consumer
消息消费概述 消息消费以组的模式开展,一个消费组内可以包含多个消费者,每一个消费者组可订阅多个主题,消费组之间有集群模式和广播模式两种消费模式. 集群模式,主题下的同一条消息只允许被其中一个消费者消费 ...
- D3-更改x轴的标签
记录,上代码
- oracle 锁查询
--v$lock中 id1 在锁模式是 TX 时保存的是 实物id 的前2段SELECT * FROM (SELECT s.SID, TRUNC(id1 / power(2, 16)) rbs, bi ...
- What happens when more restrictive access is given to a derived class method in C++?
We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive acc ...
- weak和拷贝
weak/拷贝 1. weak 只要没有strong指针指向对象,该对象就会被销毁 2. 拷贝 NSString和block用copy copy语法的作用 产生一个副本 修改了副本(源对象)并不会影响 ...
- Tomcat(2):配置Tomcat
1,打开IDEA创建一个项目 2,配置Tomcat服务器 3,运行 5,成功 t t
- 使用$.post方式来实现页面的局部刷新功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...