Skip to content

Commit 8e274a8

Browse files
committed
leetcode
1 parent c5cde7d commit 8e274a8

File tree

3 files changed

+421
-0
lines changed

3 files changed

+421
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
4+
/*
5+
6+
Really Slow - I don't like Slow
7+
8+
func findNumberOfLIS(nums []int) int {
9+
cache := make([][]int, len(nums))
10+
for i := range cache {
11+
cache[i] = make([]int, 2)
12+
}
13+
14+
max := 0
15+
num := 0
16+
for i := 0; i < len(nums); i++ {
17+
pair := findSequence(nums, cache, i)
18+
if pair[0]+1 > max {
19+
max = pair[0] + 1
20+
num = pair[1]
21+
} else if pair[0]+1 == max {
22+
num += pair[1]
23+
}
24+
}
25+
return num
26+
}
27+
28+
func findSequence(nums []int, cache [][]int, index int) []int {
29+
if index == len(nums) {
30+
return []int{0, 0}
31+
}
32+
33+
if cache[index][0] != 0 {
34+
return cache[index]
35+
}
36+
37+
max := 1
38+
numLIS := 1
39+
var pair []int
40+
41+
for i := index + 1; i < len(nums); i++ {
42+
if nums[i] > nums[index] {
43+
pair = findSequence(nums, cache, i)
44+
45+
if pair[0]+1 > max {
46+
max = pair[0] + 1
47+
numLIS = pair[1]
48+
} else if pair[0]+1 == max {
49+
numLIS += pair[1]
50+
}
51+
}
52+
}
53+
54+
cache[index] = []int{max, numLIS}
55+
return cache[index]
56+
}
57+
58+
*/
59+
60+
61+
62+
type Pair struct {
63+
first int
64+
second int
65+
}
66+
67+
func findNumberOfLIS(nums []int) int {
68+
n := len(nums)
69+
lisFQ := make([]Pair, n)
70+
lisFQ[0] = Pair{1, 1}
71+
lo := 1
72+
73+
for i := 1; i < n; i++ {
74+
mx := 0
75+
c := 1
76+
77+
for j := 0; j < i; j++ {
78+
if nums[j] < nums[i] {
79+
if lisFQ[j].first > mx {
80+
mx = lisFQ[j].first
81+
c = lisFQ[j].second
82+
} else if lisFQ[j].first == mx {
83+
c += lisFQ[j].second
84+
}
85+
}
86+
}
87+
88+
lisFQ[i] = Pair{mx + 1, c}
89+
90+
if lo < lisFQ[i].first {
91+
lo = lisFQ[i].first
92+
}
93+
}
94+
95+
count := 0
96+
for i := 0; i < n; i++ {
97+
if lisFQ[i].first == lo {
98+
count += lisFQ[i].second
99+
}
100+
}
101+
102+
return count
103+
}
104+

0 commit comments

Comments
 (0)