Skip to content
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
22 changes: 22 additions & 0 deletions src/main/java/g4001_4100/s4045_count_robot_groups/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g4001_4100.s4045_count_robot_groups;

// #Medium #Array #Stack #Monotonic_Stack #Staff #Weekly_Contest_518
// #2026_11_30_Time_2_ms_(100.00%)_Space_99.48_MB_(65.57%)

public class Solution {
public int countGroups(int[] position, int[] speed, int distance) {
int groups = 0;
int leftPos = position[position.length - 1];
int curSpeed = speed[speed.length - 1];
for (int i = position.length - 1; i >= 0; i--) {
if (leftPos - position[i] <= distance || curSpeed < speed[i]) {
leftPos = position[i];
} else {
groups++;
leftPos = position[i];
curSpeed = speed[i];
}
}
return groups + 1;
}
}
64 changes: 64 additions & 0 deletions src/main/java/g4001_4100/s4045_count_robot_groups/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
4045\. Count Robot Groups

Medium

You are given a strictly increasing integer array `position`, where `position[i]` is the initial position of the <code>i<sup>th</sup></code> robot at time `t = 0`.

You are also given an integer array `speed`, where `speed[i]` is the constant speed of the <code>i<sup>th</sup></code> robot in units per second, and an integer `distance`.

Time is **continuous** and measured in seconds. A robot or group with speed `v` moves `v * t` units to the right over any interval of `t` seconds.

Whenever the distance between two robots or groups becomes at most `distance`, they merge into a single group.

If multiple robots or groups satisfy the merging condition at the same time, all merges happen **simultaneously**. In particular, every connected collection of robots or groups whose consecutive positions differ by at most `distance` merges into one group.

After a merge, the resulting group takes the current position and speed of the **rightmost robot** in that group. Once merged, robots never separate.

Return the number of groups remaining after all possible merges have occurred.

**Example 1:**

**Input:** position = [1,5,6,20], speed = [4,3,2,3], distance = 1

**Output:** 2

**Explanation:**

