Skip to content
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

feat: add solution to lc problem: No.3459 #4095

Merged
merged 1 commit into from
Feb 22, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,40 @@ tags:
#### Python3

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

def within_bounds(i, j):
return 0 <= i < m and 0 <= j < n

@cache
def f(i, j, di, dj, turned):
result = 1
successor = next_digit[grid[i][j]]

if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = 1 + f(i + di, j + dj, di, dj, turned)

if not turned:
di, dj = dj, -di
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = max(result, 1 + f(i + di, j + dj, di, dj, True))

return result

directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
result = 0

for i in range(m):
for j in range(n):
if grid[i][j] != 1:
continue
for di, dj in directions:
result = max(result, f(i, j, di, dj, False))

return result
```

#### Java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,40 @@ tags:
#### Python3

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

def within_bounds(i, j):
return 0 <= i < m and 0 <= j < n

@cache
def f(i, j, di, dj, turned):
result = 1
successor = next_digit[grid[i][j]]

if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = 1 + f(i + di, j + dj, di, dj, turned)

if not turned:
di, dj = dj, -di
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = max(result, 1 + f(i + di, j + dj, di, dj, True))

return result

directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
result = 0

for i in range(m):
for j in range(n):
if grid[i][j] != 1:
continue
for di, dj in directions:
result = max(result, f(i, j, di, dj, False))

return result
```

#### Java
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
next_digit = {1: 2, 2: 0, 0: 2}

def within_bounds(i, j):
return 0 <= i < m and 0 <= j < n

@cache
def f(i, j, di, dj, turned):
result = 1
successor = next_digit[grid[i][j]]

if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = 1 + f(i + di, j + dj, di, dj, turned)

if not turned:
di, dj = dj, -di
if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor:
result = max(result, 1 + f(i + di, j + dj, di, dj, True))

return result

directions = ((1, 1), (-1, 1), (1, -1), (-1, -1))
result = 0

for i in range(m):
for j in range(n):
if grid[i][j] != 1:
continue
for di, dj in directions:
result = max(result, f(i, j, di, dj, False))

return result