Skip to content

Improved tasks 3457, 3459 #1914

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

Merged
merged 5 commits into from
Feb 21, 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
49 changes: 31 additions & 18 deletions src/main/java/g3401_3500/s3457_eat_pizzas/Solution.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
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) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,106 @@
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_56_ms_(72.97%)_Space_75.44_MB_(91.21%)

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 final 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);
}
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;
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));
}
}

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) {
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;
return ans;
}

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];
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];
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);
}

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];
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) {
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]);
}
}
}
}
}
dp[i][j][d][k] = res;
return res;
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);
}
}
Loading