File tree 4 files changed +131
-3
lines changed
solution/0700-0799/0781.Rabbits in Forest
4 files changed +131
-3
lines changed Original file line number Diff line number Diff line change @@ -31,10 +31,10 @@ tags:
31
31
<strong >输入:</strong >answers = [1,1,2]
32
32
<strong >输出:</strong >5
33
33
<strong >解释:</strong >
34
- 两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
34
+ 两只回答了 "1" 的兔子可能有相同的颜色,设为红色。
35
35
之后回答了 "2" 的兔子不会是红色,否则他们的回答会相互矛盾。
36
- 设回答了 "2" 的兔子为蓝色。
37
- 此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
36
+ 设回答了 "2" 的兔子为蓝色。
37
+ 此外,森林中还应有另外 2 只蓝色兔子的回答没有包含在数组中。
38
38
因此森林中兔子的最少数量是 5 只:3 只回答的和 2 只没有回答的。
39
39
</pre >
40
40
@@ -159,4 +159,52 @@ function numRabbits(answers: number[]): number {
159
159
160
160
<!-- solution: end -->
161
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
+
162
210
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -157,4 +157,54 @@ function numRabbits(answers: number[]): number {
157
157
158
158
<!-- solution: end -->
159
159
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
+
160
210
<!-- problem: end -->
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments