Skip to content

Commit da46a40

Browse files
authored
feat: add ts solution to lc problem: No.2521 (#2750)
No.2521.Distinct Prime Factors of Product of Array
1 parent ac0a028 commit da46a40

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

solution/2500-2599/2521.Distinct Prime Factors of Product of Array/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ nums 中所有元素的乘积是:2 * 4 * 8 * 16 = 1024 = 2<sup>10</sup> 。
5151

5252
对于数组中的每个元素,先对其进行质因数分解,然后将分解出的质因数加入哈希表中。最后返回哈希表的大小即可。
5353

54-
时间复杂度 $O(n\sqrt{m})$,其中 $n$ 和 $m$ 分别是数组的长度和数组中元素的最大值
54+
时间复杂度 $O(n \times \sqrt{m})$,空间复杂度 $O(\frac{m}{\log m})$,其中 $n$ 和 $m$ 分别为数组的长度和数组中元素的最大值
5555

5656
<!-- tabs:start -->
5757

@@ -137,6 +137,28 @@ func distinctPrimeFactors(nums []int) int {
137137
}
138138
```
139139

140+
```ts
141+
function distinctPrimeFactors(nums: number[]): number {
142+
const s: Set<number> = new Set();
143+
for (let n of nums) {
144+
let i = 2;
145+
while (i <= n / i) {
146+
if (n % i === 0) {
147+
s.add(i);
148+
while (n % i === 0) {
149+
n = Math.floor(n / i);
150+
}
151+
}
152+
++i;
153+
}
154+
if (n > 1) {
155+
s.add(n);
156+
}
157+
}
158+
return s.size;
159+
}
160+
```
161+
140162
<!-- tabs:end -->
141163

142164
<!-- end -->

solution/2500-2599/2521.Distinct Prime Factors of Product of Array/README_EN.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ There is 1 distinct prime factor so we return 1.
4646

4747
## Solutions
4848

49-
### Solution 1
49+
### Solution 1: Hash Table + Prime Factorization
50+
51+
For each element in the array, first perform prime factorization on it, and then add the decomposed prime factors to the hash table. Finally, return the size of the hash table.
52+
53+
The time complexity is $O(n \times \sqrt{m})$, and the space complexity is $O(\frac{m}{\log m})$. Where $n$ and $m$ are the length of the array and the maximum value in the array, respectively.
5054

5155
<!-- tabs:start -->
5256

@@ -132,6 +136,28 @@ func distinctPrimeFactors(nums []int) int {
132136
}
133137
```
134138

139+
```ts
140+
function distinctPrimeFactors(nums: number[]): number {
141+
const s: Set<number> = new Set();
142+
for (let n of nums) {
143+
let i = 2;
144+
while (i <= n / i) {
145+
if (n % i === 0) {
146+
s.add(i);
147+
while (n % i === 0) {
148+
n = Math.floor(n / i);
149+
}
150+
}
151+
++i;
152+
}
153+
if (n > 1) {
154+
s.add(n);
155+
}
156+
}
157+
return s.size;
158+
}
159+
```
160+
135161
<!-- tabs:end -->
136162

137163
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function distinctPrimeFactors(nums: number[]): number {
2+
const s: Set<number> = new Set();
3+
for (let n of nums) {
4+
let i = 2;
5+
while (i <= n / i) {
6+
if (n % i === 0) {
7+
s.add(i);
8+
while (n % i === 0) {
9+
n = Math.floor(n / i);
10+
}
11+
}
12+
++i;
13+
}
14+
if (n > 1) {
15+
s.add(n);
16+
}
17+
}
18+
return s.size;
19+
}

solution/2500-2599/2522.Partition String Into Substrings With Values at Most K/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ It can be shown that we cannot partition the string into less than 4 substrings.
6262

6363
## Solutions
6464

65-
### Solution 1
65+
### Solution 1: Memoization Search
66+
67+
We design a function $dfs(i)$ to represent the minimum number of partitions starting from index $i$ of string $s$. The answer is $dfs(0)$.
68+
69+
The calculation process of the function $dfs(i)$ is as follows:
70+
71+
- If $i \geq n$, it means that it has reached the end of the string, return $0$.
72+
- Otherwise, we enumerate all substrings starting from $i$. If the value of the substring is less than or equal to $k$, then we can take the substring as a partition. Then we can get $dfs(j + 1)$, where $j$ is the end index of the substring. We take the minimum value among all possible partitions, add $1$, and that is the value of $dfs(i)$.
73+
74+
Finally, if $dfs(0) = \infty$, it means there is no good partition, return $-1$. Otherwise, return $dfs(0)$.
75+
76+
To avoid repeated calculations, we can use memoization search.
77+
78+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
6679

6780
<!-- tabs:start -->
6881

0 commit comments

Comments
 (0)