Skip to content

Commit f795901

Browse files
authored
feat: add solutions to lc problem: No.0781 (#4104)
No.0781.Rabbits in Forest
1 parent 394bbc0 commit f795901

File tree

11 files changed

+261
-166
lines changed

11 files changed

+261
-166
lines changed

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

+77-14
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

@@ -60,7 +60,13 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### 方法一
63+
### 方法一:贪心 + 哈希表
64+
65+
根据题目描述,回答相同的兔子,可能属于同一种颜色,而回答不同的兔子,不可能属于同一种颜色。
66+
67+
因此,我们用一个哈希表 $\textit{cnt}$ 记录每种回答出现的次数。对于每种回答 $x$ 及其出现次数 $v$,我们按照每种颜色有 $x + 1$ 只兔子的原则,计算出兔子的最少数量,并将其加入答案。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{answers}$ 的长度。
6470

6571
<!-- tabs:start -->
6672

@@ -69,26 +75,83 @@ tags:
6975
```python
7076
class Solution:
7177
def numRabbits(self, answers: List[int]) -> int:
72-
counter = Counter(answers)
73-
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
78+
cnt = Counter(answers)
79+
ans = 0
80+
for x, v in cnt.items():
81+
group = x + 1
82+
ans += (v + group - 1) // group * group
83+
return ans
7484
```
7585

7686
#### Java
7787

7888
```java
7989
class Solution {
8090
public int numRabbits(int[] answers) {
81-
Map<Integer, Integer> counter = new HashMap<>();
82-
for (int e : answers) {
83-
counter.put(e, counter.getOrDefault(e, 0) + 1);
91+
Map<Integer, Integer> cnt = new HashMap<>();
92+
for (int x : answers) {
93+
cnt.merge(x, 1, Integer::sum);
8494
}
85-
int res = 0;
86-
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
87-
int answer = entry.getKey(), count = entry.getValue();
88-
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
95+
int ans = 0;
96+
for (var e : cnt.entrySet()) {
97+
int group = e.getKey() + 1;
98+
ans += (e.getValue() + group - 1) / group * group;
99+
}
100+
return ans;
101+
}
102+
}
103+
```
104+
105+
#### C++
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int numRabbits(vector<int>& answers) {
111+
unordered_map<int, int> cnt;
112+
for (int x : answers) {
113+
++cnt[x];
89114
}
90-
return res;
115+
int ans = 0;
116+
for (auto& [x, v] : cnt) {
117+
int group = x + 1;
118+
ans += (v + group - 1) / group * group;
119+
}
120+
return ans;
121+
}
122+
};
123+
```
124+
125+
#### Go
126+
127+
```go
128+
func numRabbits(answers []int) (ans int) {
129+
cnt := map[int]int{}
130+
for _, x := range answers {
131+
cnt[x]++
132+
}
133+
for x, v := range cnt {
134+
group := x + 1
135+
ans += (v + group - 1) / group * group
136+
}
137+
return
138+
}
139+
```
140+
141+
#### TypeScript
142+
143+
```ts
144+
function numRabbits(answers: number[]): number {
145+
const cnt = new Map<number, number>();
146+
for (const x of answers) {
147+
cnt.set(x, (cnt.get(x) || 0) + 1);
148+
}
149+
let ans = 0;
150+
for (const [x, v] of cnt.entries()) {
151+
const group = x + 1;
152+
ans += Math.floor((v + group - 1) / group) * group;
91153
}
154+
return ans;
92155
}
93156
```
94157

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

+74-11
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Greedy + Hash Map
62+
63+
According to the problem description, rabbits that give the same answer may belong to the same color, while rabbits that give different answers cannot belong to the same color.
64+
65+
Therefore, we use a hash map $\textit{cnt}$ to record the number of occurrences of each answer. For each answer $x$ and its occurrence $v$, we calculate the minimum number of rabbits based on the principle that each color has $x + 1$ rabbits, and add it to the answer.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{answers}$.
6268

6369
<!-- tabs:start -->
6470

@@ -67,26 +73,83 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
6773
```python
6874
class Solution:
6975
def numRabbits(self, answers: List[int]) -> int:
70-
counter = Counter(answers)
71-
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
76+
cnt = Counter(answers)
77+
ans = 0
78+
for x, v in cnt.items():
79+
group = x + 1
80+
ans += (v + group - 1) // group * group
81+
return ans
7282
```
7383

7484
#### Java
7585

7686
```java
7787
class Solution {
7888
public int numRabbits(int[] answers) {
79-
Map<Integer, Integer> counter = new HashMap<>();
80-
for (int e : answers) {
81-
counter.put(e, counter.getOrDefault(e, 0) + 1);
89+
Map<Integer, Integer> cnt = new HashMap<>();
90+
for (int x : answers) {
91+
cnt.merge(x, 1, Integer::sum);
8292
}
83-
int res = 0;
84-
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
85-
int answer = entry.getKey(), count = entry.getValue();
86-
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
93+
int ans = 0;
94+
for (var e : cnt.entrySet()) {
95+
int group = e.getKey() + 1;
96+
ans += (e.getValue() + group - 1) / group * group;
97+
}
98+
return ans;
99+
}
100+
}
101+
```
102+
103+
#### C++
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int numRabbits(vector<int>& answers) {
109+
unordered_map<int, int> cnt;
110+
for (int x : answers) {
111+
++cnt[x];
87112
}
88-
return res;
113+
int ans = 0;
114+
for (auto& [x, v] : cnt) {
115+
int group = x + 1;
116+
ans += (v + group - 1) / group * group;
117+
}
118+
return ans;
119+
}
120+
};
121+
```
122+
123+
#### Go
124+
125+
```go
126+
func numRabbits(answers []int) (ans int) {
127+
cnt := map[int]int{}
128+
for _, x := range answers {
129+
cnt[x]++
130+
}
131+
for x, v := range cnt {
132+
group := x + 1
133+
ans += (v + group - 1) / group * group
134+
}
135+
return
136+
}
137+
```
138+
139+
#### TypeScript
140+
141+
```ts
142+
function numRabbits(answers: number[]): number {
143+
const cnt = new Map<number, number>();
144+
for (const x of answers) {
145+
cnt.set(x, (cnt.get(x) || 0) + 1);
146+
}
147+
let ans = 0;
148+
for (const [x, v] of cnt.entries()) {
149+
const group = x + 1;
150+
ans += Math.floor((v + group - 1) / group) * group;
89151
}
152+
return ans;
90153
}
91154
```
92155

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int numRabbits(vector<int>& answers) {
4+
unordered_map<int, int> cnt;
5+
for (int x : answers) {
6+
++cnt[x];
7+
}
8+
int ans = 0;
9+
for (auto& [x, v] : cnt) {
10+
int group = x + 1;
11+
ans += (v + group - 1) / group * group;
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func numRabbits(answers []int) (ans int) {
2+
cnt := map[int]int{}
3+
for _, x := range answers {
4+
cnt[x]++
5+
}
6+
for x, v := range cnt {
7+
group := x + 1
8+
ans += (v + group - 1) / group * group
9+
}
10+
return
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class Solution {
22
public int numRabbits(int[] answers) {
3-
Map<Integer, Integer> counter = new HashMap<>();
4-
for (int e : answers) {
5-
counter.put(e, counter.getOrDefault(e, 0) + 1);
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
for (int x : answers) {
5+
cnt.merge(x, 1, Integer::sum);
66
}
7-
int res = 0;
8-
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
9-
int answer = entry.getKey(), count = entry.getValue();
10-
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
7+
int ans = 0;
8+
for (var e : cnt.entrySet()) {
9+
int group = e.getKey() + 1;
10+
ans += (e.getValue() + group - 1) / group * group;
1111
}
12-
return res;
12+
return ans;
1313
}
14-
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Solution:
22
def numRabbits(self, answers: List[int]) -> int:
3-
counter = Counter(answers)
4-
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
3+
cnt = Counter(answers)
4+
ans = 0
5+
for x, v in cnt.items():
6+
group = x + 1
7+
ans += (v + group - 1) // group * group
8+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function numRabbits(answers: number[]): number {
2+
const cnt = new Map<number, number>();
3+
for (const x of answers) {
4+
cnt.set(x, (cnt.get(x) || 0) + 1);
5+
}
6+
let ans = 0;
7+
for (const [x, v] of cnt.entries()) {
8+
const group = x + 1;
9+
ans += Math.floor((v + group - 1) / group) * group;
10+
}
11+
return ans;
12+
}

solution/0700-0799/0790.Domino and Tromino Tiling/README.md

+9-48
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,16 @@ tags:
9595
```python
9696
class Solution:
9797
def numTilings(self, n: int) -> int:
98-
@cache
99-
def dfs(i, j):
100-
if i > n or j > n:
101-
return 0
102-
if i == n and j == n:
103-
return 1
104-
ans = 0
105-
if i == j:
106-
ans = (
107-
dfs(i + 2, j + 2)
108-
+ dfs(i + 1, j + 1)
109-
+ dfs(i + 2, j + 1)
110-
+ dfs(i + 1, j + 2)
111-
)
112-
elif i > j:
113-
ans = dfs(i, j + 2) + dfs(i + 1, j + 2)
114-
else:
115-
ans = dfs(i + 2, j) + dfs(i + 2, j + 1)
116-
return ans % mod
117-
98+
f = [1, 0, 0, 0]
11899
mod = 10**9 + 7
119-
return dfs(0, 0)
100+
for i in range(1, n + 1):
101+
g = [0] * 4
102+
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
103+
g[1] = (f[2] + f[3]) % mod
104+
g[2] = (f[1] + f[3]) % mod
105+
g[3] = f[0]
106+
f = g
107+
return f[0]
120108
```
121109

122110
#### Java
@@ -184,31 +172,4 @@ func numTilings(n int) int {
184172

185173
<!-- solution:end -->
186174

187-
<!-- solution:start -->
188-
189-
### 方法二
190-
191-
<!-- tabs:start -->
192-
193-
#### Python3
194-
195-
```python
196-
class Solution:
197-
def numTilings(self, n: int) -> int:
198-
f = [1, 0, 0, 0]
199-
mod = 10**9 + 7
200-
for i in range(1, n + 1):
201-
g = [0] * 4
202-
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
203-
g[1] = (f[2] + f[3]) % mod
204-
g[2] = (f[1] + f[3]) % mod
205-
g[3] = f[0]
206-
f = g
207-
return f[0]
208-
```
209-
210-
<!-- tabs:end -->
211-
212-
<!-- solution:end -->
213-
214175
<!-- problem:end -->

0 commit comments

Comments
 (0)