【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)的更多相关文章

  1. [LeetCode] 673. 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. Week 12 - 673.Number of Longest Increasing Subsequence

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

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

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

  5. 673. Number of Longest Increasing Subsequence

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

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

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

  7. 【LeetCode】300. Longest Increasing Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. [LeetCode] 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. C# CheckBoxList-DropDownList回显、筛选回显

    <asp:CheckBoxList ID="ddlType" runat="server" RepeatColumns="10" Re ...

  2. Java 堆、栈、队列(遇见再更新)

    目录 Java 栈.队列 栈 常用方法 案例 队列 Java 栈.队列 栈 常用方法 boolean empty() 测试堆栈是否为空 Object peek() 查看堆栈顶部的对象 Object p ...

  3. Oracle 表结构管理

    表其实是数据的'容器'.oracle有几种类型的表: 普通表(ordinary table)又叫堆组织表. 聚簇表(clustered table) 分区表(partition table) 外部表( ...

  4. 会话-cookie

    package com.hopetesting.cookie;import javax.servlet.ServletException;import javax.servlet.annotation ...

  5. VectorCAST软件自动化测试方案

    VectorCAST 是主要用于对C/C++/Ada程序进行软件自动化测试,并能够在Windows和Linux等多种开发环境下运行.其主要功能包含自动化的单元测试.集 成测试.覆盖率分析.回归测试.代 ...

  6. 常用windows命令和Dos命令

    Windows常用快捷键 Ctrl + C :复制 Ctrl + V :粘贴 Ctrl + X :剪切 Ctrl + A :全选 Ctrl + Z :撤销(做错了后退一步) Ctrl + Y :向前一 ...

  7. 加密解密、食谱、新冠序列,各种有趣的开源项目Github上都有

    Github上是我们程序员学习开源代码.提升编程技巧的好地方.好学校,但是除了学习,小伙伴们有没有发现过Github上一些特别有意思的项目呢? 今天TJ君就来和大家分享几个自认为特别有趣的开源项目: ...

  8. Identity Server 4 从入门到落地(十一)—— Docker部署

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  9. react原理分析--this.state修改引起的重新渲染

    整理向,非原创,目的是整理出浅显易懂的方向性说明. 比如现有 this.state={name:"小明",age:18} 我们说修改组件的状态要用this.setState()来实 ...

  10. Fiddler抓包ios设备

    Fiddler绝对称得上是"抓包神器", Fiddler不但能截获各种浏览器发出的HTTP请求, 也可以截获各种智能手机发出的HTTP/HTTPS请求. Fiddler能捕获ISO ...