> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/lis/673.-number-of-longest-increasing-subsequence.md).

# 673. Number of Longest Increasing Subsequence

![](/files/-Mk2LUjK67whaSl7m1Ge)

time: O(n^2)

space:O(n)

```java
class Solution {
    public int findNumberOfLIS(int[] nums) {
        int dp[] = new int[nums.length];
        int times[] = new int[nums.length];
        
        Arrays.fill(dp, 1);
        Arrays.fill(times, 1);
        
        int count = 0;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    if (dp[j]+1 > dp[i]) {
                        dp[i] = dp[j]+1;
                        times[i] = times[j];
                    } else if (dp[j]+1 == dp[i]) {
                        times[i] += times[j];
                    }
                }
            }
        }
        // System.out.println(Arrays.toString(dp));
        // System.out.println(Arrays.toString(times));
        int maxLen = 0;
        int result = 0;
        for (int i = 0; i < dp.length; i++) {
            if (dp[i] > maxLen) {
                // System.out.println("update max to:" + dp[i] + ",result times:" + times[i]);
                maxLen = dp[i];
                result = times[i];
            } else if (dp[i] == maxLen) { // same maxlen showing again
                // System.out.println("dp[i]:" + dp[i] + ",index:" + i + ",add times:" + times[i]);
                result += times[i];
            }
        }
        
        return result;
    }
}

/*
[1,2,4,3,5,4,7,2]
 1 2 3 3 4 4 5 2
 1 1 1 1 2 1 3 1
       x   x.   => find this one
 
 if (dp[j]+1 > dp[i]) {
    dp[i] = max(dp[i], dp[j]+1)
    times[i] = times[j]
 } else if (dp[j]+1 == dp[i])
    times[i] += times[j];
 }

1 3 5 4 7
1 2 3 3 4


2 2 2 2 2
1 1 1 1 1


*/
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/dynamic-programming/lis/673.-number-of-longest-increasing-subsequence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
