Skip to content
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
91 changes: 77 additions & 14 deletions solution/0700-0799/0781.Rabbits in Forest/README.md
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 All @@ -60,7 +60,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心 + 哈希表

根据题目描述,回答相同的兔子,可能属于同一种颜色,而回答不同的兔子,不可能属于同一种颜色。

因此,我们用一个哈希表 $\textit{cnt}$ 记录每种回答出现的次数。对于每种回答 $x$ 及其出现次数 $v$,我们按照每种颜色有 $x + 1$ 只兔子的原则,计算出兔子的最少数量,并将其加入答案。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{answers}$ 的长度。

<!-- tabs:start -->

Expand All @@ -69,26 +75,83 @@ tags:
```python
class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = Counter(answers)
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
cnt = Counter(answers)
ans = 0
for x, v in cnt.items():
group = x + 1
ans += (v + group - 1) // group * group
return ans
```

#### Java

```java
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : answers) {
cnt.merge(x, 1, Integer::sum);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
int ans = 0;
for (var e : cnt.entrySet()) {
int group = e.getKey() + 1;
ans += (e.getValue() + group - 1) / group * group;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> cnt;
for (int x : answers) {
++cnt[x];
}
return res;
int ans = 0;
for (auto& [x, v] : cnt) {
int group = x + 1;
ans += (v + group - 1) / group * group;
}
return ans;
}
};
```

#### Go

```go
func numRabbits(answers []int) (ans int) {
cnt := map[int]int{}
for _, x := range answers {
cnt[x]++
}
for x, v := range cnt {
group := x + 1
ans += (v + group - 1) / group * group
}
return
}
```

#### TypeScript

```ts
function numRabbits(answers: number[]): number {
const cnt = new Map<number, number>();
for (const x of answers) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = 0;
for (const [x, v] of cnt.entries()) {
const group = x + 1;
ans += Math.floor((v + group - 1) / group) * group;
}
return ans;
}
```

Expand Down
85 changes: 74 additions & 11 deletions solution/0700-0799/0781.Rabbits in Forest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Hash Map

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.

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{answers}$.

<!-- tabs:start -->

Expand All @@ -67,26 +73,83 @@ The smallest possible number of rabbits in the forest is therefore 5: 3 that ans
```python
class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = Counter(answers)
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
cnt = Counter(answers)
ans = 0
for x, v in cnt.items():
group = x + 1
ans += (v + group - 1) // group * group
return ans
```

#### Java

```java
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : answers) {
cnt.merge(x, 1, Integer::sum);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
int ans = 0;
for (var e : cnt.entrySet()) {
int group = e.getKey() + 1;
ans += (e.getValue() + group - 1) / group * group;
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> cnt;
for (int x : answers) {
++cnt[x];
}
return res;
int ans = 0;
for (auto& [x, v] : cnt) {
int group = x + 1;
ans += (v + group - 1) / group * group;
}
return ans;
}
};
```

#### Go

```go
func numRabbits(answers []int) (ans int) {
cnt := map[int]int{}
for _, x := range answers {
cnt[x]++
}
for x, v := range cnt {
group := x + 1
ans += (v + group - 1) / group * group
}
return
}
```

#### TypeScript

```ts
function numRabbits(answers: number[]): number {
const cnt = new Map<number, number>();
for (const x of answers) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = 0;
for (const [x, v] of cnt.entries()) {
const group = x + 1;
ans += Math.floor((v + group - 1) / group) * group;
}
return ans;
}
```

Expand Down
15 changes: 15 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int numRabbits(vector<int>& answers) {
unordered_map<int, int> cnt;
for (int x : answers) {
++cnt[x];
}
int ans = 0;
for (auto& [x, v] : cnt) {
int group = x + 1;
ans += (v + group - 1) / group * group;
}
return ans;
}
};
11 changes: 11 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func numRabbits(answers []int) (ans int) {
cnt := map[int]int{}
for _, x := range answers {
cnt[x]++
}
for x, v := range cnt {
group := x + 1
ans += (v + group - 1) / group * group
}
return
}
18 changes: 9 additions & 9 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Solution {
public int numRabbits(int[] answers) {
Map<Integer, Integer> counter = new HashMap<>();
for (int e : answers) {
counter.put(e, counter.getOrDefault(e, 0) + 1);
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : answers) {
cnt.merge(x, 1, Integer::sum);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
int answer = entry.getKey(), count = entry.getValue();
res += (int) Math.ceil(count / ((answer + 1) * 1.0)) * (answer + 1);
int ans = 0;
for (var e : cnt.entrySet()) {
int group = e.getKey() + 1;
ans += (e.getValue() + group - 1) / group * group;
}
return res;
return ans;
}
}
}
8 changes: 6 additions & 2 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Solution:
def numRabbits(self, answers: List[int]) -> int:
counter = Counter(answers)
return sum([math.ceil(v / (k + 1)) * (k + 1) for k, v in counter.items()])
cnt = Counter(answers)
ans = 0
for x, v in cnt.items():
group = x + 1
ans += (v + group - 1) // group * group
return ans
12 changes: 12 additions & 0 deletions solution/0700-0799/0781.Rabbits in Forest/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function numRabbits(answers: number[]): number {
const cnt = new Map<number, number>();
for (const x of answers) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
let ans = 0;
for (const [x, v] of cnt.entries()) {
const group = x + 1;
ans += Math.floor((v + group - 1) / group) * group;
}
return ans;
}
57 changes: 9 additions & 48 deletions solution/0700-0799/0790.Domino and Tromino Tiling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,16 @@ tags:
```python
class Solution:
def numTilings(self, n: int) -> int:
@cache
def dfs(i, j):
if i > n or j > n:
return 0
if i == n and j == n:
return 1
ans = 0
if i == j:
ans = (
dfs(i + 2, j + 2)
+ dfs(i + 1, j + 1)
+ dfs(i + 2, j + 1)
+ dfs(i + 1, j + 2)
)
elif i > j:
ans = dfs(i, j + 2) + dfs(i + 1, j + 2)
else:
ans = dfs(i + 2, j) + dfs(i + 2, j + 1)
return ans % mod

f = [1, 0, 0, 0]
mod = 10**9 + 7
return dfs(0, 0)
for i in range(1, n + 1):
g = [0] * 4
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
g[1] = (f[2] + f[3]) % mod
g[2] = (f[1] + f[3]) % mod
g[3] = f[0]
f = g
return f[0]
```

#### Java
Expand Down Expand Up @@ -184,31 +172,4 @@ func numTilings(n int) int {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def numTilings(self, n: int) -> int:
f = [1, 0, 0, 0]
mod = 10**9 + 7
for i in range(1, n + 1):
g = [0] * 4
g[0] = (f[0] + f[1] + f[2] + f[3]) % mod
g[1] = (f[2] + f[3]) % mod
g[2] = (f[1] + f[3]) % mod
g[3] = f[0]
f = g
return f[0]
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading