Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add ts solution to lc problem: No.0846 #3059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 7 commits into doocs:main from rain84:feature/lc-0846
Jun 7, 2024
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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;
}

AltStyle によって変換されたページ (->オリジナル) /