Skip to content

Added tasks 3417-3420 #1892

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 9 commits into from
Jan 15, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g3401_3500.s3417_zigzag_grid_traversal_with_skip;

// #Easy #Array #Matrix #Simulation #2025_01_15_Time_1_(100.00%)_Space_45.56_(84.25%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<Integer> zigzagTraversal(int[][] grid) {
List<Integer> ans = new ArrayList<>();
int m = grid.length;
int n = grid[0].length;
int i = 0;
boolean flag = true;
boolean skip = false;
while (i < m) {
if (flag) {
for (int j = 0; j < n; j++) {
if (!skip) {
ans.add(grid[i][j]);
}
skip = !skip;
}
} else {
for (int j = n - 1; j >= 0; j--) {
if (!skip) {
ans.add(grid[i][j]);
}
skip = !skip;
}
}
flag = !flag;
i++;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3417\. Zigzag Grid Traversal With Skip

Easy

You are given an `m x n` 2D array `grid` of **positive** integers.

Your task is to traverse `grid` in a **zigzag** pattern while skipping every **alternate** cell.

Zigzag pattern traversal is defined as following the below actions:

* Start at the top-left cell `(0, 0)`.
* Move _right_ within a row until the end of the row is reached.
* Drop down to the next row, then traverse _left_ until the beginning of the row is reached.
* Continue **alternating** between right and left traversal until every row has been traversed.

**Note** that you **must skip** every _alternate_ cell during the traversal.

Return an array of integers `result` containing, **in order**, the value of the cells visited during the zigzag traversal with skips.

**Example 1:**

**Input:** grid = [[1,2],[3,4]]

**Output:** [1,4]

**Explanation:**

**![](https://assets.leetcode.com/uploads/2024/11/23/4012_example0.png)**

**Example 2:**

**Input:** grid = [[2,1],[2,1],[2,1]]

**Output:** [2,1,2]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/11/23/4012_example1.png)

**Example 3:**

**Input:** grid = [[1,2,3],[4,5,6],[7,8,9]]

**Output:** [1,3,5,7,9]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/11/23/4012_example2.png)

**Constraints:**

* `2 <= n == grid.length <= 50`
* `2 <= m == grid[i].length <= 50`
* `1 <= grid[i][j] <= 2500`
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package g3401_3500.s3418_maximum_amount_of_money_robot_can_earn;

// #Medium #Array #Dynamic_Programming #Matrix #2025_01_15_Time_12_(99.86%)_Space_72.43_(98.47%)

