From 1205ff0995690e5451b58ef7c9be70041cbbbb95 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Tue, 18 Feb 2025 00:02:42 -0800 Subject: [PATCH 1/4] Create 3459.Length-of-Longest-V-Shaped-Diagonal-Segment.cpp --- ...h-of-Longest-V-Shaped-Diagonal-Segment.cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/3459.Length-of-Longest-V-Shaped-Diagonal-Segment.cpp diff --git a/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/3459.Length-of-Longest-V-Shaped-Diagonal-Segment.cpp b/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/3459.Length-of-Longest-V-Shaped-Diagonal-Segment.cpp new file mode 100644 index 000000000..630d01175 --- /dev/null +++ b/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/3459.Length-of-Longest-V-Shaped-Diagonal-Segment.cpp @@ -0,0 +1,59 @@ +class Solution { + vector>dir; + int memo[501][501][4][2]; +public: + int lenOfVDiagonal(vector>& grid) + { + int m = grid.size(), n = grid[0].size(); + int ret = 0; + dir = {{-1,1},{1,1},{1,-1},{-1,-1}}; + + for (int i=0; i=0 && i=0 && j>& grid, int x, int y, int k, int t) + { + if (memo[x][y][k][t]!=0) return memo[x][y][k][t]; + + int m = grid.size(), n = grid[0].size(); + int ret = 1; + + int i = x+dir[k].first, j = y+dir[k].second; + + if (inbound(i,j,m,n) && canContinue(grid[x][y], grid[i][j])) + ret = max(ret, 1 + dfs(grid,i,j,k,t)); + + if (t==1) + { + int kk=(k+1)%4; + i = x+dir[k].first, j = y+dir[k].second; + if (inbound(i,j,m,n) && canContinue(grid[x][y], grid[i][j])) + ret = max(ret, 1 + dfs(grid,i,j,kk,0)); + } + memo[x][y][k][t] = ret; + return ret; + } +}; From 02094c93dba6f18388e71e4bcd85d2012664164c Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Tue, 18 Feb 2025 00:03:08 -0800 Subject: [PATCH 2/4] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 7d6b6bfb4..e2b33d869 100644 --- a/Readme.md +++ b/Readme.md @@ -552,6 +552,7 @@ [2056.Number-of-Valid-Move-Combinations-On-Chessboard](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2056.Number-of-Valid-Move-Combinations-On-Chessboard) (H) [2065.Maximum-Path-Quality-of-a-Graph](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2065.Maximum-Path-Quality-of-a-Graph) (M) [2850.Minimum-Moves-to-Spread-Stones-Over-Grid](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2850.Minimum-Moves-to-Spread-Stones-Over-Grid) (M) +[3459.Length-of-Longest-V-Shaped-Diagonal-Segment](https://github.com/wisdompeak/LeetCode/tree/master/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment) (M+) * ``search in an array`` [090.Subsets-II](https://github.com/wisdompeak/LeetCode/tree/master/DFS/090.Subsets-II) (M+) [301.Remove-Invalid-Parentheses](https://github.com/wisdompeak/LeetCode/tree/master/DFS/301.Remove-Invalid-Parentheses) (H) From 0bba13046978502f24d5e026d695cb41eb6f86cf Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Tue, 18 Feb 2025 00:15:59 -0800 Subject: [PATCH 3/4] Create Readme.md --- .../Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/Readme.md diff --git a/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/Readme.md b/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/Readme.md new file mode 100644 index 000000000..4d9e572bd --- /dev/null +++ b/DFS/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/Readme.md @@ -0,0 +1,9 @@ +### 3459.Length-of-Longest-V-Shaped-Diagonal-Segment + +很常规的深度优先搜索。每个格子、每个方向只会进入一次。所以最多有`500*500*4=1e6`种状态。再加上有一次转弯的机会,所以2e6种状态是可以遍历和存储下来的。 + +定义dfs(x,y,k,t)表示以k的方向进入(x,y)的格子、且还有t次转弯机会时,还能走的最长路径。如果t==0,那么只能按照k的方向进入下一个(i1,j1);否则还可以考察按照k+1的方向进入下一个(i2,j2). + +注意进入的下一个各自(i,j)和(x,y)要满足数值上的约束,否则即可停止往下搜索。 + +此外,本题的记忆化根据四个参数进行记忆化也是必须的。 From b508b0d0ab68837013ca4c7bcdde71e634e5d2d2 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Tue, 18 Feb 2025 00:41:44 -0800 Subject: [PATCH 4/4] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e2b33d869..d5c8f42bd 100644 --- a/Readme.md +++ b/Readme.md @@ -1479,7 +1479,7 @@ [2781.Length-of-the-Longest-Valid-Substring](https://github.com/wisdompeak/LeetCode/tree/master/String/2781.Length-of-the-Longest-Valid-Substring) (H-) [3394.Check-if-Grid-can-be-Cut-into-Sections](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3394.Check-if-Grid-can-be-Cut-into-Sections) (M) [2271.Maximum-White-Tiles-Covered-by-a-Carpet](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2271.Maximum-White-Tiles-Covered-by-a-Carpet) (M+) -[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-) +[3413.Maximum-Coins-From-K-Consecutive-Bags](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3413.Maximum-Coins-From-K-Consecutive-Bags) (H-) 3104.Find Longest Self-Contained Substring (TBD) [1520.Maximum-Number-of-Non-Overlapping-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/1520.Maximum-Number-of-Non-Overlapping-Substrings) (H-) [3458.Select-K-Disjoint-Special-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/3458.Select-K-Disjoint-Special-Substrings) (H-)