Skip to content

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/divide-array-in-sets-of-k-consecutive-numbers
49+
50+
https://leetcode.cn/problems/hand-of-straights
51+
4852
https://leetcode.cn/problems/minimum-changes-to-make-alternating-binary-string
4953

5054
https://leetcode.cn/problems/sum-of-all-odd-length-subarrays
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import isNStraightHand from "../hand-of-straights/index.ts";
2+
3+
export default isNStraightHand;

hand-of-straights/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function isNStraightHand(hand: number[], groupSize: number): boolean {
2+
const n = hand.length;
3+
if (n % groupSize !== 0) {
4+
return false;
5+
}
6+
hand.sort((a, b) => a - b);
7+
const cnt = new Map<number, number>();
8+
for (const x of hand) {
9+
cnt.set(x, (cnt.get(x) || 0) + 1);
10+
}
11+
for (const x of hand) {
12+
if (!cnt.has(x)) {
13+
continue;
14+
}
15+
for (let j = 0; j < groupSize; j++) {
16+
const num = x + j;
17+
if (!cnt.has(num)) {
18+
return false;
19+
}
20+
cnt.set(num, (cnt.get(num) ?? 0) - 1);
21+
if (cnt.get(num) == 0) {
22+
cnt.delete(num);
23+
}
24+
}
25+
}
26+
return true;
27+
}
28+
export default isNStraightHand;
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
function minOperations(s: string): number {
2-
const res = Array.prototype.reduce.call(
3-
s,
4-
//@ts-ignore
5-
(p: number, c: string, i: number) => p + Number(i % 2 === Number(c)),
6-
0
7-
) as number;
8-
return Math.min(res, s.length - res);
9-
}
10-
export default minOperations;
1+
function minOperations(s: string): number {
2+
const res = Array.prototype.reduce.call(
3+
s,
4+
//@ts-ignore
5+
(p: number, c: string, i: number) => p + Number(i % 2 === Number(c)),
6+
0,
7+
) as number;
8+
return Math.min(res, s.length - res);
9+
}
10+
export default minOperations;

0 commit comments

Comments
 (0)