Skip to content
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

Added tasks 3423-3430 #1896

Merged
merged 10 commits into from
Jan 23, 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,15 @@
package g3401_3500.s3423_maximum_difference_between_adjacent_elements_in_a_circular_array;

// #Easy #Array #2025_01_22_Time_1_(99.86%)_Space_43.72_(36.06%)

public class Solution {
public int maxAdjacentDistance(int[] nums) {
int maxDiff = 0;
for (int i = 0; i < nums.length; i++) {
int nextIndex = (i + 1) % nums.length;
int diff = Math.abs(nums[i] - nums[nextIndex]);
maxDiff = Math.max(maxDiff, diff);
}
return maxDiff;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
3423\. Maximum Difference Between Adjacent Elements in a Circular Array

Easy

Given a **circular** array `nums`, find the **maximum** absolute difference between adjacent elements.

**Note**: In a circular array, the first and last elements are adjacent.

**Example 1:**

**Input:** nums = [1,2,4]

**Output:** 3

**Explanation:**

Because `nums` is circular, `nums[0]` and `nums[2]` are adjacent. They have the maximum absolute difference of `|4 - 1| = 3`.

**Example 2:**

**Input:** nums = [-5,-10,-5]

**Output:** 5

**Explanation:**

The adjacent elements `nums[0]` and `nums[1]` have the maximum absolute difference of `|-5 - (-10)| = 5`.

**Constraints:**

* `2 <= nums.length <= 100`
* `-100 <= nums[i] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g3401_3500.s3424_minimum_cost_to_make_arrays_identical;

// #Medium #Array #Sorting #Greedy #2025_01_22_Time_60_(98.08%)_Space_57.68_(39.04%)

import java.util.Arrays;

public class Solution {
public long minCost(int[] arr, int[] brr, long k) {
long res1 = 0;
long res2 = 0;
for (int i = 0; i < arr.length; ++i) {
res1 += Math.abs(arr[i] - brr[i]);
}
Arrays.sort(arr);
Arrays.sort(brr);
for (int i = 0; i < arr.length; ++i) {
res2 += Math.abs(arr[i] - brr[i]);
}
return Math.min(res1, res2 + k);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3424\. Minimum Cost to Make Arrays Identical

Medium

You are given two integer arrays `arr` and `brr` of length `n`, and an integer `k`. You can perform the following operations on `arr` _any_ number of times:

* Split `arr` into _any_ number of **contiguous** **non-empty subarrays** and rearrange these subarrays in _any order_. This operation has a fixed cost of `k`.
* Choose any element in `arr` and add or subtract a positive integer `x` to it. The cost of this operation is `x`.


Return the **minimum** total cost to make `arr` **equal** to `brr`.

**Example 1:**

**Input:** arr = [-7,9,5], brr = [7,-2,-5], k = 2

**Output:** 13

**Explanation:**

* Split `arr` into two contiguous subarrays: `[-7]` and `[9, 5]` and rearrange them as `[9, 5, -7]`, with a cost of 2.
* Subtract 2 from element `arr[0]`. The array becomes `[7, 5, -7]`. The cost of this operation is 2.
* Subtract 7 from element `arr[1]`. The array becomes `[7, -2, -7]`. The cost of this operation is 7.
* Add 2 to element `arr[2]`. The array becomes `[7, -2, -5]`. The cost of this operation is 2.

The total cost to make the arrays equal is `2 + 2 + 7 + 2 = 13`.

**Example 2:**

**Input:** arr = [2,1], brr = [2,1], k = 0

**Output:** 0

**Explanation:**

Since the arrays are already equal, no operations are needed, and the total cost is 0.

**Constraints:**

* <code>1 <= arr.length == brr.length <= 10<sup>5</sup></code>
* <code>0 <= k <= 2 * 10<sup>10</sup></code>
* <code>-10<sup>5</sup> <= arr[i] <= 10<sup>5</sup></code>
* <code>-10<sup>5</sup> <= brr[i] <= 10<sup>5</sup></code>
96 changes: 96 additions & 0 deletions src/main/java/g3401_3500/s3425_longest_special_path/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package g3401_3500.s3425_longest_special_path;

// #Hard #Array #Hash_Table #Depth_First_Search #Tree #Sliding_Window
// #2025_01_22_Time_49_(74.66%)_Space_98.04_(44.26%)

import java.util.ArrayList;
import java.util.Arrays;

@SuppressWarnings("unchecked")
public class Solution {
private ArrayList<int[]>[] adj;
private int[] nums;
private int[] dist;
private int[] lastOccur;
private ArrayList<Integer> pathStack;
private int minIndex;
private int maxLen;
private int minNodesForMaxLen;

public int[] longestSpecialPath(int[][] edges, int[] nums) {
int n = nums.length;
this.nums = nums;
adj = new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<>();
}
for (int[] e : edges) {
int u = e[0];
int v = e[1];
int w = e[2];
adj[u].add(new int[] {v, w});
adj[v].add(new int[] {u, w});
}
dist = new int[n];
buildDist(0, -1, 0);
int maxVal = 0;
for (int val : nums) {
if (val > maxVal) {
maxVal = val;
}
}
lastOccur = new int[maxVal + 1];
Arrays.fill(lastOccur, -1);
pathStack = new ArrayList<>();
minIndex = 0;
maxLen = 0;
minNodesForMaxLen = Integer.MAX_VALUE;
dfs(0, -1);
return new int[] {maxLen, minNodesForMaxLen};
}

private void buildDist(int u, int parent, int currDist) {
dist[u] = currDist;
for (int[] edge : adj[u]) {
int v = edge[0];
int w = edge[1];
if (v == parent) {
continue;
}
buildDist(v, u, currDist + w);
}
}

private void dfs(int u, int parent) {
int stackPos = pathStack.size();
pathStack.add(u);
int val = nums[u];
int oldPos = lastOccur[val];
int oldMinIndex = minIndex;
lastOccur[val] = stackPos;
if (oldPos >= minIndex) {
minIndex = oldPos + 1;
}
if (minIndex <= stackPos) {
int ancestor = pathStack.get(minIndex);
int pathLength = dist[u] - dist[ancestor];
int pathNodes = stackPos - minIndex + 1;
if (pathLength > maxLen) {
maxLen = pathLength;
minNodesForMaxLen = pathNodes;
} else if (pathLength == maxLen && pathNodes < minNodesForMaxLen) {
minNodesForMaxLen = pathNodes;
}
}
for (int[] edge : adj[u]) {
int v = edge[0];
if (v == parent) {
continue;
}
dfs(v, u);
}
pathStack.remove(pathStack.size() - 1);
lastOccur[val] = oldPos;
minIndex = oldMinIndex;
}
}
48 changes: 48 additions & 0 deletions src/main/java/g3401_3500/s3425_longest_special_path/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3425\. Longest Special Path

Hard

You are given an undirected tree rooted at node `0` with `n` nodes numbered from `0` to `n - 1`, represented by a 2D array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> indicates an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>length<sub>i</sub></code>. You are also given an integer array `nums`, where `nums[i]` represents the value at node `i`.

A **special path** is defined as a **downward** path from an ancestor node to a descendant node such that all the values of the nodes in that path are **unique**.

**Note** that a path may start and end at the same node.

Return an array `result` of size 2, where `result[0]` is the **length** of the **longest** special path, and `result[1]` is the **minimum** number of nodes in all _possible_ **longest** special paths.

**Example 1:**

**Input:** edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]

**Output:** [6,2]

**Explanation:**

#### In the image below, nodes are colored by their corresponding values in `nums`

![](https://assets.leetcode.com/uploads/2024/11/02/tree3.jpeg)

The longest special paths are `2 -> 5` and `0 -> 1 -> 4`, both having a length of 6. The minimum number of nodes across all longest special paths is 2.

**Example 2:**

**Input:** edges = [[1,0,8]], nums = [2,2]

**Output:** [0,1]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/11/02/tree4.jpeg)

The longest special paths are `0` and `1`, both having a length of 0. The minimum number of nodes across all longest special paths is 1.

**Constraints:**

* <code>2 <= n <= 5 * 10<sup>4</sup></code>
* `edges.length == n - 1`
* `edges[i].length == 3`
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
* <code>1 <= length<sub>i</sub> <= 10<sup>3</sup></code>
* `nums.length == n`
* <code>0 <= nums[i] <= 5 * 10<sup>4</sup></code>
* The input is generated such that `edges` represents a valid tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package g3401_3500.s3426_manhattan_distances_of_all_arrangements_of_pieces;

// #Hard #Math #Combinatorics #2025_01_22_Time_20_(87.92%)_Space_40.82_(98.07%)

public class Solution {
private long comb(long a, long b, long mod) {
if (b > a) {
return 0;
}
long numer = 1;
long denom = 1;
for (long i = 0; i < b; ++i) {
numer = numer * (a - i) % mod;
denom = denom * (i + 1) % mod;
}
long denomInv = 1;
long exp = mod - 2;
while (exp > 0) {
if (exp % 2 > 0) {
denomInv = denomInv * denom % mod;
}
denom = denom * denom % mod;
exp /= 2;
}
return numer * denomInv % mod;
}

public int distanceSum(int m, int n, int k) {
long res = 0;
long mod = 1000000007;
long base = comb((long) m * n - 2, k - 2L, mod);
for (int d = 1; d < n; ++d) {
res = (res + (long) d * (n - d) % mod * m % mod * m % mod) % mod;
}
for (int d = 1; d < m; ++d) {
res = (res + (long) d * (m - d) % mod * n % mod * n % mod) % mod;
}
return (int) (res * base % mod);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3426\. Manhattan Distances of All Arrangements of Pieces

Hard

You are given three integers `m`, `n`, and `k`.

There is a rectangular grid of size `m × n` containing `k` identical pieces. Return the sum of Manhattan distances between every pair of pieces over all **valid arrangements** of pieces.

A **valid arrangement** is a placement of all `k` pieces on the grid with **at most** one piece per cell.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.

**Example 1:**

**Input:** m = 2, n = 2, k = 2

**Output:** 8

**Explanation:**

The valid arrangements of pieces on the board are:

![](https://assets.leetcode.com/uploads/2024/12/25/4040example1.drawio)![](https://assets.leetcode.com/uploads/2024/12/25/untitled-diagramdrawio.png)

* In the first 4 arrangements, the Manhattan distance between the two pieces is 1.
* In the last 2 arrangements, the Manhattan distance between the two pieces is 2.

Thus, the total Manhattan distance across all valid arrangements is `1 + 1 + 1 + 1 + 2 + 2 = 8`.

**Example 2:**

**Input:** m = 1, n = 4, k = 3

**Output:** 20

**Explanation:**

The valid arrangements of pieces on the board are:

![](https://assets.leetcode.com/uploads/2024/12/25/4040example2drawio.png)

* The first and last arrangements have a total Manhattan distance of `1 + 1 + 2 = 4`.
* The middle two arrangements have a total Manhattan distance of `1 + 2 + 3 = 6`.

The total Manhattan distance between all pairs of pieces across all arrangements is `4 + 6 + 6 + 4 = 20`.

**Constraints:**

* <code>1 <= m, n <= 10<sup>5</sup></code>
* <code>2 <= m * n <= 10<sup>5</sup></code>
* `2 <= k <= m * n`
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package g3401_3500.s3427_sum_of_variable_length_subarrays;

// #Easy #Array #Prefix_Sum #2025_01_22_Time_0_(100.00%)_Space_43.77_(58.41%)

public class Solution {
public int subarraySum(int[] nums) {
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
int j = i - nums[i] - 1;
nums[i] += nums[i - 1];
res += nums[i] - (j < 0 ? 0 : nums[j]);
}
return res;
}
}
Loading
Loading