From 3a4db2818d01fecf7c1c386c79734a01da418fe5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 21 Feb 2025 03:01:29 +0200 Subject: [PATCH 1/5] Improved task 3459 --- .../Solution.java | 109 ++++++++++-------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java index 013814405..e4b406118 100644 --- a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java +++ b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java @@ -1,60 +1,79 @@ package g3401_3500.s3459_length_of_longest_v_shaped_diagonal_segment; // #Hard #Array #Dynamic_Programming #Matrix #Memoization -// #2025_02_18_Time_461_ms_(36.09%)_Space_127.47_MB_(39.48%) - -import java.util.Arrays; +// #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%) public class Solution { - private final int[][] ds = {{1, 1}, {1, -1}, {-1, -1}, {-1, 1}}; - private final int[] nx = {2, 2, 0}; - private int[][] grid; - private int n; - private int m; - private int[][][][] dp; + private int[][] directions = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; - public int lenOfVDiagonal(int[][] g) { - this.grid = g; - this.n = g.length; - this.m = g[0].length; - this.dp = new int[n][m][4][2]; - for (int[][][] d1 : dp) { - for (int[][] d2 : d1) { - for (int[] d3 : d2) { - Arrays.fill(d3, -1); - } + public int lenOfVDiagonal(int[][] grid) { + int m = grid.length, n = grid[0].length; + int[][] bottomLeft = new int[m][n]; + int[][] bottomRight = new int[m][n]; + int[][] topLeft = new int[m][n]; + int[][] topRight = new int[m][n]; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + bottomLeft[i][j] = 1; + bottomRight[i][j] = 1; + topLeft[i][j] = 1; + topRight[i][j] = 1; } } - int res = 0; - for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - if (g[i][j] == 1) { - for (int d = 0; d < 4; d++) { - res = Math.max(res, dp(i, j, 1, d, 1)); - } + int ans = 0; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int x = grid[i][j]; + if (x == 1) { + ans = 1; + continue; + } + if (i > 0 && j + 1 < n && grid[i - 1][j + 1] == 2 - x) { + bottomLeft[i][j] = bottomLeft[i - 1][j + 1] + 1; + } + if (i > 0 && j > 0 && grid[i - 1][j - 1] == 2 - x) { + bottomRight[i][j] = bottomRight[i - 1][j - 1] + 1; } } } - return res; - } - - private int dp(int i, int j, int x, int d, int k) { - if (i < 0 || i >= n || j < 0 || j >= m) { - return 0; - } - if (grid[i][j] != x) { - return 0; - } - if (dp[i][j][d][k] != -1) { - return dp[i][j][d][k]; + for (int i = m - 1; i >= 0; --i) { + for (int j = n - 1; j >= 0; --j) { + int x = grid[i][j]; + if (x == 1) { + continue; + } + if (i + 1 < m && j + 1 < n && grid[i + 1][j + 1] == 2 - x) { + topLeft[i][j] = topLeft[i + 1][j + 1] + 1; + } + if (i + 1 < m && j > 0 && grid[i + 1][j - 1] == 2 - x) { + topRight[i][j] = topRight[i + 1][j - 1] + 1; + } + } } - int res = dp(i + ds[d][0], j + ds[d][1], nx[x], d, k) + 1; - if (k > 0) { - int d2 = (d + 1) % 4; - int res2 = dp(i + ds[d2][0], j + ds[d2][1], nx[x], d2, 0) + 1; - res = Math.max(res, res2); + int[][][] memo = {topLeft, topRight, bottomRight, bottomLeft}; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + int x = grid[i][j]; + if (x == 1) { + continue; + } + x >>= 1; + for (int k = 0; k < 4; ++k) { + int v = memo[k][i][j]; + if ((v & 1) != x) { + continue; + } + if (v + memo[k + 3 & 3][i][j] <= ans) { + continue; + } + int[] d = directions[k]; + int ni = i - d[0] * v, nj = j - d[1] * v; + if (ni >= 0 && nj >= 0 && ni < m && nj < n && grid[ni][nj] == 1) { + ans = Math.max(ans, v + memo[k + 3 & 3][i][j]); + } + } + } } - dp[i][j][d][k] = res; - return res; + return ans; } } From 17dbb1e5a169c69e03bcb953856544b12c4b57b0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 21 Feb 2025 06:39:50 +0200 Subject: [PATCH 2/5] Fixed sonar --- .../Solution.java | 57 ++++++++++++++----- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java index e4b406118..b2014bb64 100644 --- a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java +++ b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java @@ -4,14 +4,15 @@ // #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%) public class Solution { - private int[][] directions = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; + private final int[][] directions = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; - public int lenOfVDiagonal(int[][] grid) { - int m = grid.length, n = grid[0].length; - int[][] bottomLeft = new int[m][n]; - int[][] bottomRight = new int[m][n]; - int[][] topLeft = new int[m][n]; - int[][] topRight = new int[m][n]; + private void initializeArrays( + int[][] bottomLeft, + int[][] bottomRight, + int[][] topLeft, + int[][] topRight, + int m, + int n) { for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { bottomLeft[i][j] = 1; @@ -20,6 +21,10 @@ public int lenOfVDiagonal(int[][] grid) { topRight[i][j] = 1; } } + } + + private int processBottomDirections( + int[][] grid, int[][] bottomLeft, int[][] bottomRight, int m, int n) { int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { @@ -36,6 +41,11 @@ public int lenOfVDiagonal(int[][] grid) { } } } + return ans; + } + + private void processTopDirections( + int[][] grid, int[][] topLeft, int[][] topRight, int m, int n) { for (int i = m - 1; i >= 0; --i) { for (int j = n - 1; j >= 0; --j) { int x = grid[i][j]; @@ -50,7 +60,10 @@ public int lenOfVDiagonal(int[][] grid) { } } } - int[][][] memo = {topLeft, topRight, bottomRight, bottomLeft}; + } + + private int findMaxDiagonal(int[][] grid, int[][][] memo, int m, int n, int initialAns) { + int ans = initialAns; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { int x = grid[i][j]; @@ -63,17 +76,31 @@ public int lenOfVDiagonal(int[][] grid) { if ((v & 1) != x) { continue; } - if (v + memo[k + 3 & 3][i][j] <= ans) { - continue; - } - int[] d = directions[k]; - int ni = i - d[0] * v, nj = j - d[1] * v; - if (ni >= 0 && nj >= 0 && ni < m && nj < n && grid[ni][nj] == 1) { - ans = Math.max(ans, v + memo[k + 3 & 3][i][j]); + if (v + memo[k + 3 & 3][i][j] > ans) { + int[] d = directions[k]; + int ni = i - d[0] * v; + int nj = j - d[1] * v; + if (ni >= 0 && nj >= 0 && ni < m && nj < n && grid[ni][nj] == 1) { + ans = Math.max(ans, v + memo[k + 3 & 3][i][j]); + } } } } } return ans; } + + public int lenOfVDiagonal(int[][] grid) { + int m = grid.length; + int n = grid[0].length; + int[][] bottomLeft = new int[m][n]; + int[][] bottomRight = new int[m][n]; + int[][] topLeft = new int[m][n]; + int[][] topRight = new int[m][n]; + initializeArrays(bottomLeft, bottomRight, topLeft, topRight, m, n); + int ans = processBottomDirections(grid, bottomLeft, bottomRight, m, n); + processTopDirections(grid, topLeft, topRight, m, n); + int[][][] memo = {topLeft, topRight, bottomRight, bottomLeft}; + return findMaxDiagonal(grid, memo, m, n, ans); + } } From 17ed8f2bcd4e54a90713bcbbe7ea24fb5cfcb21a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 21 Feb 2025 06:45:09 +0200 Subject: [PATCH 3/5] Fixed tag --- .../Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java index b2014bb64..d61c432fe 100644 --- a/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java +++ b/src/main/java/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment/Solution.java @@ -1,7 +1,7 @@ package g3401_3500.s3459_length_of_longest_v_shaped_diagonal_segment; // #Hard #Array #Dynamic_Programming #Matrix #Memoization -// #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%) +// #2025_02_21_Time_56_ms_(72.97%)_Space_75.44_MB_(91.21%) public class Solution { private final int[][] directions = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; From ec63b966049e36509964d5769184e2b61af0ddf7 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 21 Feb 2025 06:56:23 +0200 Subject: [PATCH 4/5] Improved 3457 --- .../g3401_3500/s3457_eat_pizzas/Solution.java | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java b/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java index de35a7396..47f23e9a4 100644 --- a/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java +++ b/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java @@ -1,26 +1,40 @@ package g3401_3500.s3457_eat_pizzas; -// #Medium #Array #Sorting #Greedy #2025_02_18_Time_63_ms_(40.14%)_Space_81.02_MB_(36.94%) +// #Medium #Array #Sorting #Greedy #2025_02_21_Time_16_ms_(100.00%)_Space_75.98_MB_(97.29%) -import java.util.Arrays; - -public class Solution { +class Solution { public long maxWeight(int[] pizzas) { - int n = pizzas.length; - int m = n / 4; - int z = (m + 1) / 2; - int y = m / 2; - int j = 0; - Arrays.sort(pizzas); - long res = 0; - for (int i = 0; i < z; ++i) { - res += pizzas[n - 1 - j]; - j += 1; + int max = 0; + for (int x : pizzas) { + max = Math.max(max, x); + } + int[] count = new int[max + 1]; + for (int x : pizzas) { + count[x]++; + } + int m = pizzas.length; + int n = m / 4; + int index = 0; + for (int x = max; x > 0; --x) { + if (count[x] == 0) { + continue; + } + int c = count[x]; + while (c-- > 0) { + pizzas[index++] = x; + } + if (index >= m / 2) { + break; + } + } + long ans = 0; + for (int i = 0; i < (n + 1) / 2; ++i) { + ans += pizzas[i]; } - for (int i = 0; i < y; ++i) { - res += pizzas[n - 1 - j - 1]; - j += 2; + int k = n - (n + 1) / 2; + for (int i = (n + 1) / 2 + 1; k > 0; i += 2, k--) { + ans += pizzas[i]; } - return res; + return ans; } } From e02790c7e9e4cf17c87bb5d7b376a9094f99e4d9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 21 Feb 2025 07:10:16 +0200 Subject: [PATCH 5/5] Fixed sonar --- .../g3401_3500/s3457_eat_pizzas/Solution.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java b/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java index 47f23e9a4..1f31423b2 100644 --- a/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java +++ b/src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java @@ -16,15 +16,14 @@ public long maxWeight(int[] pizzas) { int n = m / 4; int index = 0; for (int x = max; x > 0; --x) { - if (count[x] == 0) { - continue; - } - int c = count[x]; - while (c-- > 0) { - pizzas[index++] = x; - } - if (index >= m / 2) { - break; + if (count[x] != 0) { + int c = count[x]; + while (c-- > 0) { + pizzas[index++] = x; + } + if (index >= m / 2) { + break; + } } } long ans = 0;