Skip to content

Commit 8fec0e4

Browse files
committed
feat: update solutions to lc problems: No.1170,1183
1 parent dcb83f6 commit 8fec0e4

File tree

7 files changed

+10
-49
lines changed

7 files changed

+10
-49
lines changed

solution/1100-1199/1170.Compare Strings by Frequency of the Smallest Character/README.md

+2-11
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,9 @@ function numSmallerByFrequency(queries: string[], words: string[]): number[] {
196196
for (const c of s) {
197197
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
198198
}
199-
for (const x of cnt) {
200-
if (x) {
201-
return x;
202-
}
203-
}
204-
return 0;
199+
return cnt.find(x => x > 0);
205200
};
206-
const nums: number[] = [];
207-
for (const w of words) {
208-
nums.push(f(w));
209-
}
210-
nums.sort((a, b) => a - b);
201+
const nums = words.map(f).sort((a, b) => a - b);
211202
const ans: number[] = [];
212203
for (const q of queries) {
213204
const x = f(q);

solution/1100-1199/1170.Compare Strings by Frequency of the Smallest Character/README_EN.md

+2-11
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,9 @@ function numSmallerByFrequency(queries: string[], words: string[]): number[] {
174174
for (const c of s) {
175175
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
176176
}
177-
for (const x of cnt) {
178-
if (x) {
179-
return x;
180-
}
181-
}
182-
return 0;
177+
return cnt.find(x => x > 0);
183178
};
184-
const nums: number[] = [];
185-
for (const w of words) {
186-
nums.push(f(w));
187-
}
188-
nums.sort((a, b) => a - b);
179+
const nums = words.map(f).sort((a, b) => a - b);
189180
const ans: number[] = [];
190181
for (const q of queries) {
191182
const x = f(q);

solution/1100-1199/1170.Compare Strings by Frequency of the Smallest Character/Solution.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,9 @@ function numSmallerByFrequency(queries: string[], words: string[]): number[] {
44
for (const c of s) {
55
cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)]++;
66
}
7-
for (const x of cnt) {
8-
if (x) {
9-
return x;
10-
}
11-
}
12-
return 0;
7+
return cnt.find(x => x > 0);
138
};
14-
const nums: number[] = [];
15-
for (const w of words) {
16-
nums.push(f(w));
17-
}
18-
nums.sort((a, b) => a - b);
9+
const nums = words.map(f).sort((a, b) => a - b);
1910
const ans: number[] = [];
2011
for (const q of queries) {
2112
const x = f(q);

solution/1100-1199/1175.Prime Arrangements/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
我们在 $[2,n]$ 范围内顺序遍历每个数 $i$,如果这个数为质数($primes[i]==true$),质数个数增 1,然后将其所有的倍数 $j$ 都标记为合数(除了该质数本身),即 $primes[j]=false$,这样在运行结束的时候我们即能知道质数的个数。
5353

54-
时间复杂度 $O(nloglogn)$。
54+
时间复杂度 $O(n \times \log \log n)$。
5555

5656
<!-- tabs:start -->
5757

solution/1100-1199/1183.Maximum Number of Ones/README.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ var maximumNumberOfOnes = function (width, height, sideLength, maxOnes) {
169169
}
170170
}
171171
cnt.sort((a, b) => b - a);
172-
let ans = 0;
173-
for (let i = 0; i < maxOnes; ++i) {
174-
ans += cnt[i];
175-
}
176-
return ans;
172+
return cnt.slice(0, maxOnes).reduce((a, b) => a + b, 0);
177173
};
178174
```
179175

solution/1100-1199/1183.Maximum Number of Ones/README_EN.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,7 @@ var maximumNumberOfOnes = function (width, height, sideLength, maxOnes) {
151151
}
152152
}
153153
cnt.sort((a, b) => b - a);
154-
let ans = 0;
155-
for (let i = 0; i < maxOnes; ++i) {
156-
ans += cnt[i];
157-
}
158-
return ans;
154+
return cnt.slice(0, maxOnes).reduce((a, b) => a + b, 0);
159155
};
160156
```
161157

solution/1100-1199/1183.Maximum Number of Ones/Solution.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@ var maximumNumberOfOnes = function (width, height, sideLength, maxOnes) {
1515
}
1616
}
1717
cnt.sort((a, b) => b - a);
18-
let ans = 0;
19-
for (let i = 0; i < maxOnes; ++i) {
20-
ans += cnt[i];
21-
}
22-
return ans;
18+
return cnt.slice(0, maxOnes).reduce((a, b) => a + b, 0);
2319
};

0 commit comments

Comments
 (0)