diff --git a/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README.md b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README.md index 27d543ba1e569..62c53551b4b51 100644 --- a/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README.md +++ b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README.md @@ -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 diff --git a/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README_EN.md b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README_EN.md index 29fad9abda361..30dfdb6482d2d 100644 --- a/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README_EN.md +++ b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/README_EN.md @@ -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 diff --git a/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/Solution.py b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/Solution.py new file mode 100644 index 0000000000000..b9caa8512361c --- /dev/null +++ b/solution/3400-3499/3459.Length of Longest V-Shaped Diagonal Segment/Solution.py @@ -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