673.number-of-longest-increasing-subsequence.go 616 B

1234567891011121314151617181920212223242526272829303132
  1. func findNumberOfLIS(nums []int) int {
  2. n, max := len(nums), 1
  3. if n == 0 {
  4. return 0
  5. }
  6. dp := make([]int, n) // The number of LIS end with nums[i]
  7. length := make([]int, n) // The length of LIS ...
  8. dp[0], length[0] = 1, 1
  9. for i := 1; i < n; i++ {
  10. dp[i], length[i] = 1, 1
  11. for j := 0; j < i; j++ {
  12. if nums[j] < nums[i] {
  13. if l := length[j] + 1; l == length[i] {
  14. dp[i] += dp[j]
  15. } else if length[i] < l {
  16. if max < l {
  17. max = l
  18. }
  19. dp[i] = dp[j]
  20. length[i] = l
  21. }
  22. }
  23. }
  24. }
  25. res := 0
  26. for i := range dp {
  27. if length[i] == max {
  28. res += dp[i]
  29. }
  30. }
  31. return res
  32. }