leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 56 】2022-02-05 - 673. 最长递增子序列的个数 #66

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

673. 最长递增子序列的个数

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/

前置知识

示例 1:

输入: [1,3,5,4,7] 输出: 2 解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。 示例 2:

输入: [2,2,2,2,2] 输出: 5 解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。 注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。

zzzpppy commented 2 years ago
class Solution {
    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        int[] f = new int[n], g = new int[n];
        int max = 1;
        for (int i = 0; i < n; i++) {
            f[i] = g[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
                    if (f[i] < f[j] + 1) {
                        f[i] = f[j] + 1;
                        g[i] = g[j];
                    } else if (f[i] == f[j] + 1) {
                        g[i] += g[j];
                    }
                }
            }
            max = Math.max(max, f[i]);
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            if (f[i] == max) ans += g[i];
        }
        return ans;
    }
}
moirobinzhang commented 2 years ago

Code:

public class Solution { public int FindNumberOfLIS(int[] nums) { int maxLen = 1; int count = 0; int[][] dp = new int[nums.Length][];

    for (int i = 0; i < nums.Length; i++)
    {
        dp[i] = new int[2]{1, 1};

        for (int j = 0; j < i; j++)
        {
            if (nums[i] > nums[j])
            {
                if (dp[j][0] + 1 > maxLen)
                {
                    count = dp[j][1];
                    maxLen = dp[j][0] + 1;
                }
                else if (dp[j][0] + 1 == maxLen)
                {
                    count += dp[j][1];
                }

                if (dp[j][0] + 1 > dp[i][0])
                {
                    dp[i][0] = dp[j][0] + 1;
                    dp[i][1] = dp[j][1];
                }
                else if (dp[j][0] + 1 == dp[i][0])
                {
                    dp[i][1] += dp[j][1];
                }

            }
        }

        if (maxLen == 1)
            count++;
    }

    return count;
}

}

spacker-343 commented 2 years ago
    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][2];
        dp[0][0] = 1;
        dp[0][1] = 1;
        int maxLength = 1;
        int total = 1;  
        int tempLength;
        int temp;
        for(int i = 1 ; i < n ; i++){
            tempLength = 0;
            temp = 1;
            for(int j = 0 ; j < i ; j++){ 
                if(nums[j] < nums[i]){
                    if(tempLength < dp[j][0]){  
                        tempLength = dp[j][0];
                        temp = dp[j][1];
                    }else if(tempLength == dp[j][0]){
                        temp += dp[j][1];
                    }
                }
            }
            tempLength++;
            dp[i][0] = tempLength;
            dp[i][1] = temp;
            if(maxLength < tempLength){
                maxLength = tempLength;
                total = temp;
            }else if(maxLength == tempLength){
                total += temp;
            }
        }
        return total;
    }
yetfan commented 2 years ago

思路

代码

class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        dp = []
        cnt = []
        n = len(nums)
        maxlen = 0
        res = 0
        for i in range(n):
            dp.append(1)
            cnt.append(1)
            for j in range(i):
                if nums[j] < nums[i]:
                    if dp[j] + 1 > dp[i]:  # 更长的序列
                        dp[i] = dp[j] + 1
                        cnt[i] = cnt[j]
                    elif dp[j] + 1 == dp[i]: # 另一个“暂时最长”序列
                        cnt[i] += cnt[j]
            if dp[i] > maxlen:
                maxlen = dp[i]
                res = cnt[i]
            elif dp[i] == maxlen:
                res += cnt[i]
        return res

复杂度 时间 O(n^2) 空间 O(n)

for123s commented 2 years ago
class Solution {
public:
    int findNumberOfLIS(vector<int> &nums) {
        int n = nums.size(), maxLen = 0, ans = 0;
        vector<int> dp(n), cnt(n);
        for (int i = 0; i < n; ++i) {
            dp[i] = 1;
            cnt[i] = 1;
            for (int j = 0; j < i; ++j) {
                if (nums[i] > nums[j]) {
                    if (dp[j] + 1 > dp[i]) {
                        dp[i] = dp[j] + 1;
                        cnt[i] = cnt[j]; 
                    } else if (dp[j] + 1 == dp[i]) {
                        cnt[i] += cnt[j];
                    }
                }
            }
            if (dp[i] > maxLen) {
                maxLen = dp[i];
                ans = cnt[i]; 
            } else if (dp[i] == maxLen) {
                ans += cnt[i];
            }
        }
        return ans;
    }
};
declan92 commented 2 years ago

思路
在LIS的基础上,要增加两点

  1. 最长子序列长度增加时,个数重新计算;
  2. 有重复长度的最长子序列时,个数增加;
    java code
    class Solution {
    public int findNumberOfLIS(int[] nums) {
        int n = nums.length;
        if (n == 0)
            return 0;
        int[] dp = new int[n];
        int[] count = new int[n];
        Arrays.fill(dp, 1);
        Arrays.fill(count, 1);
        int maxVal = 1;
        int ans = 1;
        for (int i = 1; i < n; i++) {
            for (int j = i - 1; j >= 0; j--) {
                if (nums[i] > nums[j]) {
                    if (dp[j] + 1 > dp[i]) {
                        dp[i] = dp[j] + 1;
                        count[i] = count[j];
                    }else if(dp[j]+1 ==dp[i] ){
                        count[i]+=count[j];
                    }
                }
            }
            if(dp[i]>maxVal){
                maxVal = dp[i];
                ans = count[i];
            }else if(dp[i]==maxVal){
                ans+=count[i];
            }
        }
        return ans;
    }
    }

    时间:O(n^2)
    空间:O(n)

