【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 ... 
随机推荐
- ysoserial-CommonsBeanutils1的shiro无依赖链改造
			ysoserial-CommonsBeanutils1的shiro无依赖链改造 一.CB1利用链分析 此条利用链需要配合Commons-Beanutils组件来进行利用,在shiro中是自带此组件的. ... 
- linux系统中安装MySQL
			linux系统中安装MySQL 检查原来linux系统中安装的版本 rpm -qa | grep mysql 将其卸载掉 以 mysql-libs-5.1.71-1.el6.x86_64 版本为例 r ... 
- 一次线上GC故障解决过程记录
			排查了三四个小时,终于解决了这个GC问题,记录解决过程于此,希望对大家有所帮助.本文假定读者已具备基本的GC常识和JVM调优知识,关于JVM调优工具使用可以查看我在同一分类下的另一篇文章: http: ... 
- 关于C语言中不同类型数据进行计算  有符号和无符号数进行计算
			float是8个有效位, 做个试验: 输出如下: 上面说明了什么: 1, 18/2.2 是除不尽的, 因为是define,所以没有给ratio变量赋值类型,但是从sizeof输出的结果是8,所以系统默 ... 
- Largest Rectangle in Histogram及二维解法
			昨天看岛娘直播解题,看到很经典的一题Largest Rectangle in Histogram 题目地址:https://leetcode.com/problems/largest-rectangl ... 
- spring Profile 为不同环境提供不同的配置支持
			说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ... 
- bugku 杂项 流量分析(cnss)
			bugku 杂项 流量分析(cnss) 此题较为简单 wireshark 追踪第一行tcp流信息 得到如下 GET /stat.htm?id=2724999&r=http%3A%2F%2Fsp ... 
- 【科研工具】流程图软件Visio Pro 2019 详细安装破解教程
			[更新区] 安装教程我下周会在bilibili上传视频,这周事情太多暂时先不弄. [注意] 安装Visio需要和自己的Word版本一样,这里因为我的Word是学校的正版2019(所以学校为什么正版没买 ... 
- 通过DT10获取程序执行过程中的实时覆盖率
			DT10是新一代的动态测试工具,可以长时间跟踪记录目标程序执行情况,获取目标程序动态执行数据,帮助进行难于重现的Bug错误分析,覆盖率检测,性能测试,变量跟踪等等功能. 系统测试覆盖率,通常是用于判断 ... 
- Python pyecharts绘制仪表盘
			一.仪表盘gauge.add方法简介 gauge.add()方法签名 add(name,attr,value, scale_range=none, angle_range=none,**kwargs) ... 
