Skip to content

Commit a0aef0a

Browse files
committed
feat: add solutions to lc problems: No.1819,2288
* No.1819.Number of Different Subsequences GCDs * No.2288.Apply Discount to Prices
1 parent 0ed0a61 commit a0aef0a

File tree

13 files changed

+526
-51
lines changed

13 files changed

+526
-51
lines changed

solution/1800-1899/1819.Number of Different Subsequences GCDs/README.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,142 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:枚举 + 数学**
57+
58+
对于数组 `nums` 的所有子序列,其最大公约数一定不超过数组中的最大值 $mx$。
59+
60+
因此我们可以枚举 $[1,.. mx]$ 中的每个数 $x$,判断 $x$ 是否为数组 `nums` 的子序列的最大公约数,如果是,则答案加一。
61+
62+
那么问题转换为:判断 $x$ 是否为数组 `nums` 的子序列的最大公约数。我们可以通过枚举 $x$ 的倍数 $y$,判断 $y$ 是否在数组 `nums` 中,如果 $y$ 在数组 `nums` 中,则计算 $y$ 的最大公约数 $g$,如果出现 $g = x$,则 $x$ 是数组 `nums` 的子序列的最大公约数。
63+
64+
时间复杂度 $O(n + M \times \log M)$,空间复杂度 $O(M)$。其中 $n$ 和 $M$ 分别是数组 `nums` 的长度和数组 `nums` 中的最大值。
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**
5969

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

6272
```python
63-
73+
class Solution:
74+
def countDifferentSubsequenceGCDs(self, nums: List[int]) -> int:
75+
mx = max(nums)
76+
vis = set(nums)
77+
ans = 0
78+
for x in range(1, mx + 1):
79+
g = 0
80+
for y in range(x, mx + 1, x):
81+
if y in vis:
82+
g = gcd(g, y)
83+
if g == x:
84+
ans += 1
85+
break
86+
return ans
6487
```
6588

6689
### **Java**
6790

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

7093
```java
94+
class Solution {
95+
public int countDifferentSubsequenceGCDs(int[] nums) {
96+
int mx = Arrays.stream(nums).max().getAsInt();
97+
boolean[] vis = new boolean[mx + 1];
98+
for (int x : nums) {
99+
vis[x] = true;
100+
}
101+
int ans = 0;
102+
for (int x = 1; x <= mx; ++x) {
103+
int g = 0;
104+
for (int y = x; y <= mx; y += x) {
105+
if (vis[y]) {
106+
g = gcd(g, y);
107+
if (x == g) {
108+
++ans;
109+
break;
110+
}
111+
}
112+
}
113+
}
114+
return ans;
115+
}
116+
117+
private int gcd(int a, int b) {
118+
return b == 0 ? a : gcd(b, a % b);
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
int countDifferentSubsequenceGCDs(vector<int>& nums) {
129+
int mx = *max_element(nums.begin(), nums.end());
130+
vector<bool> vis(mx + 1);
131+
for (int& x : nums) {
132+
vis[x] = true;
133+
}
134+
int ans = 0;
135+
for (int x = 1; x <= mx; ++x) {
136+
int g = 0;
137+
for (int y = x; y <= mx; y += x) {
138+
if (vis[y]) {
139+
g = gcd(g, y);
140+
if (g == x) {
141+
++ans;
142+
break;
143+
}
144+
}
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
71151
152+
### **Go**
153+
154+
```go
155+
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
156+
mx := 0
157+
for _, x := range nums {
158+
mx = max(mx, x)
159+
}
160+
vis := make([]bool, mx+1)
161+
for _, x := range nums {
162+
vis[x] = true
163+
}
164+
for x := 1; x <= mx; x++ {
165+
g := 0
166+
for y := x; y <= mx; y += x {
167+
if vis[y] {
168+
g = gcd(g, y)
169+
if g == x {
170+
ans++
171+
break
172+
}
173+
}
174+
}
175+
}
176+
return
177+
}
178+
179+
func max(a, b int) int {
180+
if a > b {
181+
return a
182+
}
183+
return b
184+
}
185+
186+
func gcd(a, b int) int {
187+
if b == 0 {
188+
return a
189+
}
190+
return gcd(b, a%b)
191+
}
72192
```
73193

74194
### **...**

solution/1800-1899/1819.Number of Different Subsequences GCDs/README_EN.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,123 @@ The different GCDs are 6, 10, 3, 2, and 1.
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def countDifferentSubsequenceGCDs(self, nums: List[int]) -> int:
57+
mx = max(nums)
58+
vis = set(nums)
59+
ans = 0
60+
for x in range(1, mx + 1):
61+
g = 0
62+
for y in range(x, mx + 1, x):
63+
if y in vis:
64+
g = gcd(g, y)
65+
if g == x:
66+
ans += 1
67+
break
68+
return ans
5669
```
5770

5871
### **Java**
5972