jiaqiliu37 commented 2 years ago
class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        dp_len = [1 for i in range(len(nums))]
        dp_num = [1 for i in range(len(nums))]
        max_len = 1

        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[j] > nums[i]:
                    if dp_len[i] + 1 > dp_len[j]:
                        dp_len[j] += 1
                        dp_num[j] = dp_num[i]
                        max_len = max(max_len, dp_len[j])
                    elif dp_len[i] + 1 == dp_len[j]:
                        dp_num[j] += dp_num[i]

        return sum(dp_num[i] for i in range(len(nums)) if dp_len[i] == max_len)

Time complexity O(n^2) Space complexity O(n)

Alfie100 commented 2 years ago

题目链接: 673. 最长递增子序列的个数 https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/

解题思路

动态规划。

Python 代码

class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        n = len(nums)
        dp = [1] * n    # dp[i]:以nums[i]结尾的最长递增子序列的长度
        cnt = [1] * n   # cnt[i]:以nums[i]结尾的最长递增子序列的个数

        for i in range(n):
            for j in range(i):
                if nums[i] > nums[j]:
                    if dp[i] < dp[j]+1:
                        dp[i] = dp[j]+1
                        cnt[i] = cnt[j]
                    elif dp[i] == dp[j]+1:
                        cnt[i] += cnt[j]

        # print(dp, cnt)
        max_len = max(dp)
        ans = sum(cnt[i] for i in range(n) if dp[i]==max_len)
        return ans

复杂度分析

xuhzyy commented 2 years ago
class Solution(object):
    def findNumberOfLIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        n = len(nums)
        dp = [0] * n
        cn = [0] * n
        ans, maxlen = 0, 0
        for i in range(n):
            dp[i], cn[i] = 1, 1
            for j in range(i):
                if nums[i] > nums[j]:
                    if dp[i] < dp[j] + 1:
                        dp[i] = dp[j] + 1
                        cn[i] = cn[j]
                    elif dp[i] == dp[j] + 1:
                        cn[i] += cn[j]
            if dp[i] > maxlen:
                maxlen = dp[i]
                ans = cn[i]
            elif dp[i] == maxlen:
                ans += cn[i]
        return ans
dahaiyidi commented 2 years ago

Problem

[673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/)

++

给定一个未排序的整数数组 nums返回最长递增子序列的个数

注意 这个数列必须是 严格 递增的。


Note


Complexity


Python

class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        d, cnt = [], []
        for v in nums:
            i = bisect(len(d), lambda i: d[i][-1] >= v)  
            c = 1
            if i > 0:
                k = bisect(len(d[i - 1]), lambda k: d[i - 1][k] < v) # 在递减的序列中寻找第一个<目标值的位置
                c = cnt[i - 1][-1] - cnt[i - 1][k]
            if i == len(d):
                d.append([v])
                cnt.append([0, c])
            else:
                d[i].append(v)
                cnt[i].append(cnt[i][-1] + c)
        return cnt[-1][-1]

def bisect(n: int, f: Callable[[int], bool]) -> int:
    l, r = 0, n
    while l < r:
        mid = (l + r) // 2
        if f(mid):
            r = mid
        else:
            l = mid + 1
    return l

C++

From : https://github.com/dahaiyidi/awsome-leetcode

qiaojunch commented 2 years ago

class Solution: def findNumberOfLIS(self, nums: List[int]) -> int: '''' Define dp_l(i) as the length of the longest increasing subsequence in nums[:i + 1]. Define dp_c(i) as the number of the longest increasing subsequence in nums[:i + 1]. Note: dp_c is also the indegree of nums[i] Then we'll get the recursion below: dp_l(i) = max(dp_l(j) + 1, dp_l(i)) for j in range(i) dp_c(i) += dp_c(j) if dp_l(i) == dp_l(j) + 1

    '''
    if not nums:
        return 0
    n = len(nums)
    dp_l = [1] * n
    dp_c = [1] * n
    for i, num in enumerate(nums):
        for j in range(i):
            if nums[i] <= nums[j]:   
                continue
            if dp_l[j] + 1 > dp_l[i]:
                dp_l[i] = dp_l[j] + 1
                dp_c[i] = dp_c[j]
            elif dp_l[j] + 1 == dp_l[i]:
                dp_c[i] += dp_c[j]
    max_length = max(x for x in dp_l)
    count = 0
    for l, c in zip(dp_l, dp_c):
        if l == max_length:
            count += c

    print("dpl ", dp_l) 
    print("dpc ", dp_c)

    return count