|
| 1 | +/* |
| 2 | +
|
| 3 | +-* 673. Number of Longest Increasing Subsequence *- |
| 4 | +
|
| 5 | +Given an integer array nums, return the number of longest increasing subsequences. |
| 6 | +
|
| 7 | +Notice that the sequence has to be strictly increasing. |
| 8 | +
|
| 9 | + |
| 10 | +
|
| 11 | +Example 1: |
| 12 | +
|
| 13 | +Input: nums = [1,3,5,4,7] |
| 14 | +Output: 2 |
| 15 | +Explanation: The two longest increasing subsequences are [1, 3, 4, 7] and [1, 3, 5, 7]. |
| 16 | +Example 2: |
| 17 | +
|
| 18 | +Input: nums = [2,2,2,2,2] |
| 19 | +Output: 5 |
| 20 | +Explanation: The length of the longest increasing subsequence is 1, and there are 5 increasing subsequences of length 1, so output 5. |
| 21 | + |
| 22 | +
|
| 23 | +Constraints: |
| 24 | +
|
| 25 | +1 <= nums.length <= 2000 |
| 26 | +-106 <= nums[i] <= 106 |
| 27 | +
|
| 28 | +
|
| 29 | +*/ |
| 30 | +/* |
| 31 | +
|
| 32 | +
|
| 33 | +class Pair { |
| 34 | + final int first; |
| 35 | + final int second; |
| 36 | +
|
| 37 | + const Pair(this.first, this.second); |
| 38 | +} |
| 39 | +
|
| 40 | +class A { |
| 41 | + int findNumberOfLIS(List<int> nums) { |
| 42 | + final int n = nums.length; |
| 43 | + final List<Pair> lisFQ = List.filled(n, Pair(0, 0)); |
| 44 | + lisFQ[0] = Pair(1, 1); |
| 45 | + int lo = 1; |
| 46 | +
|
| 47 | + for (int i = 1; i < nums.length; i++) { |
| 48 | + int mx = 0; |
| 49 | + int c = 1; |
| 50 | +
|
| 51 | + for (int j = 0; j < i; j++) { |
| 52 | + if (nums[j] < nums[i]) { |
| 53 | + if (lisFQ[j].first > mx) { |
| 54 | + mx = lisFQ[j].first; |
| 55 | + c = lisFQ[j].second; |
| 56 | + } else if (lisFQ[j].first == mx) { |
| 57 | + c += lisFQ[j].second; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +
|
| 62 | + lisFQ[i] = Pair(mx + 1, c); |
| 63 | +
|
| 64 | + if (lo < lisFQ[i].first) { |
| 65 | + lo = lisFQ[i].first; |
| 66 | + } |
| 67 | + } |
| 68 | +
|
| 69 | + int count = 0; |
| 70 | +
|
| 71 | + for (int i = 0; i < nums.length; i++) { |
| 72 | + if (lisFQ[i].first == lo) { |
| 73 | + count += lisFQ[i].second; |
| 74 | + } |
| 75 | + } |
| 76 | +
|
| 77 | + return count; |
| 78 | + } |
| 79 | +} |
| 80 | +*/ |
| 81 | +/* |
| 82 | +// I need to Work On Pair |
| 83 | +
|
| 84 | +class Solution { |
| 85 | + List<int> f(List<int> nums, int i, int prev, List<List<Pair>> dp) { |
| 86 | + if (i >= nums.length) |
| 87 | + return [ |
| 88 | + 0, |
| 89 | + 1 |
| 90 | + ]; // The length of LIS is 0, and there is one such LIS (an empty LIS). |
| 91 | +
|
| 92 | + if (dp[prev + 1][i].first != -1) return dp[prev + 1][i]; |
| 93 | +
|
| 94 | + // 'a' is by taking that value and storing the length of the subsequence in a[0] |
| 95 | + // and storing the frequency of that subsequence in a[1]. |
| 96 | + // 'b' is storing the then length of subsequence in b[0] and freq. of that subsequence in b[1] by not taking that value (means by skipping) ... |
| 97 | +
|
| 98 | + List<int> a = [0, 0], b = [0, 0]; |
| 99 | +
|
| 100 | + if (prev == -1 || nums[i] > nums[prev]) { |
| 101 | + a = f(nums, i + 1, i, dp); |
| 102 | + a[0]++; |
| 103 | + } |
| 104 | +
|
| 105 | + b = f(nums, i + 1, prev, dp); |
| 106 | +
|
| 107 | + // if my length of the longest subsequence by taking ele. and not taking that ele. are equal then we just add up the freq. |
| 108 | + if (a[0] == b[0]) { |
| 109 | + dp[prev + 1][i] = Pair(a[0], a[1] + b[1]); |
| 110 | + } |
| 111 | + // if a has the longest subsequence length, then we take a. |
| 112 | + else if (a[0] > b[0]) { |
| 113 | + dp[prev + 1][i] = Pair(a[0], a[1]); |
| 114 | + } else { |
| 115 | + dp[prev + 1][i] = Pair(b[0], b[1]); |
| 116 | + } |
| 117 | +
|
| 118 | + return dp[prev + 1][i] as List<int>; |
| 119 | + } |
| 120 | +
|
| 121 | + int findNumberOfLIS(List<int> nums) { |
| 122 | + int n = nums.length; |
| 123 | +
|
| 124 | + List<List<Pair>> dp = |
| 125 | + List.generate(n + 1, (index) => List.filled(n, Pair(-1, -1))); |
| 126 | +
|
| 127 | + return f(nums, 0, -1, dp)[1]; |
| 128 | + } |
| 129 | +} |
| 130 | +
|
| 131 | +class Pair { |
| 132 | + int first; |
| 133 | + int second; |
| 134 | +
|
| 135 | + Pair(this.first, this.second); |
| 136 | +} |
| 137 | +*/ |
| 138 | + |
| 139 | +class Solution { |
| 140 | + int findNumberOfLIS(List<int> nums) { |
| 141 | + final List<List<int>> cache = |
| 142 | + List.generate(nums.length, (index) => List<int>.filled(2, 0)); |
| 143 | + int max = 0; |
| 144 | + int number = 0; |
| 145 | + for (int i = 0; i < nums.length; i++) { |
| 146 | + final List<int> pair = findSequence(nums, cache, i); |
| 147 | + if (pair[0] + 1 > max) { |
| 148 | + max = pair[0] + 1; |
| 149 | + number = pair[1]; |
| 150 | + } else if (pair[0] + 1 == max) { |
| 151 | + number += pair[1]; |
| 152 | + } |
| 153 | + } |
| 154 | + return number; |
| 155 | + } |
| 156 | + |
| 157 | + List<int> findSequence(List<int> nums, List<List<int>> cache, int index) { |
| 158 | + if (index == nums.length) return [0, 0]; |
| 159 | + if (cache[index][0] != 0) return List.from(cache[index]); |
| 160 | + |
| 161 | + int max = 1; |
| 162 | + int numLIS = 1; |
| 163 | + List<int> pair = [0, 0]; // [LIS, num_LIS] |
| 164 | + for (int i = index + 1; i < nums.length; i++) { |
| 165 | + if (nums[i] > nums[index]) { |
| 166 | + pair = findSequence(nums, cache, i); |
| 167 | + // pair now has the result of LIS and number of times LIS occurred |
| 168 | + // we need to combine the subproblem values. |
| 169 | + if (pair[0] + 1 > max) { |
| 170 | + max = pair[0] + 1; |
| 171 | + numLIS = pair[1]; |
| 172 | + } else if (pair[0] + 1 == max) { |
| 173 | + numLIS += pair[1]; |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + cache[index] = [max, numLIS]; |
| 179 | + return List.from(cache[index]); |
| 180 | + } |
| 181 | +} |
0 commit comments