Skip to content

Commit f9d30c2

Browse files
committed
Runtime: 177 ms (Top 85.5%) | Memory: 16.58 MB (Top 51.9%)
1 parent d81eecb commit f9d30c2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Runtime: 177 ms (Top 85.5%) | Memory: 16.58 MB (Top 51.9%)
2+
3+
class Solution:
4+
def differenceOfDistinctValues(self, grid: List[List[int]]) -> List[List[int]]:
5+
6+
d = defaultdict(list)
7+
8+
for i, j in product(range(len(grid)),
9+
range(len(grid[0]))):
10+
d[i-j].append((i,j)) # <-- Construct dict of diagonals; i - j
11+
# remains constant on a '\' diag.
12+
for diag in d:
13+
arr = [grid[i][j] for i,j in d[diag]] # <-- Construct an arr of the elements
14+
# on the diagonal.
15+
16+
for idx, (i,j) in enumerate(d[diag]): # <-- Overwrite each element in the diagonal.
17+
grid[i][j] = abs(len(set(arr[:idx])) - # in the array with its score.
18+
len(set(arr[idx+1:])))
19+
20+
return grid # <-- Return the overwritten 'grid' as the
21+
# 'answer' matrix.

0 commit comments

Comments
 (0)