6073
```java
74+
class Solution {
75+
public int countDifferentSubsequenceGCDs(int[] nums) {
76+
int mx = Arrays.stream(nums).max().getAsInt();
77+
boolean[] vis = new boolean[mx + 1];
78+
for (int x : nums) {
79+
vis[x] = true;
80+
}
81+
int ans = 0;
82+
for (int x = 1; x <= mx; ++x) {
83+
int g = 0;
84+
for (int y = x; y <= mx; y += x) {
85+
if (vis[y]) {
86+
g = gcd(g, y);
87+
if (x == g) {
88+
++ans;
89+
break;
90+
}
91+
}
92+
}
93+
}
94+
return ans;
95+
}
96+
97+
private int gcd(int a, int b) {
98+
return b == 0 ? a : gcd(b, a % b);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int countDifferentSubsequenceGCDs(vector<int>& nums) {
109+
int mx = *max_element(nums.begin(), nums.end());
110+
vector<bool> vis(mx + 1);
111+
for (int& x : nums) {
112+
vis[x] = true;
113+
}
114+
int ans = 0;
115+
for (int x = 1; x <= mx; ++x) {
116+
int g = 0;
117+
for (int y = x; y <= mx; y += x) {
118+
if (vis[y]) {
119+
g = gcd(g, y);
120+
if (g == x) {
121+
++ans;
122+
break;
123+
}
124+
}
125+
}
126+
}
127+
return ans;
128+
}
129+
};
130+
```
61131
132+
### **Go**
133+
134+
```go
135+
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
136+
mx := 0
137+
for _, x := range nums {
138+
mx = max(mx, x)
139+
}
140+
vis := make([]bool, mx+1)
141+
for _, x := range nums {
142+
vis[x] = true
143+
}
144+
for x := 1; x <= mx; x++ {
145+
g := 0
146+
for y := x; y <= mx; y += x {
147+
if vis[y] {
148+
g = gcd(g, y)
149+
if g == x {
150+
ans++
151+
break
152+
}
153+
}
154+
}
155+
}
156+
return
157+
}
158+
159+
func max(a, b int) int {
160+
if a > b {
161+
return a
162+
}
163+
return b
164+
}
165+
166+
func gcd(a, b int) int {
167+
if b == 0 {
168+
return a
169+
}
170+
return gcd(b, a%b)
171+
}
62172
```
63173

64174
### **...**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
int countDifferentSubsequenceGCDs(vector<int>& nums) {
4+
int mx = *max_element(nums.begin(), nums.end());
5+
vector<bool> vis(mx + 1);
6+
for (int& x : nums) {
7+
vis[x] = true;
8+
}
9+
int ans = 0;
10+
for (int x = 1; x <= mx; ++x) {
11+
int g = 0;
12+
for (int y = x; y <= mx; y += x) {
13+
if (vis[y]) {
14+
g = gcd(g, y);
15+
if (g == x) {
16+
++ans;
17+
break;
18+
}
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
func countDifferentSubsequenceGCDs(nums []int) (ans int) {
2+
mx := 0
3+
for _, x := range nums {
4+
mx = max(mx, x)
5+
}
6+
vis := make([]bool, mx+1)
7+
for _, x := range nums {
8+
vis[x] = true
9+
}
10+
for x := 1; x <= mx; x++ {
11+
g := 0
12+
for y := x; y <= mx; y += x {
13+
if vis[y] {
14+
g = gcd(g, y)
15+
if g == x {
16+
ans++
17+
break
18+
}
19+
}
20+
}
21+
}
22+
return
23+
}
24+
25+
func max(a, b int) int {
26+
if a > b {
27+
return a
28+
}
29+
return b
30+
}
31+
32+
func gcd(a, b int) int {
33+
if b == 0 {
34+
return a
35+
}
36+
return gcd(b, a%b)
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int countDifferentSubsequenceGCDs(int[] nums) {
3+
int mx = Arrays.stream(nums).max().getAsInt();
4+
boolean[] vis = new boolean[mx + 1];
5+
for (int x : nums) {
6+
vis[x] = true;
7+
}
8+
int ans = 0;
9+
for (int x = 1; x <= mx; ++x) {
10+
int g = 0;
11+
for (int y = x; y <= mx; y += x) {
12+
if (vis[y]) {
13+
g = gcd(g, y);
14+
if (x == g) {
15+
++ans;
16+
break;
17+
}
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
24+
private int gcd(int a, int b) {
25+
return b == 0 ? a : gcd(b, a % b);
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def countDifferentSubsequenceGCDs(self, nums: List[int]) -> int:
3+
mx = max(nums)
4+
vis = set(nums)
5+
ans = 0
6+
for x in range(1, mx + 1):
7+
g = 0
8+
for y in range(x, mx + 1, x):
9+
if y in vis:
10+
g = gcd(g, y)
11+
if g == x:
12+
ans += 1
13+
break
14+
return ans

solution/2200-2299/2287.Rearrange Characters to Make Target String/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
**方法一:计数 + 枚举**
59+
**方法一:计数**
6060

6161
我们统计字符串 `s``target` 中每个字符出现的次数,记为 `cnt1``cnt2`。对于 `target` 中的每个字符,我们计算 `cnt1` 中该字符出现的次数除以 `cnt2` 中该字符出现的次数,取最小值即可。
6262

0 commit comments

Comments
 (0)