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

Commit dae8f50

Browse files
authored
feat: add solutions to lc problem: No.0781 (#4364)
1 parent d057514 commit dae8f50

File tree

4 files changed

+131
-3
lines changed

4 files changed

+131
-3
lines changed

‎solution/0700-0799/0781.Rabbits in Forest/README.md‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ tags:
3131
<strong>输入:</strong>answers = [1,1,2]
3232
<strong>输出:</strong>5
3333
<strong>解释:</strong>
34-
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
34+
两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
3535
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
36-
设回答了 "2" 的兔子为蓝色。
37-
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
36+
设回答了 "2" 的兔子为蓝色。
37+
此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
3838
因此森林中兔子的最少数量是 5 只:3 只回答的和 2 只没有回答的。
3939
</pre>
4040

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

160160
<!-- solution:end -->
161161

162+
### Solution 2: Greedy + Hash Map
163+
164+
<!-- tabs:start -->
165+
166+
#### TypeScript
167+
168+
```ts
169+
function numRabbits(answers: number[]): number {
170+
const cnt: Record<number, number> = {};
171+
let ans = 0;
172+
173+
for (const x of answers) {
174+
if (cnt[x]) {
175+
cnt[x]--;
176+
} else {
177+
cnt[x] = x;
178+
ans += x + 1;
179+
}
180+
}
181+
182+
return ans;
183+
}
184+
```
185+
186+
#### JavaScript
187+
188+
```js
189+
function numRabbits(answers) {
190+
const cnt = {};
191+
let ans = 0;
192+
193+
for (const x of answers) {
194+
if (cnt[x]) {
195+
cnt[x]--;
196+
} else {
197+
cnt[x] = x;
198+
ans += x + 1;
199+
}
200+
}
201+
202+
return ans;
203+
}
204+
```
205+
206+
<!-- tabs:end -->
207+
208+
<!-- solution:end -->
209+
162210
<!-- problem:end -->

‎solution/0700-0799/0781.Rabbits in Forest/README_EN.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,54 @@ function numRabbits(answers: number[]): number {
157157

158158
<!-- solution:end -->
159159

160+
<!-- solution:start -->
161+
162+
### Solution 2: Greedy + Hash Map
163+
164+
<!-- tabs:start -->
165+
166+
#### TypeScript
167+
168+
```ts
169+
function numRabbits(answers: number[]): number {
170+
const cnt: Record<number, number> = {};
171+
let ans = 0;
172+
173+
for (const x of answers) {
174+
if (cnt[x]) {
175+
cnt[x]--;
176+
} else {
177+
cnt[x] = x;
178+
ans += x + 1;
179+
}
180+
}
181+
182+
return ans;
183+
}
184+
```
185+
186+
#### JavaScript
187+
188+
```js
189+
function numRabbits(answers) {
190+
const cnt = {};
191+
let ans = 0;
192+
193+
for (const x of answers) {
194+
if (cnt[x]) {
195+
cnt[x]--;
196+
} else {
197+
cnt[x] = x;
198+
ans += x + 1;
199+
}
200+
}
201+
202+
return ans;
203+
}
204+
```
205+
206+
<!-- tabs:end -->
207+
208+
<!-- solution:end -->
209+
160210
<!-- problem:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function numRabbits(answers) {
2+
const cnt = {};
3+
let ans = 0;
4+
5+
for (const x of answers) {
6+
if (cnt[x]) {
7+
cnt[x]--;
8+
} else {
9+
cnt[x] = x;
10+
ans += x + 1;
11+
}
12+
}
13+
14+
return ans;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function numRabbits(answers: number[]): number {
2+
const cnt: Record<number, number> = {};
3+
let ans = 0;
4+
5+
for (const x of answers) {
6+
if (cnt[x]) {
7+
cnt[x]--;
8+
} else {
9+
cnt[x] = x;
10+
ans += x + 1;
11+
}
12+
}
13+
14+
return ans;
15+
}

0 commit comments

Comments
(0)

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