Skip to content

Commit 589255e

Browse files
committed
feat: add solutions to lc problems: No.1735,2600~2603
* No.1735.Count Ways to Make Array With Product * No.2600.K Items With the Maximum Sum * No.2601.Prime Subtraction Operation * No.2602.Minimum Operations to Make All Array Elements Equal * No.2603.Collect Coins in a Tree
1 parent 9e43d40 commit 589255e

File tree

46 files changed

+2703
-22
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2703
-22
lines changed

solution/0100-0199/0125.Valid Palindrome/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func verify(ch byte) bool {
310310

311311
### **PHP**
312312

313-
```php
313+
```php
314314
class Solution {
315315
/**
316316
* @param String $s

solution/0100-0199/0125.Valid Palindrome/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func verify(ch byte) bool {
298298

299299
### **PHP**
300300

301-
```php
301+
```php
302302
class Solution {
303303
/**
304304
* @param String $s

solution/0100-0199/0188.Best Time to Buy and Sell Stock IV/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer <code>k</code>.</p>
88

9-
<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions.</p>
9+
<p>Find the maximum profit you can achieve. You may complete at most <code>k</code> transactions: i.e. you may buy at most <code>k</code> times and sell at most <code>k</code> times.</p>
1010

1111
<p><strong>Note:</strong> You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).</p>
1212

solution/0200-0299/0211.Design Add and Search Words Data Structure/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ wordDictionary.search(&quot;b..&quot;); // return True
4242
<li><code>1 &lt;= word.length &lt;= 25</code></li>
4343
<li><code>word</code> in <code>addWord</code> consists of lowercase English letters.</li>
4444
<li><code>word</code> in <code>search</code> consist of <code>&#39;.&#39;</code> or lowercase English letters.</li>
45-
<li>There will be at most <code>3</code> dots in <code>word</code> for <code>search</code> queries.</li>
45+
<li>There will be at most <code>2</code> dots in <code>word</code> for <code>search</code> queries.</li>
4646
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>addWord</code> and <code>search</code>.</li>
4747
</ul>
4848

solution/1700-1799/1735.Count Ways to Make Array With Product/README.md

+222
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,237 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```python
53+
N = 10020
54+
MOD = 10**9 + 7
55+
f = [1] * N
56+
g = [1] * N
57+
p = defaultdict(list)
58+
for i in range(1, N):
59+
f[i] = f[i - 1] * i % MOD
60+
g[i] = pow(f[i], MOD - 2, MOD)
61+
x = i
62+
j = 2
63+
while j <= x // j:
64+
if x % j == 0:
65+
cnt = 0
66+
while x % j == 0:
67+
cnt += 1
68+
x //= j
69+
p[i].append(cnt)
70+
j += 1
71+
if x > 1:
72+
p[i].append(1)
5373

74+
75+
def comb(n, k):
76+
return f[n] * g[k] * g[n - k] % MOD
77+
78+
79+
class Solution:
80+
def waysToFillArray(self, queries: List[List[int]]) -> List[int]:
81+
ans = []
82+
for n, k in queries:
83+
t = 1
84+
for x in p[k]:
85+
t = t * comb(x + n - 1, n - 1) % MOD
86+
ans.append(t)
87+
return ans
5488
```
5589

5690
### **Java**
5791

5892
<!-- 这里可写当前语言的特殊实现逻辑 -->
5993

6094
```java
95+
class Solution {
96+
private static final int N = 10020;
97+
private static final int MOD = (int) 1e9 + 7;
98+
private static final long[] F = new long[N];
99+
private static final long[] G = new long[N];
100+
private static final List<Integer>[] P = new List[N];
101+
102+
static {
103+
F[0] = 1;
104+
G[0] = 1;
105+
Arrays.setAll(P, k -> new ArrayList<>());
106+
for (int i = 1; i < N; ++i) {
107+
F[i] = F[i - 1] * i % MOD;
108+
G[i] = qmi(F[i], MOD - 2, MOD);
109+
int x = i;
110+
for (int j = 2; j <= x / j; ++j) {
111+
if (x % j == 0) {
112+
int cnt = 0;
113+
while (x % j == 0) {
114+
++cnt;
115+
x /= j;
116+
}
117+
P[i].add(cnt);
118+
}
119+
}
120+
if (x > 1) {
121+
P[i].add(1);
122+
}
123+
}
124+
}
125+
126+
public static long qmi(long a, long k, long p) {
127+
long res = 1;
128+
while (k != 0) {
129+
if ((k & 1) == 1) {
130+
res = res * a % p;
131+
}
132+
k >>= 1;
133+
a = a * a % p;
134+
}
135+
return res;
136+
}
137+
138+
public static long comb(int n, int k) {
139+
return (F[n] * G[k] % MOD) * G[n - k] % MOD;
140+
}
141+
142+
143+
public int[] waysToFillArray(int[][] queries) {
144+
int m = queries.length;
145+
int[] ans = new int[m];
146+
for (int i = 0; i < m; ++i) {
147+
int n = queries[i][0], k = queries[i][1];
148+
long t = 1;
149+
for (int x : P[k]) {
150+
t = t * comb(x + n - 1, n - 1) % MOD;
151+
}
152+
ans[i] = (int) t;
153+
}
154+
return ans;
155+
}
156+
}
157+
```
158+
159+
### **C++**
160+
161+
```cpp
162+
int N = 10020;
163+
int MOD = 1e9 + 7;
164+
long f[10020];
165+
long g[10020];
166+
vector<int> p[10020];
167+
168+
long qmi(long a, long k, long p) {
169+
long res = 1;
170+
while (k != 0) {
171+
if ((k & 1) == 1) {
172+
res = res * a % p;
173+
}
174+
k >>= 1;
175+
a = a * a % p;
176+
}
177+
return res;
178+
}
179+
180+
int init = []() {
181+
f[0] = 1;
182+
g[0] = 1;
183+
for (int i = 1; i < N; ++i) {
184+
f[i] = f[i - 1] * i % MOD;
185+
g[i] = qmi(f[i], MOD - 2, MOD);
186+
int x = i;
187+
for (int j = 2; j <= x / j; ++j) {
188+
if (x % j == 0) {
189+
int cnt = 0;
190+
while (x % j == 0) {
191+
++cnt;
192+
x /= j;
193+
}
194+
p[i].push_back(cnt);
195+
}
196+
}
197+
if (x > 1) {
198+
p[i].push_back(1);
199+
}
200+
}
201+
return 0;
202+
}();
203+
204+
int comb(int n, int k) {
205+
return (f[n] * g[k] % MOD) * g[n - k] % MOD;
206+
}
207+
208+
class Solution {
209+
public:
210+
vector<int> waysToFillArray(vector<vector<int>>& queries) {
211+
vector<int> ans;
212+
for (auto& q : queries) {
213+
int n = q[0], k = q[1];
214+
long long t = 1;
215+
for (int x : p[k]) {
216+
t = t * comb(x + n - 1, n - 1) % MOD;
217+
}
218+
ans.push_back(t);
219+
}
220+
return ans;
221+
}
222+
};
223+
```
224+
225+
### **Go**
226+
227+
```go
228+
const n = 1e4 + 20
229+
const mod = 1e9 + 7
230+
231+
var f = make([]int, n)
232+
var g = make([]int, n)
233+
var p = make([][]int, n)
234+
235+
func qmi(a, k, p int) int {
236+
res := 1
237+
for k != 0 {
238+
if k&1 == 1 {
239+
res = res * a % p
240+
}
241+
k >>= 1
242+
a = a * a % p
243+
}
244+
return res
245+
}
246+
247+
func init() {
248+
f[0], g[0] = 1, 1
249+
for i := 1; i < n; i++ {
250+
f[i] = f[i-1] * i % mod
251+
g[i] = qmi(f[i], mod-2, mod)
252+
x := i
253+
for j := 2; j <= x/j; j++ {
254+
if x%j == 0 {
255+
cnt := 0
256+
for x%j == 0 {
257+
cnt++
258+
x /= j
259+
}
260+
p[i] = append(p[i], cnt)
261+
}
262+
}
263+
if x > 1 {
264+
p[i] = append(p[i], 1)
265+
}
266+
}
267+
}
268+
269+
func comb(n, k int) int {
270+
return (f[n] * g[k] % mod) * g[n-k] % mod
271+
}
61272
273+
func waysToFillArray(queries [][]int) (ans []int) {
274+
for _, q := range queries {
275+
n, k := q[0], q[1]
276+
t := 1
277+
for _, x := range p[k] {
278+
t = t * comb(x+n-1, n-1) % mod
279+
}
280+
ans = append(ans, t)
281+
}
282+
return
283+
}
62284
```
63285

64286
### **...**

0 commit comments

Comments
 (0)