File tree 6 files changed +160
-2
lines changed
6 files changed +160
-2
lines changed Original file line number Diff line number Diff line change 15
15
steps :
16
16
- uses : anc95/ChatGPT-CodeReview@main
17
17
env :
18
- GITHUB_TOKEN : ${{ secrets.ACTION_TOKEN }}
18
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
19
19
OPENAI_API_KEY : ${{ secrets.OPENAI_API_KEY }}
20
20
LANGUAGE : Chinese
Original file line number Diff line number Diff line change 39
39
40
40
<!-- 这里可写通用的实现逻辑 -->
41
41
42
+ ** 方法一:计数**
43
+
44
+ 我们用哈希表或数组 $cnt$ 记录当前可用的展台以及数量。
45
+
46
+ 然后遍历 $demand$,对于每一天,遍历该天的展台需求,如果 $cnt$ 中有该展台,则将其数量减一,否则我们需要准备一个新的展台,答案加一。遍历结束后,将该天的所有展台都加入 $cnt$ 中。
47
+
48
+ 最后返回答案即可。
49
+
50
+ 时间复杂度 $O(L)$,空间复杂度 $O(C)$。其中 $L$ 为 $demand$ 中所有字符串的长度之和,而 $C$ 为字符集的大小,本题中 $C = 26$。
51
+
42
52
<!-- tabs:start -->
43
53
44
54
### ** Python3**
45
55
46
56
<!-- 这里可写当前语言的特殊实现逻辑 -->
47
57
48
58
``` 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
50
72
```
51
73
52
74
### ** Java**
53
75
54
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
77
56
78
``` 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
+ ```
57
126
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
+ }
58
146
```
59
147
60
148
### ** ...**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments