Skip to content

Commit c3fa0cf

Browse files
feat: add python solution to lc problem: No.3459 (#4095)
1 parent 245eb39 commit c3fa0cf

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,40 @@ tags:
119119
#### Python3
120120

121121
```python
122+
class Solution:
123+
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
124+
m, n = len(grid), len(grid[0])
125+
next_digit = {1: 2, 2: 0, 0: 2}
122126

127+
def within_bounds(i, j):
128+
return 0 <= i < m and 0 <= j < n
129+
130+
@cache
131+
def f(i, j, di, dj, turned):
132+
result = 1
133+
successor = next_digit[grid[i][j]]
134+
135+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
136+
result = 1 + f(i + di, j + dj, di, dj, turned)
137+
138+
if not turned:
139+
di, dj = dj, -di
140+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
141+
result = max(result, 1 + f(i + di, j + dj, di, dj, True))
142+
143+
return result
144+
145+
directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
146+
result = 0
147+
148+
for i in range(m):
149+
for j in range(n):
150+
if grid[i][j] != 1:
151+
continue
152+
for di, dj in directions:
153+
result = max(result, f(i, j, di, dj, False))
154+
155+
return result
123156
```
124157

125158
#### Java

solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,40 @@ tags:
117117
#### Python3
118118

119119
```python
120+
class Solution:
121+
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
122+
m, n = len(grid), len(grid[0])
123+
next_digit = {1: 2, 2: 0, 0: 2}
120124

125+
def within_bounds(i, j):
126+
return 0 <= i < m and 0 <= j < n
127+
128+
@cache
129+
def f(i, j, di, dj, turned):
130+
result = 1
131+
successor = next_digit[grid[i][j]]
132+
133+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
134+
result = 1 + f(i + di, j + dj, di, dj, turned)
135+
136+
if not turned:
137+
di, dj = dj, -di
138+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
139+
result = max(result, 1 + f(i + di, j + dj, di, dj, True))
140+
141+
return result
142+
143+
directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
144+
result = 0
145+
146+
for i in range(m):
147+
for j in range(n):
148+
if grid[i][j] != 1:
149+
continue
150+
for di, dj in directions:
151+
result = max(result, f(i, j, di, dj, False))
152+
153+
return result
121154
```
122155

123156
#### Java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
next_digit = {1: 2, 2: 0, 0: 2}
5+
6+
def within_bounds(i, j):
7+
return 0 <= i < m and 0 <= j < n
8+
9+
@cache
10+
def f(i, j, di, dj, turned):
11+
result = 1
12+
successor = next_digit[grid[i][j]]
13+
14+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
15+
result = 1 + f(i + di, j + dj, di, dj, turned)
16+
17+
if not turned:
18+
di, dj = dj, -di
19+
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
20+
result = max(result, 1 + f(i + di, j + dj, di, dj, True))
21+
22+
return result
23+
24+
directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
25+
result = 0
26+
27+
for i in range(m):
28+
for j in range(n):
29+
if grid[i][j] != 1:
30+
continue
31+
for di, dj in directions:
32+
result = max(result, f(i, j, di, dj, False))
33+
34+
return result

0 commit comments

Comments
 (0)