public class Solution {
public int maximumAmount(int[][] coins) {
int m = coins.length;
int n = coins[0].length;
int[][] dp = new int[m][n];
int[][] dp1 = new int[m][n];
int[][] dp2 = new int[m][n];
dp[0][0] = coins[0][0];
for (int j = 1; j < n; j++) {
dp[0][j] = dp[0][j - 1] + coins[0][j];
}
for (int i = 1; i < m; i++) {
dp[i][0] = dp[i - 1][0] + coins[i][0];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]) + coins[i][j];
}
}
dp1[0][0] = Math.max(coins[0][0], 0);
for (int j = 1; j < n; j++) {
dp1[0][j] = Math.max(dp[0][j - 1], dp1[0][j - 1] + coins[0][j]);
}
for (int i = 1; i < m; i++) {
dp1[i][0] = Math.max(dp[i - 1][0], dp1[i - 1][0] + coins[i][0]);
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp1[i][j] =
Math.max(
Math.max(dp[i][j - 1], dp[i - 1][j]),
Math.max(dp1[i][j - 1], dp1[i - 1][j]) + coins[i][j]);
}
}
dp2[0][0] = Math.max(coins[0][0], 0);
for (int j = 1; j < n; j++) {
dp2[0][j] = Math.max(dp1[0][j - 1], dp2[0][j - 1] + coins[0][j]);
}
for (int i = 1; i < m; i++) {
dp2[i][0] = Math.max(dp1[i - 1][0], dp2[i - 1][0] + coins[i][0]);
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp2[i][j] =
Math.max(
Math.max(dp1[i][j - 1], dp1[i - 1][j]),
Math.max(dp2[i][j - 1], dp2[i - 1][j]) + coins[i][j]);
}
}
return dp2[m - 1][n - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
3418\. Maximum Amount of Money Robot Can Earn

Medium

You are given an `m x n` grid. A robot starts at the top-left corner of the grid `(0, 0)` and wants to reach the bottom-right corner `(m - 1, n - 1)`. The robot can move either right or down at any point in time.

The grid contains a value `coins[i][j]` in each cell:

* If `coins[i][j] >= 0`, the robot gains that many coins.
* If `coins[i][j] < 0`, the robot encounters a robber, and the robber steals the **absolute** value of `coins[i][j]` coins.

The robot has a special ability to **neutralize robbers** in at most **2 cells** on its path, preventing them from stealing coins in those cells.

**Note:** The robot's total coins can be negative.

Return the **maximum** profit the robot can gain on the route.

**Example 1:**

**Input:** coins = [[0,1,-1],[1,-2,3],[2,-3,4]]

**Output:** 8

**Explanation:**

An optimal path for maximum coins is:

1. Start at `(0, 0)` with `0` coins (total coins = `0`).
2. Move to `(0, 1)`, gaining `1` coin (total coins = `0 + 1 = 1`).
3. Move to `(1, 1)`, where there's a robber stealing `2` coins. The robot uses one neutralization here, avoiding the robbery (total coins = `1`).
4. Move to `(1, 2)`, gaining `3` coins (total coins = `1 + 3 = 4`).
5. Move to `(2, 2)`, gaining `4` coins (total coins = `4 + 4 = 8`).

**Example 2:**

**Input:** coins = [[10,10,10],[10,10,10]]

**Output:** 40

**Explanation:**

An optimal path for maximum coins is:

1. Start at `(0, 0)` with `10` coins (total coins = `10`).
2. Move to `(0, 1)`, gaining `10` coins (total coins = `10 + 10 = 20`).
3. Move to `(0, 2)`, gaining another `10` coins (total coins = `20 + 10 = 30`).
4. Move to `(1, 2)`, gaining the final `10` coins (total coins = `30 + 10 = 40`).

**Constraints:**

* `m == coins.length`
* `n == coins[i].length`
* `1 <= m, n <= 500`
* `-1000 <= coins[i][j] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package g3401_3500.s3419_minimize_the_maximum_edge_weight_of_graph;

// #Medium #Binary_Search #Graph #Shortest_Path #Depth_First_Search #Breadth_First_Search
// #2025_01_15_Time_64_(99.28%)_Space_110.17_(57.63%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

@SuppressWarnings({"unchecked", "unused", "java:S1172"})
public class Solution {
private ArrayList<ArrayList<Pair>> revadj;

private static class Pair {
int node;
int weight;

public Pair(int node, int weight) {
this.node = node;
this.weight = weight;
}
}

public int minMaxWeight(int n, int[][] edges, int threshold) {
ArrayList<ArrayList<Pair>> adj = new ArrayList<>();
revadj = new ArrayList<>();
for (int i = 0; i <= n + 1; i++) {
adj.add(new ArrayList<>());
revadj.add(new ArrayList<>());
}
for (int[] edge : edges) {
int u = edge[0];
int v = edge[1];
int wt = edge[2];
adj.get(u).add(new Pair(v, wt));
revadj.get(v).add(new Pair(u, wt));
}
if (!check(n)) {
return -1;
}
int[] dist = new int[n + 1];
Arrays.fill(dist, (int) (1e9));
dist[0] = 0;
Queue<Pair> q = new LinkedList<>();
q.offer(new Pair(0, 0));
while (!q.isEmpty()) {
int u = q.peek().node;
int currMax = q.peek().weight;
q.poll();
for (int i = 0; i < revadj.get(u).size(); i++) {
int v = revadj.get(u).get(i).node;
int wt = revadj.get(u).get(i).weight;
if (dist[v] > Math.max(wt, currMax)) {
dist[v] = Math.max(wt, currMax);
q.offer(new Pair(v, dist[v]));
}
}
}
int maxi = dist[0];
for (int i = 0; i < n; i++) {
maxi = Math.max(maxi, dist[i]);
}
return maxi;
}

private boolean check(int n) {
int[] vis = new int[n];
ArrayList<Integer> nodes = new ArrayList<>();
dfs(0, vis, nodes);
return nodes.size() == n;
}

private void dfs(int u, int[] vis, ArrayList<Integer> nodes) {
nodes.add(u);
vis[u] = 1;
for (int i = 0; i < revadj.get(u).size(); i++) {
int v = revadj.get(u).get(i).node;
if (vis[v] == 0) {
dfs(v, vis, nodes);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
3419\. Minimize the Maximum Edge Weight of Graph

Medium

You are given two integers, `n` and `threshold`, as well as a **directed** weighted graph of `n` nodes numbered from 0 to `n - 1`. The graph is represented by a **2D** integer array `edges`, where <code>edges[i] = [A<sub>i</sub>, B<sub>i</sub>, W<sub>i</sub>]</code> indicates that there is an edge going from node <code>A<sub>i</sub></code> to node <code>B<sub>i</sub></code> with weight <code>W<sub>i</sub></code>.

You have to remove some edges from this graph (possibly **none**), so that it satisfies the following conditions:

* Node 0 must be reachable from all other nodes.
* The **maximum** edge weight in the resulting graph is **minimized**.
* Each node has **at most** `threshold` outgoing edges.

Return the **minimum** possible value of the **maximum** edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1.

**Example 1:**

**Input:** n = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2

**Output:** 1

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/12/09/s-1.png)

Remove the edge `2 -> 0`. The maximum weight among the remaining edges is 1.

**Example 2:**

**Input:** n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1

**Output:** \-1

**Explanation:**

It is impossible to reach node 0 from node 2.

**Example 3:**

**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1

**Output:** 2

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/12/09/s2-1.png)

Remove the edges `1 -> 3` and `1 -> 4`. The maximum weight among the remaining edges is 2.

**Example 4:**

**Input:** n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1

**Output:** \-1

**Constraints:**

* <code>2 <= n <= 10<sup>5</sup></code>
* `1 <= threshold <= n - 1`
* <code>1 <= edges.length <= min(10<sup>5</sup>, n * (n - 1) / 2).</code>
* `edges[i].length == 3`
* <code>0 <= A<sub>i</sub>, B<sub>i</sub> < n</code>
* <code>A<sub>i</sub> != B<sub>i</sub></code>
* <code>1 <= W<sub>i</sub> <= 10<sup>6</sup></code>
* There **may be** multiple edges between a pair of nodes, but they must have unique weights.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g3401_3500.s3420_count_non_decreasing_subarrays_after_k_operations;

// #Hard #Array #Two_Pointers #Stack #Monotonic_Stack #Queue #Segment_Tree #Monotonic_Queue
// #2025_01_15_Time_29_(83.94%)_Space_62.04_(56.93%)

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
public long countNonDecreasingSubarrays(int[] nums, long k) {
int n = nums.length;
for (int i = 0; i < n / 2; ++i) {
int temp = nums[i];
nums[i] = nums[n - 1 - i];
nums[n - 1 - i] = temp;
}
long res = 0;
Deque<Integer> q = new ArrayDeque<>();
int i = 0;
for (int j = 0; j < nums.length; ++j) {
while (!q.isEmpty() && nums[q.peekLast()] < nums[j]) {
int r = q.pollLast();
int l = q.isEmpty() ? i - 1 : q.peekLast();
k -= (long) (r - l) * (nums[j] - nums[r]);
}
q.addLast(j);
while (k < 0) {
k += nums[q.peekFirst()] - nums[i];
if (q.peekFirst() == i) {
q.pollFirst();
}
++i;
}
res += j - i + 1;
}
return res;
}
}
Loading
Loading