Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions solution/0800-0899/0846.Hand of Straights/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ func isNStraightHand(hand []int, groupSize int) bool {
}
```

#### TypeScript

```ts
function isNStraightHand(hand: number[], groupSize: number) {
const cnt: Record<number, number> = {};
for (const i of hand) {
cnt[i] = (cnt[i] ?? 0) + 1;
}

const keys = Object.keys(cnt).map(Number);
for (const i of keys) {
while (cnt[i]) {
for (let j = i; j < groupSize + i; j++) {
if (!cnt[j]) {
return false;
}
cnt[j]--;
}
}
}

return true;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -299,6 +324,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
}
```

#### TypeScript

```ts
function isNStraightHand(hand: number[], groupSize: number): boolean {
const n = hand.length;
if (n % groupSize) {
return false;
}

const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
hand.sort((a, b) => a - b);

for (let i = 0; i < n; i++) {
let isPushed = false;

for (const g of groups) {
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) {
continue;
}

g.push(hand[i]);
isPushed = true;
break;
}

if (!isPushed) {
return false;
}
}

return true;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
59 changes: 59 additions & 0 deletions solution/0800-0899/0846.Hand of Straights/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ func isNStraightHand(hand []int, groupSize int) bool {
}
```

#### TypeScript

```ts
function isNStraightHand(hand: number[], groupSize: number) {
const cnt: Record<number, number> = {};
for (const i of hand) {
cnt[i] = (cnt[i] ?? 0) + 1;
}

const keys = Object.keys(cnt).map(Number);
for (const i of keys) {
while (cnt[i]) {
for (let j = i; j < groupSize + i; j++) {
if (!cnt[j]) {
return false;
}
cnt[j]--;
}
}
}

return true;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -284,6 +309,40 @@ func isNStraightHand(hand []int, groupSize int) bool {
}
```

#### TypeScript

```ts
function isNStraightHand(hand: number[], groupSize: number): boolean {
const n = hand.length;
if (n % groupSize) {
return false;
}

const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
hand.sort((a, b) => a - b);

for (let i = 0; i < n; i++) {
let isPushed = false;

for (const g of groups) {
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) {
continue;
}

g.push(hand[i]);
isPushed = true;
break;
}

if (!isPushed) {
return false;
}
}

return true;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
20 changes: 20 additions & 0 deletions solution/0800-0899/0846.Hand of Straights/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function isNStraightHand(hand: number[], groupSize: number) {
const cnt: Record<number, number> = {};
for (const i of hand) {
cnt[i] = (cnt[i] ?? 0) + 1;
}

const keys = Object.keys(cnt).map(Number);
for (const i of keys) {
while (cnt[i]) {
for (let j = i; j < groupSize + i; j++) {
if (!cnt[j]) {
return false;
}
cnt[j]--;
}
}
}

return true;
}
29 changes: 29 additions & 0 deletions solution/0800-0899/0846.Hand of Straights/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function isNStraightHand(hand: number[], groupSize: number): boolean {
const n = hand.length;
if (n % groupSize) {
return false;
}

const groups: number[][] = Array.from({ length: n / groupSize }, () => []);
hand.sort((a, b) => a - b);

for (let i = 0; i < n; i++) {
let isPushed = false;

for (const g of groups) {
if (g.length === groupSize || (g.length && hand[i] - g.at(-1)! !== 1)) {
continue;
}

g.push(hand[i]);
isPushed = true;
break;
}

if (!isPushed) {
return false;
}
}

return true;
}