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 solutions to lc problem: No.0781 #4364

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 2 commits into doocs:main from rain84:feature/lc-0781
Apr 21, 2025
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
54 changes: 51 additions & 3 deletions solution/0700-0799/0781.Rabbits in Forest/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ tags:
<strong>输入:</strong>answers = [1,1,2]
<strong>输出:</strong>5
<strong>解释:</strong>
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
设回答了 "2" 的兔子为蓝色。
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
设回答了 "2" 的兔子为蓝色。
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
因此森林中兔子的最少数量是 5 只:3 只回答的和 2 只没有回答的。
</pre>

Expand Down Expand Up @@ -159,4 +159,52 @@ function numRabbits(answers: number[]): number {

<!-- solution:end -->

### Solution 2: Greedy + Hash Map

<!-- tabs:start -->

#### TypeScript

```ts
function numRabbits(answers: number[]): number {
const cnt: Record<number, number> = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}
```

#### JavaScript

```js
function numRabbits(answers) {
const cnt = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
50 changes: 50 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,54 @@ function numRabbits(answers: number[]): number {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Greedy + Hash Map

<!-- tabs:start -->

#### TypeScript

```ts
function numRabbits(answers: number[]): number {
const cnt: Record<number, number> = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}
```

#### JavaScript

```js
function numRabbits(answers) {
const cnt = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
15 changes: 15 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution2.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function numRabbits(answers) {
const cnt = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}
15 changes: 15 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution2.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function numRabbits(answers: number[]): number {
const cnt: Record<number, number> = {};
let ans = 0;

for (const x of answers) {
if (cnt[x]) {
cnt[x]--;
} else {
cnt[x] = x;
ans += x + 1;
}
}

return ans;
}

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