Skip to content

Commit d8ec3b6

Browse files
committed
feat: add solutions to lcp problem: No.68
1 parent 4c5a06e commit d8ec3b6

File tree

5 files changed

+167
-1
lines changed

5 files changed

+167
-1
lines changed

lcp/LCP 68. 美观的花束/README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,114 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:双指针**
48+
49+
我们用双指针 $j$ 和 $i$ 分别指向当前窗口的左右端点,用数组或哈希表 $d$ 记录当前窗口内的元素以及出现的次数。
50+
51+
遍历数组 $flowers$,每一次我们将 $flowers[i]$ 加入到窗口中,即 $d[flowers[i]]++$,然后判断 $d[flowers[i]]$ 是否大于 $cnt$,如果大于 $cnt$,则我们需要将 $flowers[j]$ 从窗口中移除,即 $d[flowers[j]]--$,并将 $j$ 右移,直到 $d[flowers[i]] \leq cnt$。此时窗口内的元素都不超过 $cnt$ 个,因此我们可以将 $i - j + 1$ 加到答案中。
52+
53+
最后返回答案即可。
54+
55+
时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为数组 $flowers$ 的长度以及数组 $flowers$ 中的最大值。
56+
4757
<!-- tabs:start -->
4858

4959
### **Python3**
5060

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

5363
```python
54-
64+
class Solution:
65+
def beautifulBouquet(self, flowers: List[int], cnt: int) -> int:
66+
mod = 10**9 + 7
67+
d = Counter()
68+
ans = j = 0
69+
for i, x in enumerate(flowers):
70+
d[x] += 1
71+
while d[x] > cnt:
72+
d[flowers[j]] -= 1
73+
j += 1
74+
ans = (ans + i - j + 1) % mod
75+
return ans
5576
```
5677

5778
### **Java**
5879

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

6182
```java
83+
class Solution {
84+
public int beautifulBouquet(int[] flowers, int cnt) {
85+
int mx = 0;
86+
for (int x : flowers) {
87+
mx = Math.max(mx, x);
88+
}
89+
int[] d = new int[mx + 1];
90+
long ans = 0;
91+
final int mod = (int) 1e9 + 7;
92+
for (int i = 0, j = 0; i < flowers.length; ++i) {
93+
++d[flowers[i]];
94+
while (d[flowers[i]] > cnt) {
95+
--d[flowers[j++]];
96+
}
97+
ans = (ans + i - j + 1) % mod;
98+
}
99+
return (int) ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int beautifulBouquet(vector<int>& flowers, int cnt) {
110+
int mx = *max_element(flowers.begin(), flowers.end());
111+
int d[mx + 1];
112+
memset(d, 0, sizeof(d));
113+
long long ans = 0;
114+
const int mod = 1e9 + 7;
115+
for (int i = 0, j = 0; i < flowers.size(); ++i) {
116+
++d[flowers[i]];
117+
while (d[flowers[i]] > cnt) {
118+
--d[flowers[j++]];
119+
}
120+
ans = (ans + i - j + 1) % mod;
121+
}
122+
return ans;
123+
}
124+
};
125+
```
62126
127+
### **Go**
128+
129+
```go
130+
func beautifulBouquet(flowers []int, cnt int) (ans int) {
131+
mx := 0
132+
for _, x := range flowers {
133+
mx = max(mx, x)
134+
}
135+
d := make([]int, mx+1)
136+
j := 0
137+
const mod = 1e9 + 7
138+
for i, x := range flowers {
139+
d[x]++
140+
for d[x] > cnt {
141+
d[flowers[j]]--
142+
j++
143+
}
144+
ans = (ans + i - j + 1) % mod
145+
}
146+
return
147+
}
148+
149+
func max(a, b int) int {
150+
if a > b {
151+
return a
152+
}
153+
return b
154+
}
63155
```
64156

65157
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int beautifulBouquet(vector<int>& flowers, int cnt) {
4+
int mx = *max_element(flowers.begin(), flowers.end());
5+
int d[mx + 1];
6+
memset(d, 0, sizeof(d));
7+
long long ans = 0;
8+
const int mod = 1e9 + 7;
9+
for (int i = 0, j = 0; i < flowers.size(); ++i) {
10+
++d[flowers[i]];
11+
while (d[flowers[i]] > cnt) {
12+
--d[flowers[j++]];
13+
}
14+
ans = (ans + i - j + 1) % mod;
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func beautifulBouquet(flowers []int, cnt int) (ans int) {
2+
mx := 0
3+
for _, x := range flowers {
4+
mx = max(mx, x)
5+
}
6+
d := make([]int, mx+1)
7+
j := 0
8+
const mod = 1e9 + 7
9+
for i, x := range flowers {
10+
d[x]++
11+
for d[x] > cnt {
12+
d[flowers[j]]--
13+
j++
14+
}
15+
ans = (ans + i - j + 1) % mod
16+
}
17+
return
18+
}
19+
20+
func max(a, b int) int {
21+
if a > b {
22+
return a
23+
}
24+
return b
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int beautifulBouquet(int[] flowers, int cnt) {
3+
int mx = 0;
4+
for (int x : flowers) {
5+
mx = Math.max(mx, x);
6+
}
7+
int[] d = new int[mx + 1];
8+
long ans = 0;
9+
final int mod = (int) 1e9 + 7;
10+
for (int i = 0, j = 0; i < flowers.length; ++i) {
11+
++d[flowers[i]];
12+
while (d[flowers[i]] > cnt) {
13+
--d[flowers[j++]];
14+
}
15+
ans = (ans + i - j + 1) % mod;
16+
}
17+
return (int) ans;
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def beautifulBouquet(self, flowers: List[int], cnt: int) -> int:
3+
mod = 10**9 + 7
4+
d = Counter()
5+
ans = j = 0
6+
for i, x in enumerate(flowers):
7+
d[x] += 1
8+
while d[x] > cnt:
9+
d[flowers[j]] -= 1
10+
j += 1
11+
ans = (ans + i - j + 1) % mod
12+
return ans

0 commit comments

Comments
 (0)