Skip to content

feat: add solutions to lc problem: No.0835 #1365

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
merged 1 commit into from
Aug 1, 2023
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
125 changes: 124 additions & 1 deletion solution/0800-0899/0835.Image Overlap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,145 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:枚举**

我们可以枚举 $img1$ 和 $img2$ 的每个 $1$ 的位置,分别记为 $(i, j)$ 和 $(h, k)$。然后我们计算得到偏移量 $(i - h, j - k)$,记为 $(dx, dy)$,用哈希表 $cnt$ 记录每个偏移量出现的次数。最后我们遍历哈希表 $cnt$,找到出现次数最多的偏移量,即为答案。

时间复杂度 $O(n^4)$,空间复杂度 $O(n^2)$。其中 $n$ 是 $img1$ 的边长。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:
n = len(img1)
cnt = Counter()
for i in range(n):
for j in range(n):
if img1[i][j]:
for h in range(n):
for k in range(n):
if img2[h][k]:
cnt[(i - h, j - k)] += 1
return max(cnt.values()) if cnt else 0
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int largestOverlap(int[][] img1, int[][] img2) {
int n = img1.length;
Map<List<Integer>, Integer> cnt = new HashMap<>();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j] == 1) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k] == 1) {
List<Integer> t = List.of(i - h, j - k);
ans = Math.max(ans, cnt.merge(t, 1, Integer::sum));
}
}
}
}
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int largestOverlap(vector<vector<int>>& img1, vector<vector<int>>& img2) {
int n = img1.size();
map<pair<int, int>, int> cnt;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j]) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k]) {
ans = max(ans, ++cnt[{i - h, j - k}]);
}
}
}
}
}
}
return ans;
}
};
```

### **Go**

```go
func largestOverlap(img1 [][]int, img2 [][]int) (ans int) {
type pair struct{ x, y int }
cnt := map[pair]int{}
for i, row1 := range img1 {
for j, x1 := range row1 {
if x1 == 1 {
for h, row2 := range img2 {
for k, x2 := range row2 {
if x2 == 1 {
t := pair{i - h, j - k}
cnt[t]++
ans = max(ans, cnt[t])
}
}
}
}
}
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **TypeScript**

```ts
function largestOverlap(img1: number[][], img2: number[][]): number {
const n = img1.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (img1[i][j] === 1) {
for (let h = 0; h < n; ++h) {
for (let k = 0; k < n; ++k) {
if (img2[h][k] === 1) {
const t = (i - h) * 200 + (j - k);
cnt.set(t, (cnt.get(t) ?? 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
}
}
}
}
return ans;
}
```

### **...**
Expand Down
119 changes: 118 additions & 1 deletion solution/0800-0899/0835.Image Overlap/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,130 @@ The number of positions that have a 1 in both images is 3 (shown in red).
### **Python3**

```python

class Solution:
def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:
n = len(img1)
cnt = Counter()
for i in range(n):
for j in range(n):
if img1[i][j]:
for h in range(n):
for k in range(n):
if img2[h][k]:
cnt[(i - h, j - k)] += 1
return max(cnt.values()) if cnt else 0
```

### **Java**

```java
class Solution {
public int largestOverlap(int[][] img1, int[][] img2) {
int n = img1.length;
Map<List<Integer>, Integer> cnt = new HashMap<>();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j] == 1) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k] == 1) {
List<Integer> t = List.of(i - h, j - k);
ans = Math.max(ans, cnt.merge(t, 1, Integer::sum));
}
}
}
}
}
}
return ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int largestOverlap(vector<vector<int>>& img1, vector<vector<int>>& img2) {
int n = img1.size();
map<pair<int, int>, int> cnt;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j]) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k]) {
ans = max(ans, ++cnt[{i - h, j - k}]);
}
}
}
}
}
}
return ans;
}
};
```

### **Go**

```go
func largestOverlap(img1 [][]int, img2 [][]int) (ans int) {
type pair struct{ x, y int }
cnt := map[pair]int{}
for i, row1 := range img1 {
for j, x1 := range row1 {
if x1 == 1 {
for h, row2 := range img2 {
for k, x2 := range row2 {
if x2 == 1 {
t := pair{i - h, j - k}
cnt[t]++
ans = max(ans, cnt[t])
}
}
}
}
}
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **TypeScript**

```ts
function largestOverlap(img1: number[][], img2: number[][]): number {
const n = img1.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (img1[i][j] === 1) {
for (let h = 0; h < n; ++h) {
for (let k = 0; k < n; ++k) {
if (img2[h][k] === 1) {
const t = (i - h) * 200 + (j - k);
cnt.set(t, (cnt.get(t) ?? 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
}
}
}
}
return ans;
}
```

### **...**
Expand Down
22 changes: 22 additions & 0 deletions solution/0800-0899/0835.Image Overlap/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int largestOverlap(vector<vector<int>>& img1, vector<vector<int>>& img2) {
int n = img1.size();
map<pair<int, int>, int> cnt;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j]) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k]) {
ans = max(ans, ++cnt[{i - h, j - k}]);
}
}
}
}
}
}
return ans;
}
};
27 changes: 27 additions & 0 deletions solution/0800-0899/0835.Image Overlap/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func largestOverlap(img1 [][]int, img2 [][]int) (ans int) {
type pair struct{ x, y int }
cnt := map[pair]int{}
for i, row1 := range img1 {
for j, x1 := range row1 {
if x1 == 1 {
for h, row2 := range img2 {
for k, x2 := range row2 {
if x2 == 1 {
t := pair{i - h, j - k}
cnt[t]++
ans = max(ans, cnt[t])
}
}
}
}
}
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
22 changes: 22 additions & 0 deletions solution/0800-0899/0835.Image Overlap/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int largestOverlap(int[][] img1, int[][] img2) {
int n = img1.length;
Map<List<Integer>, Integer> cnt = new HashMap<>();
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (img1[i][j] == 1) {
for (int h = 0; h < n; ++h) {
for (int k = 0; k < n; ++k) {
if (img2[h][k] == 1) {
List<Integer> t = List.of(i - h, j - k);
ans = Math.max(ans, cnt.merge(t, 1, Integer::sum));
}
}
}
}
}
}
return ans;
}
}
12 changes: 12 additions & 0 deletions solution/0800-0899/0835.Image Overlap/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def largestOverlap(self, img1: List[List[int]], img2: List[List[int]]) -> int:
n = len(img1)
cnt = Counter()
for i in range(n):
for j in range(n):
if img1[i][j]:
for h in range(n):
for k in range(n):
if img2[h][k]:
cnt[(i - h, j - k)] += 1
return max(cnt.values()) if cnt else 0
21 changes: 21 additions & 0 deletions solution/0800-0899/0835.Image Overlap/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function largestOverlap(img1: number[][], img2: number[][]): number {
const n = img1.length;
const cnt: Map<number, number> = new Map();
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
if (img1[i][j] === 1) {
for (let h = 0; h < n; ++h) {
for (let k = 0; k < n; ++k) {
if (img2[h][k] === 1) {
const t = (i - h) * 200 + (j - k);
cnt.set(t, (cnt.get(t) ?? 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
}
}
}
}
return ans;
}