Skip to content

Commit 017f330

Browse files
committed
feat: add solutions to lcp problem: No.66
1 parent a7eebca commit 017f330

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

.github/workflows/chatgpt-cr.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ jobs:
1515
steps:
1616
- uses: anc95/ChatGPT-CodeReview@main
1717
env:
18-
GITHUB_TOKEN: ${{ secrets.ACTION_TOKEN }}
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1919
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
2020
LANGUAGE: Chinese

lcp/LCP 66. 最小展台数量/README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,110 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
**方法一:计数**
43+
44+
我们用哈希表或数组 $cnt$ 记录当前可用的展台以及数量。
45+
46+
然后遍历 $demand$,对于每一天,遍历该天的展台需求,如果 $cnt$ 中有该展台,则将其数量减一,否则我们需要准备一个新的展台,答案加一。遍历结束后,将该天的所有展台都加入 $cnt$ 中。
47+
48+
最后返回答案即可。
49+
50+
时间复杂度 $O(L)$,空间复杂度 $O(C)$。其中 $L$ 为 $demand$ 中所有字符串的长度之和,而 $C$ 为字符集的大小,本题中 $C = 26$。
51+
4252
<!-- tabs:start -->
4353

4454
### **Python3**
4555

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

4858
```python
49-
59+
class Solution:
60+
def minNumBooths(self, demand: List[str]) -> int:
61+
cnt = Counter()
62+
ans = 0
63+
for s in demand:
64+
for c in s:
65+
if cnt[c]:
66+
cnt[c] -= 1
67+
else:
68+
ans += 1
69+
for c in s:
70+
cnt[c] += 1
71+
return ans
5072
```
5173

5274
### **Java**
5375

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

5678
```java
79+
class Solution {
80+
public int minNumBooths(String[] demand) {
81+
int[] cnt = new int[26];
82+
int ans = 0;
83+
for (var s : demand) {
84+
int m = s.length();
85+
for (int i = 0; i < m; ++i) {
86+
int j = s.charAt(i) - 'a';
87+
if (cnt[j] > 0) {
88+
--cnt[j];
89+
} else {
90+
++ans;
91+
}
92+
}
93+
for (int i = 0; i < m; ++i) {
94+
++cnt[s.charAt(i) - 'a'];
95+
}
96+
}
97+
return ans;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int minNumBooths(vector<string>& demand) {
108+
int cnt[26]{};
109+
int ans = 0;
110+
for (auto& s : demand) {
111+
for (char& c : s) {
112+
if (cnt[c - 'a']) {
113+
--cnt[c - 'a'];
114+
} else {
115+
++ans;
116+
}
117+
}
118+
for (char& c : s) {
119+
++cnt[c - 'a'];
120+
}
121+
}
122+
return ans;
123+
}
124+
};
125+
```
57126
127+
### **Go**
128+
129+
```go
130+
func minNumBooths(demand []string) (ans int) {
131+
cnt := [26]int{}
132+
for _, s := range demand {
133+
for _, c := range s {
134+
if cnt[c-'a'] > 0 {
135+
cnt[c-'a']--
136+
} else {
137+
ans++
138+
}
139+
}
140+
for _, c := range s {
141+
cnt[c-'a']++
142+
}
143+
}
144+
return
145+
}
58146
```
59147

60148
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minNumBooths(vector<string>& demand) {
4+
int cnt[26]{};
5+
int ans = 0;
6+
for (auto& s : demand) {
7+
for (char& c : s) {
8+
if (cnt[c - 'a']) {
9+
--cnt[c - 'a'];
10+
} else {
11+
++ans;
12+
}
13+
}
14+
for (char& c : s) {
15+
++cnt[c - 'a'];
16+
}
17+
}
18+
return ans;
19+
}
20+
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func minNumBooths(demand []string) (ans int) {
2+
cnt := [26]int{}
3+
for _, s := range demand {
4+
for _, c := range s {
5+
if cnt[c-'a'] > 0 {
6+
cnt[c-'a']--
7+
} else {
8+
ans++
9+
}
10+
}
11+
for _, c := range s {
12+
cnt[c-'a']++
13+
}
14+
}
15+
return
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int minNumBooths(String[] demand) {
3+
int[] cnt = new int[26];
4+
int ans = 0;
5+
for (var s : demand) {
6+
int m = s.length();
7+
for (int i = 0; i < m; ++i) {
8+
int j = s.charAt(i) - 'a';
9+
if (cnt[j] > 0) {
10+
--cnt[j];
11+
} else {
12+
++ans;
13+
}
14+
}
15+
for (int i = 0; i < m; ++i) {
16+
++cnt[s.charAt(i) - 'a'];
17+
}
18+
}
19+
return ans;
20+
}
21+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minNumBooths(self, demand: List[str]) -> int:
3+
cnt = Counter()
4+
ans = 0
5+
for s in demand:
6+
for c in s:
7+
if cnt[c]:
8+
cnt[c] -= 1
9+
else:
10+
ans += 1
11+
for c in s:
12+
cnt[c] += 1
13+
return ans

0 commit comments

Comments
 (0)