Skip to content

Commit e486c44

Browse files
author
Partho Biswas
committed
835. Image Overlap
1 parent 21009f6 commit e486c44

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ I have solved quite a number of problems from several topics. See the below tabl
157157
|49| **[289. Game of Life](https://tinyurl.com/y5ujyvu5)** | [Python](https://tinyurl.com/wu6rdaw/289_Game_of_Life.py), [Swift](https://tinyurl.com/wuja3c4/289_Game_of_Life.swift)| --- | Medium | --- |
158158
|50| **[54. Spiral Matrix](https://tinyurl.com/yy5rnvce)** | [Python](https://tinyurl.com/wu6rdaw/54_Spiral_Matrix.py), [Swift](https://tinyurl.com/wuja3c4/54_Spiral_Matrix.swift)| [Official](https://tinyurl.com/s6np53k) | Medium | --- |
159159
|51| **[380. Insert Delete GetRandom O(1)](https://tinyurl.com/y3urnkfj)** | [Python](https://tinyurl.com/wu6rdaw/380_Insert_Delete_GetRandom_O(1).py), [Swift](https://tinyurl.com/wuja3c4/380_Insert_Delete_GetRandom_O(1).swift)| [Official](https://tinyurl.com/t4t38od) | Medium | --- |
160+
|52| **[835. Image Overlap](https://tinyurl.com/r9x9nkm)** | [Python](https://tinyurl.com/wu6rdaw/835_Image_Overlap.py), [Swift](https://tinyurl.com/wuja3c4/835_Image_Overlap.swift)| [Art 1](https://tinyurl.com/tx3gd98), [Art 2](https://tinyurl.com/tnmj6du) | Medium | --- |
160161

161162

162163
</p>

leetcode.com/python/743_Network_Delay_Time.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import defaultdict
44

55
# """
6-
# Source: https://tinyurl.com/rmcr2xk. Uses simple DFS - Accepted
6+
# Source: https://tinyurl.com/rmcr2xk. Uses simple DFS - Time Limit Exceeded
77
class Solution(object):
88
def networkDelayTime(self, times, N, K):
99
graph = defaultdict(list)
@@ -15,7 +15,7 @@ def networkDelayTime(self, times, N, K):
1515
return totalTime if totalTime < float("inf") else -1
1616

1717
def DFS(self, graph, distance, node, elapsedTimeSoFar):
18-
if elapsedTimeSoFar >= distance[node]:
18+
if elapsedTimeSoFar >= distance[node]: # Here we are essentially checking if our existing/already calculated disstance is grater than out current culculated time. If our current path is greated than already traverssed path then this path is't going to give us any better solution. so we leave this path.
1919
return
2020
distance[node] = elapsedTimeSoFar
2121
for neighbour, time in sorted(graph[node]):
@@ -24,7 +24,7 @@ def DFS(self, graph, distance, node, elapsedTimeSoFar):
2424

2525

2626
# """
27-
# Source: https://tinyurl.com/vbetlaq. Uses simple BFS - Accepted
27+
# Source: https://tinyurl.com/vbetlaq. Uses simple BFS - Accepted. Here BFS if preferable over DFS
2828
class Solution:
2929
def networkDelayTime(self, times, N, K):
3030
elapsedTime, graph, queue = [0] + [float("inf")] * N, defaultdict(list), deque([(0, K)])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def largestOverlap(self, A, B):
4+
"""
5+
:type A: List[List[int]]
6+
:type B: List[List[int]]
7+
:rtype: int
8+
"""
9+
translationDistanceCount = defaultdict(int)
10+
a1Coordinates, b1Coordinates = [], []
11+
for i in range(len(A)):
12+
for j in range(len(A[0])):
13+
if A[i][j] == 1:
14+
a1Coordinates.append((i, j))
15+
if B[i][j] == 1:
16+
b1Coordinates.append((i, j))
17+
18+
for aRow, aCol in a1Coordinates:
19+
for bRow, bCol in b1Coordinates:
20+
translation = (bRow - aRow, bCol - aCol)
21+
translationDistanceCount[translation] += 1
22+
return max(translationDistanceCount.values()) if translationDistanceCount else 0

0 commit comments

Comments
 (0)