From bb40ea1ffff57a538a3078eb39e544c64205f96a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 10 Jul 2023 21:14:48 +0800 Subject: [PATCH 1/2] feat: update sql solutions to lc problems --- .../0601.Human Traffic of Stadium/README.md | 15 +++++------ .../README_EN.md | 15 +++++------ .../Solution.sql | 15 +++++------ .../README.md | 26 ++++++++----------- .../README_EN.md | 26 ++++++++----------- .../Solution.sql | 26 ++++++++----------- .../README.md | 20 +++++++++++--- .../README_EN.md | 20 +++++++++++--- .../Solution.sql | 17 +++++++----- .../0600-0699/0607.Sales Person/README.md | 15 +++++++++++ .../0600-0699/0607.Sales Person/README_EN.md | 15 +++++++++++ solution/0600-0699/0608.Tree Node/README.md | 26 +++---------------- .../0600-0699/0608.Tree Node/README_EN.md | 26 +++---------------- .../0600-0699/0608.Tree Node/Solution.sql | 18 +++++-------- .../README.md | 8 ++++++ .../README_EN.md | 8 ++++++ .../Solution.sql | 9 +++---- .../0614.Second Degree Follower/README.md | 19 ++++++++------ .../0614.Second Degree Follower/README_EN.md | 19 ++++++++------ .../0614.Second Degree Follower/Solution.sql | 19 ++++++++------ 20 files changed, 195 insertions(+), 167 deletions(-) diff --git a/solution/0600-0699/0601.Human Traffic of Stadium/README.md b/solution/0600-0699/0601.Human Traffic of Stadium/README.md index 889cb6f2844e7..66947e4133a32 100644 --- a/solution/0600-0699/0601.Human Traffic of Stadium/README.md +++ b/solution/0600-0699/0601.Human Traffic of Stadium/README.md @@ -73,19 +73,18 @@ id 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 ```sql # Write your MySQL query statement below WITH - s AS ( - SELECT *, id - row_number() OVER (ORDER BY id) AS rk + S AS ( + SELECT + *, + id - (row_number() OVER (ORDER BY id)) AS rk FROM Stadium WHERE people >= 100 ), - t AS ( - SELECT *, count(*) OVER (PARTITION BY rk) AS cnt - FROM s - ) + T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S) SELECT id, visit_date, people -FROM t +FROM T WHERE cnt >= 3 -ORDER BY visit_date; +ORDER BY 1; ``` diff --git a/solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md b/solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md index 073a97f988e64..ca8eccf97a58b 100644 --- a/solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md +++ b/solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md @@ -68,19 +68,18 @@ The rows with ids 2 and 3 are not included because we need at least three consec ```sql # Write your MySQL query statement below WITH - s AS ( - SELECT *, id - row_number() OVER (ORDER BY id) AS rk + S AS ( + SELECT + *, + id - (row_number() OVER (ORDER BY id)) AS rk FROM Stadium WHERE people >= 100 ), - t AS ( - SELECT *, count(*) OVER (PARTITION BY rk) AS cnt - FROM s - ) + T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S) SELECT id, visit_date, people -FROM t +FROM T WHERE cnt >= 3 -ORDER BY visit_date; +ORDER BY 1; ``` diff --git a/solution/0600-0699/0601.Human Traffic of Stadium/Solution.sql b/solution/0600-0699/0601.Human Traffic of Stadium/Solution.sql index 4c18e2e38a58c..3b95415d2e5cc 100644 --- a/solution/0600-0699/0601.Human Traffic of Stadium/Solution.sql +++ b/solution/0600-0699/0601.Human Traffic of Stadium/Solution.sql @@ -1,15 +1,14 @@ # Write your MySQL query statement below WITH - s AS ( - SELECT *, id - row_number() OVER (ORDER BY id) AS rk + S AS ( + SELECT + *, + id - (row_number() OVER (ORDER BY id)) AS rk FROM Stadium WHERE people >= 100 ), - t AS ( - SELECT *, count(*) OVER (PARTITION BY rk) AS cnt - FROM s - ) + T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S) SELECT id, visit_date, people -FROM t +FROM T WHERE cnt >= 3 -ORDER BY visit_date; +ORDER BY 1; diff --git a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md index 4a96c2cb5b0d5..a37ab854d45ea 100644 --- a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md +++ b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md @@ -71,21 +71,17 @@ RequestAccepted 表: ### **SQL** ```sql -SELECT - ids AS id, - COUNT(*) AS num -FROM - ( - SELECT - requester_id AS ids - FROM RequestAccepted - UNION ALL - SELECT - accepter_id - FROM RequestAccepted - ) AS t -GROUP BY ids -ORDER BY num DESC +# Write your MySQL query statement below +WITH + T AS ( + SELECT requester_id, accepter_id FROM RequestAccepted + UNION + SELECT accepter_id, requester_id FROM RequestAccepted + ) +SELECT requester_id AS id, count(accepter_id) AS num +FROM T +GROUP BY 1 +ORDER BY 2 DESC LIMIT 1; ``` diff --git a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md index 31caeee369c05..731146bfaa6a8 100644 --- a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md +++ b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md @@ -60,21 +60,17 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends ### **SQL** ```sql -SELECT - ids AS id, - COUNT(*) AS num -FROM - ( - SELECT - requester_id AS ids - FROM RequestAccepted - UNION ALL - SELECT - accepter_id - FROM RequestAccepted - ) AS t -GROUP BY ids -ORDER BY num DESC +# Write your MySQL query statement below +WITH + T AS ( + SELECT requester_id, accepter_id FROM RequestAccepted + UNION + SELECT accepter_id, requester_id FROM RequestAccepted + ) +SELECT requester_id AS id, count(accepter_id) AS num +FROM T +GROUP BY 1 +ORDER BY 2 DESC LIMIT 1; ``` diff --git a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/Solution.sql b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/Solution.sql index f1cb34fe40acd..2cbbdb5c6b300 100644 --- a/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/Solution.sql +++ b/solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/Solution.sql @@ -1,16 +1,12 @@ -SELECT - ids AS id, - COUNT(*) AS num -FROM - ( - SELECT - requester_id AS ids - FROM RequestAccepted - UNION ALL - SELECT - accepter_id - FROM RequestAccepted - ) AS t -GROUP BY ids -ORDER BY num DESC +# Write your MySQL query statement below +WITH + T AS ( + SELECT requester_id, accepter_id FROM RequestAccepted + UNION + SELECT accepter_id, requester_id FROM RequestAccepted + ) +SELECT requester_id AS id, count(accepter_id) AS num +FROM T +GROUP BY 1 +ORDER BY 2 DESC LIMIT 1; diff --git a/solution/0600-0699/0603.Consecutive Available Seats/README.md b/solution/0600-0699/0603.Consecutive Available Seats/README.md index a5eff4352cffe..ab6bce298b918 100644 --- a/solution/0600-0699/0603.Consecutive Available Seats/README.md +++ b/solution/0600-0699/0603.Consecutive Available Seats/README.md @@ -66,9 +66,23 @@ Cinema 表: SELECT DISTINCT a.seat_id FROM Cinema AS a - JOIN Cinema AS b - ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE -ORDER BY a.seat_id; + JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free +ORDER BY 1; +``` + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + seat_id, + (free + (lag(free) OVER (ORDER BY seat_id))) AS a, + (free + (lead(free) OVER (ORDER BY seat_id))) AS b + FROM Cinema + ) +SELECT seat_id +FROM T +WHERE a = 2 OR b = 2; ``` diff --git a/solution/0600-0699/0603.Consecutive Available Seats/README_EN.md b/solution/0600-0699/0603.Consecutive Available Seats/README_EN.md index a8623e767242e..3723397853cf8 100644 --- a/solution/0600-0699/0603.Consecutive Available Seats/README_EN.md +++ b/solution/0600-0699/0603.Consecutive Available Seats/README_EN.md @@ -63,9 +63,23 @@ Cinema table: SELECT DISTINCT a.seat_id FROM Cinema AS a - JOIN Cinema AS b - ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE -ORDER BY a.seat_id; + JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free +ORDER BY 1; +``` + +```sql +# Write your MySQL query statement below +WITH + T AS ( + SELECT + seat_id, + (free + (lag(free) OVER (ORDER BY seat_id))) AS a, + (free + (lead(free) OVER (ORDER BY seat_id))) AS b + FROM Cinema + ) +SELECT seat_id +FROM T +WHERE a = 2 OR b = 2; ``` diff --git a/solution/0600-0699/0603.Consecutive Available Seats/Solution.sql b/solution/0600-0699/0603.Consecutive Available Seats/Solution.sql index 8f1de58bfd321..259bea7a4e2e2 100644 --- a/solution/0600-0699/0603.Consecutive Available Seats/Solution.sql +++ b/solution/0600-0699/0603.Consecutive Available Seats/Solution.sql @@ -1,7 +1,12 @@ # Write your MySQL query statement below -SELECT DISTINCT a.seat_id -FROM - Cinema AS a - JOIN Cinema AS b - ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE -ORDER BY a.seat_id; +WITH + T AS ( + SELECT + seat_id, + (free + (lag(free) OVER (ORDER BY seat_id))) AS a, + (free + (lead(free) OVER (ORDER BY seat_id))) AS b + FROM Cinema + ) +SELECT seat_id +FROM T +WHERE a = 2 OR b = 2; diff --git a/solution/0600-0699/0607.Sales Person/README.md b/solution/0600-0699/0607.Sales Person/README.md index 9533a56abec90..0f19638099ddc 100644 --- a/solution/0600-0699/0607.Sales Person/README.md +++ b/solution/0600-0699/0607.Sales Person/README.md @@ -149,4 +149,19 @@ WHERE ); ``` +```sql +# Write your MySQL query statement below +SELECT name +FROM SalesPerson +WHERE + sales_id NOT IN ( + SELECT sales_id + FROM + Company AS c + JOIN Orders AS o USING (com_id) + GROUP BY sales_id + HAVING sum(name = 'RED') > 0 + ); +``` + diff --git a/solution/0600-0699/0607.Sales Person/README_EN.md b/solution/0600-0699/0607.Sales Person/README_EN.md index 1e14283587146..20de0a1a0f74e 100644 --- a/solution/0600-0699/0607.Sales Person/README_EN.md +++ b/solution/0600-0699/0607.Sales Person/README_EN.md @@ -144,4 +144,19 @@ WHERE ); ``` +```sql +# Write your MySQL query statement below +SELECT name +FROM SalesPerson +WHERE + sales_id NOT IN ( + SELECT sales_id + FROM + Company AS c + JOIN Orders AS o USING (com_id) + GROUP BY sales_id + HAVING sum(name = 'RED') > 0 + ); +``` + diff --git a/solution/0600-0699/0608.Tree Node/README.md b/solution/0600-0699/0608.Tree Node/README.md index 8e44248c5f0c8..f61bf6772bc26 100644 --- a/solution/0600-0699/0608.Tree Node/README.md +++ b/solution/0600-0699/0608.Tree Node/README.md @@ -79,36 +79,16 @@ ### **SQL** -```sql -SELECT - id, - ( - CASE - WHEN p_id IS NULL THEN 'Root' - WHEN id IN ( - SELECT p_id - FROM tree - ) THEN 'Inner' - ELSE 'Leaf' - END - ) AS type -FROM tree; -``` - ```sql # Write your MySQL query statement below SELECT id, CASE WHEN p_id IS NULL THEN 'Root' - WHEN id IN ( - SELECT - p_id - FROM tree - ) THEN 'Inner' + WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner' ELSE 'Leaf' - END AS Type -FROM tree; + END AS type +FROM Tree; ``` diff --git a/solution/0600-0699/0608.Tree Node/README_EN.md b/solution/0600-0699/0608.Tree Node/README_EN.md index 59b35f75b0e95..7bd9abc66e2dd 100644 --- a/solution/0600-0699/0608.Tree Node/README_EN.md +++ b/solution/0600-0699/0608.Tree Node/README_EN.md @@ -90,36 +90,16 @@ Tree table: ### **SQL** -```sql -SELECT - id, - ( - CASE - WHEN p_id IS NULL THEN 'Root' - WHEN id IN ( - SELECT p_id - FROM tree - ) THEN 'Inner' - ELSE 'Leaf' - END - ) AS type -FROM tree; -``` - ```sql # Write your MySQL query statement below SELECT id, CASE WHEN p_id IS NULL THEN 'Root' - WHEN id IN ( - SELECT - p_id - FROM tree - ) THEN 'Inner' + WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner' ELSE 'Leaf' - END AS Type -FROM tree; + END AS type +FROM Tree; ``` diff --git a/solution/0600-0699/0608.Tree Node/Solution.sql b/solution/0600-0699/0608.Tree Node/Solution.sql index 796c695b1a20b..1594b6e56d36b 100644 --- a/solution/0600-0699/0608.Tree Node/Solution.sql +++ b/solution/0600-0699/0608.Tree Node/Solution.sql @@ -1,13 +1,9 @@ +# Write your MySQL query statement below SELECT id, - ( - CASE - WHEN p_id IS NULL THEN 'Root' - WHEN id IN ( - SELECT p_id - FROM tree - ) THEN 'Inner' - ELSE 'Leaf' - END - ) AS type -FROM tree; + CASE + WHEN p_id IS NULL THEN 'Root' + WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner' + ELSE 'Leaf' + END AS type +FROM Tree; diff --git a/solution/0600-0699/0613.Shortest Distance in a Line/README.md b/solution/0600-0699/0613.Shortest Distance in a Line/README.md index 9a9bc41eac9cd..2b3c86b211481 100644 --- a/solution/0600-0699/0613.Shortest Distance in a Line/README.md +++ b/solution/0600-0699/0613.Shortest Distance in a Line/README.md @@ -59,4 +59,12 @@ FROM JOIN Point AS p2 ON p1.x != p2.x; ``` +```sql +# Write your MySQL query statement below +SELECT x - lag(x) OVER (ORDER BY x) AS shortest +FROM Point +ORDER BY 1 +LIMIT 1, 1; +``` + diff --git a/solution/0600-0699/0613.Shortest Distance in a Line/README_EN.md b/solution/0600-0699/0613.Shortest Distance in a Line/README_EN.md index 5ec59d1be84b2..7ff960c99526e 100644 --- a/solution/0600-0699/0613.Shortest Distance in a Line/README_EN.md +++ b/solution/0600-0699/0613.Shortest Distance in a Line/README_EN.md @@ -62,4 +62,12 @@ FROM JOIN Point AS p2 ON p1.x != p2.x; ``` +```sql +# Write your MySQL query statement below +SELECT x - lag(x) OVER (ORDER BY x) AS shortest +FROM Point +ORDER BY 1 +LIMIT 1, 1; +``` + diff --git a/solution/0600-0699/0613.Shortest Distance in a Line/Solution.sql b/solution/0600-0699/0613.Shortest Distance in a Line/Solution.sql index 00f4ea870c10a..905278e8fc265 100644 --- a/solution/0600-0699/0613.Shortest Distance in a Line/Solution.sql +++ b/solution/0600-0699/0613.Shortest Distance in a Line/Solution.sql @@ -1,6 +1,5 @@ # Write your MySQL query statement below -SELECT - min(abs(p1.x - p2.x)) AS shortest -FROM - Point AS p1 - JOIN Point AS p2 ON p1.x != p2.x; +SELECT x - lag(x) OVER (ORDER BY x) AS shortest +FROM Point +ORDER BY 1 +LIMIT 1, 1; diff --git a/solution/0600-0699/0614.Second Degree Follower/README.md b/solution/0600-0699/0614.Second Degree Follower/README.md index 89e8a733e6fd6..39e9a8b890b15 100644 --- a/solution/0600-0699/0614.Second Degree Follower/README.md +++ b/solution/0600-0699/0614.Second Degree Follower/README.md @@ -57,14 +57,17 @@ ```sql # Write your MySQL query statement below -SELECT - f1.follower, - COUNT(DISTINCT f2.follower) AS num -FROM - follow AS f1 - JOIN follow AS f2 ON f1.follower = f2.followee -GROUP BY f1.follower -ORDER BY f1.follower; +WITH + T AS ( + SELECT f1.follower AS follower, f2.follower AS followee + FROM + Follow AS f1 + JOIN Follow AS f2 ON f1.follower = f2.followee + ) +SELECT follower, count(DISTINCT followee) AS num +FROM T +GROUP BY 1 +ORDER BY 1; ``` diff --git a/solution/0600-0699/0614.Second Degree Follower/README_EN.md b/solution/0600-0699/0614.Second Degree Follower/README_EN.md index f0369c93efbfb..468ee04d242bc 100644 --- a/solution/0600-0699/0614.Second Degree Follower/README_EN.md +++ b/solution/0600-0699/0614.Second Degree Follower/README_EN.md @@ -68,14 +68,17 @@ User Alice has 1 follower. Alice is not a second-degree follower because she doe ```sql # Write your MySQL query statement below -SELECT - f1.follower, - COUNT(DISTINCT f2.follower) AS num -FROM - follow AS f1 - JOIN follow AS f2 ON f1.follower = f2.followee -GROUP BY f1.follower -ORDER BY f1.follower; +WITH + T AS ( + SELECT f1.follower AS follower, f2.follower AS followee + FROM + Follow AS f1 + JOIN Follow AS f2 ON f1.follower = f2.followee + ) +SELECT follower, count(DISTINCT followee) AS num +FROM T +GROUP BY 1 +ORDER BY 1; ``` diff --git a/solution/0600-0699/0614.Second Degree Follower/Solution.sql b/solution/0600-0699/0614.Second Degree Follower/Solution.sql index 981418670ee8b..7fc84d6e07401 100644 --- a/solution/0600-0699/0614.Second Degree Follower/Solution.sql +++ b/solution/0600-0699/0614.Second Degree Follower/Solution.sql @@ -1,9 +1,12 @@ # Write your MySQL query statement below -SELECT - f1.follower, - COUNT(DISTINCT f2.follower) AS num -FROM - follow AS f1 - JOIN follow AS f2 ON f1.follower = f2.followee -GROUP BY f1.follower -ORDER BY f1.follower; +WITH + T AS ( + SELECT f1.follower AS follower, f2.follower AS followee + FROM + Follow AS f1 + JOIN Follow AS f2 ON f1.follower = f2.followee + ) +SELECT follower, count(DISTINCT followee) AS num +FROM T +GROUP BY 1 +ORDER BY 1; From be4ae6cd577c2e099a9a2160ae53968cd5fe15bd Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 10 Jul 2023 22:03:40 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.0931 * No.0931.Minimum Falling Path Sum --- .../0931.Minimum Falling Path Sum/README.md | 135 ++++++++++++------ .../README_EN.md | 121 ++++++++++------ .../Solution.cpp | 38 ++--- .../0931.Minimum Falling Path Sum/Solution.go | 36 +++-- .../Solution.java | 45 +++--- .../0931.Minimum Falling Path Sum/Solution.py | 23 ++- .../0931.Minimum Falling Path Sum/Solution.ts | 18 +++ 7 files changed, 264 insertions(+), 152 deletions(-) create mode 100644 solution/0900-0999/0931.Minimum Falling Path Sum/Solution.ts diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/README.md b/solution/0900-0999/0931.Minimum Falling Path Sum/README.md index e5d1db010668b..853f667cf91ac 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/README.md +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/README.md @@ -46,7 +46,19 @@ -动态规划。 +**方法一:动态规划** + +我们定义 $f[i][j]$ 表示从第一行开始下降,到达第 $i$ 行第 $j$ 列的最小路径和。那么我们可以得到这样的动态规划转移方程: + +$$ +f[i][j] = matrix[i][j] + \min \left\{ \begin{aligned} & f[i - 1][j - 1], & j > 0 \\ & f[i - 1][j], & 0 \leq j < n \\ & f[i - 1][j + 1], & j + 1 < n \end{aligned} \right. +$$ + +最终的答案即为 $\min \limits_{0 \leq j < n} f[n - 1][j]$。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。 + +我们注意到,状态 $f[i][j]$ 只与上一行的状态有关,因此我们可以使用滚动数组的方式,去掉第一维的状态,将空间复杂度优化到 $O(n)$。 @@ -58,15 +70,14 @@ class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) - for i in range(1, n): - for j in range(n): - mi = matrix[i - 1][j] - if j > 0: - mi = min(mi, matrix[i - 1][j - 1]) - if j < n - 1: - mi = min(mi, matrix[i - 1][j + 1]) - matrix[i][j] += mi - return min(matrix[n - 1]) + f = [0] * n + for row in matrix: + g = [0] * n + for j, x in enumerate(row): + l, r = max(0, j - 1), min(n, j + 2) + g[j] = min(f[l:r]) + x + f = g + return min(f) ``` ### **Java** @@ -77,23 +88,26 @@ class Solution: class Solution { public int minFallingPathSum(int[][] matrix) { int n = matrix.length; - for (int i = 1; i < n; ++i) { + var f = new int[n]; + for (var row : matrix) { + var g = f.clone(); for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; if (j > 0) { - mi = Math.min(mi, matrix[i - 1][j - 1]); + g[j] = Math.min(g[j], f[j - 1]); } - if (j < n - 1) { - mi = Math.min(mi, matrix[i - 1][j + 1]); + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); } - matrix[i][j] += mi; + g[j] += row[j]; } + f = g; } - int res = Integer.MAX_VALUE; - for (int j = 0; j < n; ++j) { - res = Math.min(res, matrix[n - 1][j]); + // return Arrays.stream(f).min().getAsInt(); + int ans = 1 << 30; + for (int x : f) { + ans = Math.min(ans, x); } - return res; + return ans; } } ``` @@ -105,19 +119,21 @@ class Solution { public: int minFallingPathSum(vector>& matrix) { int n = matrix.size(); - for (int i = 1; i < n; ++i) { + vector f(n); + for (auto& row : matrix) { + auto g = f; for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; - if (j > 0) mi = min(mi, matrix[i - 1][j - 1]); - if (j < n - 1) mi = min(mi, matrix[i - 1][j + 1]); - matrix[i][j] += mi; + if (j) { + g[j] = min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = min(g[j], f[j + 1]); + } + g[j] += row[j]; } + f = move(g); } - int res = INT_MAX; - for (int j = 0; j < n; ++j) { - res = min(res, matrix[n - 1][j]); - } - return res; + return *min_element(f.begin(), f.end()); } }; ``` @@ -127,25 +143,56 @@ public: ```go func minFallingPathSum(matrix [][]int) int { n := len(matrix) - for i := 1; i < n; i++ { - for j := 0; j < n; j++ { - mi := matrix[i-1][j] - if j > 0 && mi > matrix[i-1][j-1] { - mi = matrix[i-1][j-1] + f := make([]int, n) + for _, row := range matrix { + g := make([]int, n) + copy(g, f) + for j, x := range row { + if j > 0 { + g[j] = min(g[j], f[j-1]) } - if j < n-1 && mi > matrix[i-1][j+1] { - mi = matrix[i-1][j+1] + if j+1 < n { + g[j] = min(g[j], f[j+1]) } - matrix[i][j] += mi + g[j] += x } + f = g } - res := 10000 - for j := 0; j < n; j++ { - if res > matrix[n-1][j] { - res = matrix[n-1][j] - } + ans := 1 << 30 + for _, x := range f { + ans = min(ans, x) } - return res + return ans +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` + +### **TypeScript** + +```ts +function minFallingPathSum(matrix: number[][]): number { + const n = matrix.length; + const f: number[] = new Array(n).fill(0); + for (const row of matrix) { + const g = f.slice(); + for (let j = 0; j < n; ++j) { + if (j > 0) { + g[j] = Math.min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); + } + g[j] += row[j]; + } + f.splice(0, n, ...g); + } + return Math.min(...f); } ``` diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/README_EN.md b/solution/0900-0999/0931.Minimum Falling Path Sum/README_EN.md index 051e8d168fb08..b66b9112a3e20 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/README_EN.md +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/README_EN.md @@ -46,15 +46,14 @@ Dynamic programming. class Solution: def minFallingPathSum(self, matrix: List[List[int]]) -> int: n = len(matrix) - for i in range(1, n): - for j in range(n): - mi = matrix[i - 1][j] - if j > 0: - mi = min(mi, matrix[i - 1][j - 1]) - if j < n - 1: - mi = min(mi, matrix[i - 1][j + 1]) - matrix[i][j] += mi - return min(matrix[n - 1]) + f = [0] * n + for row in matrix: + g = [0] * n + for j, x in enumerate(row): + l, r = max(0, j - 1), min(n, j + 2) + g[j] = min(f[l:r]) + x + f = g + return min(f) ``` ### **Java** @@ -63,23 +62,26 @@ class Solution: class Solution { public int minFallingPathSum(int[][] matrix) { int n = matrix.length; - for (int i = 1; i < n; ++i) { + var f = new int[n]; + for (var row : matrix) { + var g = f.clone(); for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; if (j > 0) { - mi = Math.min(mi, matrix[i - 1][j - 1]); + g[j] = Math.min(g[j], f[j - 1]); } - if (j < n - 1) { - mi = Math.min(mi, matrix[i - 1][j + 1]); + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); } - matrix[i][j] += mi; + g[j] += row[j]; } + f = g; } - int res = Integer.MAX_VALUE; - for (int j = 0; j < n; ++j) { - res = Math.min(res, matrix[n - 1][j]); + // return Arrays.stream(f).min().getAsInt(); + int ans = 1 << 30; + for (int x : f) { + ans = Math.min(ans, x); } - return res; + return ans; } } ``` @@ -91,19 +93,21 @@ class Solution { public: int minFallingPathSum(vector>& matrix) { int n = matrix.size(); - for (int i = 1; i < n; ++i) { + vector f(n); + for (auto& row : matrix) { + auto g = f; for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; - if (j > 0) mi = min(mi, matrix[i - 1][j - 1]); - if (j < n - 1) mi = min(mi, matrix[i - 1][j + 1]); - matrix[i][j] += mi; + if (j) { + g[j] = min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = min(g[j], f[j + 1]); + } + g[j] += row[j]; } + f = move(g); } - int res = INT_MAX; - for (int j = 0; j < n; ++j) { - res = min(res, matrix[n - 1][j]); - } - return res; + return *min_element(f.begin(), f.end()); } }; ``` @@ -113,25 +117,56 @@ public: ```go func minFallingPathSum(matrix [][]int) int { n := len(matrix) - for i := 1; i < n; i++ { - for j := 0; j < n; j++ { - mi := matrix[i-1][j] - if j > 0 && mi > matrix[i-1][j-1] { - mi = matrix[i-1][j-1] + f := make([]int, n) + for _, row := range matrix { + g := make([]int, n) + copy(g, f) + for j, x := range row { + if j > 0 { + g[j] = min(g[j], f[j-1]) } - if j < n-1 && mi > matrix[i-1][j+1] { - mi = matrix[i-1][j+1] + if j+1 < n { + g[j] = min(g[j], f[j+1]) } - matrix[i][j] += mi + g[j] += x } + f = g } - res := 10000 - for j := 0; j < n; j++ { - if res > matrix[n-1][j] { - res = matrix[n-1][j] - } + ans := 1 << 30 + for _, x := range f { + ans = min(ans, x) + } + return ans +} + +func min(a, b int) int { + if a < b { + return a } - return res + return b +} +``` + +### **TypeScript** + +```ts +function minFallingPathSum(matrix: number[][]): number { + const n = matrix.length; + const f: number[] = new Array(n).fill(0); + for (const row of matrix) { + const g = f.slice(); + for (let j = 0; j < n; ++j) { + if (j > 0) { + g[j] = Math.min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); + } + g[j] += row[j]; + } + f.splice(0, n, ...g); + } + return Math.min(...f); } ``` diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.cpp b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.cpp index db9e583a1237d..668f5c6bc5ced 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.cpp +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.cpp @@ -1,19 +1,21 @@ -class Solution { -public: - int minFallingPathSum(vector>& matrix) { - int n = matrix.size(); - for (int i = 1; i < n; ++i) { - for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; - if (j > 0) mi = min(mi, matrix[i - 1][j - 1]); - if (j < n - 1) mi = min(mi, matrix[i - 1][j + 1]); - matrix[i][j] += mi; - } - } - int res = INT_MAX; - for (int j = 0; j < n; ++j) { - res = min(res, matrix[n - 1][j]); - } - return res; - } +class Solution { +public: + int minFallingPathSum(vector>& matrix) { + int n = matrix.size(); + vector f(n); + for (auto& row : matrix) { + auto g = f; + for (int j = 0; j < n; ++j) { + if (j) { + g[j] = min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = min(g[j], f[j + 1]); + } + g[j] += row[j]; + } + f = move(g); + } + return *min_element(f.begin(), f.end()); + } }; \ No newline at end of file diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.go b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.go index 6ff98e2748081..5797743d5121c 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.go +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.go @@ -1,22 +1,30 @@ func minFallingPathSum(matrix [][]int) int { n := len(matrix) - for i := 1; i < n; i++ { - for j := 0; j < n; j++ { - mi := matrix[i-1][j] - if j > 0 && mi > matrix[i-1][j-1] { - mi = matrix[i-1][j-1] + f := make([]int, n) + for _, row := range matrix { + g := make([]int, n) + copy(g, f) + for j, x := range row { + if j > 0 { + g[j] = min(g[j], f[j-1]) } - if j < n-1 && mi > matrix[i-1][j+1] { - mi = matrix[i-1][j+1] + if j+1 < n { + g[j] = min(g[j], f[j+1]) } - matrix[i][j] += mi + g[j] += x } + f = g } - res := 10000 - for j := 0; j < n; j++ { - if res > matrix[n-1][j] { - res = matrix[n-1][j] - } + ans := 1 << 30 + for _, x := range f { + ans = min(ans, x) + } + return ans +} + +func min(a, b int) int { + if a < b { + return a } - return res + return b } \ No newline at end of file diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.java b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.java index 2bab8e9ae6872..a8cd5a48de874 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.java +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.java @@ -1,22 +1,25 @@ -class Solution { - public int minFallingPathSum(int[][] matrix) { - int n = matrix.length; - for (int i = 1; i < n; ++i) { - for (int j = 0; j < n; ++j) { - int mi = matrix[i - 1][j]; - if (j > 0) { - mi = Math.min(mi, matrix[i - 1][j - 1]); - } - if (j < n - 1) { - mi = Math.min(mi, matrix[i - 1][j + 1]); - } - matrix[i][j] += mi; - } - } - int res = Integer.MAX_VALUE; - for (int j = 0; j < n; ++j) { - res = Math.min(res, matrix[n - 1][j]); - } - return res; - } +class Solution { + public int minFallingPathSum(int[][] matrix) { + int n = matrix.length; + var f = new int[n]; + for (var row : matrix) { + var g = f.clone(); + for (int j = 0; j < n; ++j) { + if (j > 0) { + g[j] = Math.min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); + } + g[j] += row[j]; + } + f = g; + } + // return Arrays.stream(f).min().getAsInt(); + int ans = 1 << 30; + for (int x : f) { + ans = Math.min(ans, x); + } + return ans; + } } \ No newline at end of file diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.py b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.py index c96313d60939d..908a4ab112d55 100644 --- a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.py +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.py @@ -1,12 +1,11 @@ -class Solution: - def minFallingPathSum(self, matrix: List[List[int]]) -> int: - n = len(matrix) - for i in range(1, n): - for j in range(n): - mi = matrix[i - 1][j] - if j > 0: - mi = min(mi, matrix[i - 1][j - 1]) - if j < n - 1: - mi = min(mi, matrix[i - 1][j + 1]) - matrix[i][j] += mi - return min(matrix[n - 1]) +class Solution: + def minFallingPathSum(self, matrix: List[List[int]]) -> int: + n = len(matrix) + f = [0] * n + for row in matrix: + g = [0] * n + for j, x in enumerate(row): + l, r = max(0, j - 1), min(n, j + 2) + g[j] = min(f[l:r]) + x + f = g + return min(f) diff --git a/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.ts b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.ts new file mode 100644 index 0000000000000..b67d24f5c70f0 --- /dev/null +++ b/solution/0900-0999/0931.Minimum Falling Path Sum/Solution.ts @@ -0,0 +1,18 @@ +function minFallingPathSum(matrix: number[][]): number { + const n = matrix.length; + const f: number[] = new Array(n).fill(0); + for (const row of matrix) { + const g = f.slice(); + for (let j = 0; j < n; ++j) { + if (j > 0) { + g[j] = Math.min(g[j], f[j - 1]); + } + if (j + 1 < n) { + g[j] = Math.min(g[j], f[j + 1]); + } + g[j] += row[j]; + } + f.splice(0, n, ...g); + } + return Math.min(...f); +}