**![](https://assets.leetcode.com/uploads/2026/08/09/c4drawio.png)**

* Initially, the groups are {R<sub>1</sub>}, {R<sub>2</sub>}, {R<sub>3</sub>}, and {R<sub>4</sub>}.
* At `t = 0`, the robots R<sub>2</sub> and R<sub>3</sub> at positions 5 and 6, respectively, merge because they are 1 unit apart. The resulting group moves with the position and speed of the rightmost robot R<sub>3</sub>. The groups are now {R<sub>1</sub>}, {R<sub>2</sub>, R<sub>3</sub>}, and {R<sub>4</sub>}.
* Later at `t = 2`, the robot R<sub>1</sub> catches up to the group {R<sub>2</sub>, R<sub>3</sub>} and merges with it. The groups are now {R<sub>1</sub>, R<sub>2</sub>, R<sub>3</sub>} and {R<sub>4</sub>}.

Thus, the answer is 2.

**Example 2:**

**Input:** position = [1,5,9], speed = [3,2,2], distance = 2

**Output:** 2

**Explanation:**

**![](https://assets.leetcode.com/uploads/2026/08/09/c5.png)**

* Initially, the groups are {R<sub>1</sub>}, {R<sub>2</sub>}, and {R<sub>3</sub>}.
* At `t = 2`, the robot R<sub>1</sub> catches up to the robot R<sub>2</sub> and merges with it. The resulting group moves with the position and speed of the rightmost robot R<sub>2</sub>. The groups are now {R<sub>1</sub>, R<sub>2</sub>} and {R<sub>3</sub>}.

Thus, the answer is 2.

**Example 3:**

**Input:** position = [9], speed = [8], distance = 5

**Output:** 1

**Explanation:**

Initially, there is only one group. Therefore, the answer is 1.

**Constraints:**

* <code>1 <= position.length == speed.length <= 10<sup>5</sup></code>
* <code>1 <= position[i], speed[i], distance <= 10<sup>9</sup></code>
* `position` is strictly increasing.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package g4001_4100.s4046_minimum_cost_path_with_at_most_k_turns;

// #Hard #Array #Matrix #Heap_Priority_Queue #Shortest_Path #Graph_Theory #Senior_Staff
// #Weekly_Contest_518 #2026_11_30_Time_13_ms_(100.00%)_Space_46.43_MB_(97.54%)

import java.util.Arrays;

public class Solution {
private static final long INF = (long) 1e15;

public int minCost(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
if (m == 1 && n == 1) {
return grid[0][0];
}
long[][] entry = new long[m][n];
for (long[] row : entry) {
Arrays.fill(row, INF);
}
entry[0][0] = grid[0][0];
for (int t = 0; t <= k; t++) {
long[][] next = new long[m][n];
for (long[] row : next) {
Arrays.fill(row, INF);
}
for (int i = 0; i < m; i++) {
long run = INF;
for (int j = 1; j < n; j++) {
run = Math.min(run, entry[i][j - 1]) + grid[i][j];
next[i][j] = Math.min(next[i][j], run);
}
}
for (int i = 0; i < m; i++) {
long run = INF;
for (int j = n - 2; j >= 0; j--) {
run = Math.min(run, entry[i][j + 1]) + grid[i][j];
next[i][j] = Math.min(next[i][j], run);
}
}
for (int j = 0; j < n; j++) {
long run = INF;
for (int i = 1; i < m; i++) {
run = Math.min(run, entry[i - 1][j]) + grid[i][j];
next[i][j] = Math.min(next[i][j], run);
}
}
for (int j = 0; j < n; j++) {
long run = INF;
for (int i = m - 2; i >= 0; i--) {
run = Math.min(run, entry[i + 1][j]) + grid[i][j];
next[i][j] = Math.min(next[i][j], run);
}
}
entry = next;
}
return entry[m - 1][n - 1] >= INF ? -1 : (int) entry[m - 1][n - 1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
4046\. Minimum Cost Path With At Most K Turns

Hard

You are given a 2D integer array `grid` of size `m x n`, where `grid[i][j]` represents the cost of visiting cell `(i, j)`, and an integer `k`.

You start at the **top-left** cell `(0, 0)` and want to reach the **bottom-right** cell `(m - 1, n - 1)`.

From each cell, you may move one step in any of the four directions: **up**, **down**, **left**, or **right**.

The cost of a path is the sum of the values of all visited cells, **including** the starting and ending cells. If a cell is visited more than once, its value is included each time it is visited.

Return the **minimum** possible path cost to reach `(m - 1, n - 1)` using **at most** `k` turns. If no such path exists, return `-1`.

A **turn** occurs when the direction changes between two consecutive moves. For example, moving right and then down counts as one turn, while moving right and then right does not.

**Example 1:**

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

**Output:** 12

**Explanation:**

* An optimal path is `(0, 0) → (1, 0) → (1, 1) → (1, 2)`. The moves are down, right, right.
* The direction changes from down to right once, so the path uses exactly `k = 1` turn.
* The total path cost is `2 + 1 + 4 + 5 = 12`.

**Example 2:**

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

**Output:** 20

**Explanation:**

* An optimal path is `(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)`. The moves are down, right, right, down.
* The direction changes from down to right and from right to down, so the path uses exactly `k = 2` turns.
* The total path cost is `4 + 3 + 2 + 5 + 6 = 20`.

**Example 3:**

**Input:** grid = [[1,9],[3,4]], k = 0

**Output:** \-1

**Explanation:**

* It is impossible to reach `(1, 1)` using `k = 0` turns. Thus, the answer is -1.

**Constraints:**

* `1 <= m == grid.length <= 75`
* `1 <= n == grid[i].length <= 75`
* `0 <= grid[i][j] <= 1000`
* `0 <= k < min(m, n)`
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g4001_4100.s4045_count_robot_groups;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countGroups() {
assertThat(
new Solution().countGroups(new int[] {1, 5, 6, 20}, new int[] {4, 3, 2, 3}, 1),
equalTo(2));
}

@Test
void countGroups2() {
assertThat(
new Solution().countGroups(new int[] {1, 5, 9}, new int[] {3, 2, 2}, 2),
equalTo(2));
}

@Test
void countGroups3() {
assertThat(new Solution().countGroups(new int[] {9}, new int[] {8}, 5), equalTo(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g4001_4100.s4046_minimum_cost_path_with_at_most_k_turns;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minCost() {
assertThat(new Solution().minCost(new int[][] {{2, 7, 3}, {1, 4, 5}}, 1), equalTo(12));
}

@Test
void minCost2() {
assertThat(
new Solution().minCost(new int[][] {{4, 1, 9}, {3, 2, 5}, {4, 8, 6}}, 2),
equalTo(20));
}

@Test
void minCost3() {
assertThat(new Solution().minCost(new int[][] {{1, 9}, {3, 4}}, 0), equalTo(-1));
}

@Test
void minCost4() {
assertThat(new Solution().minCost(new int[][] {{1}}, 0), equalTo(1));
}
}
Loading