Skip to content

Added tasks 3101-3102 #1743

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 4 commits into from
Apr 20, 2024
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,25 @@
package g3101_3200.s3101_count_alternating_subarrays;

// #Medium #Array #Math #2024_04_20_Time_3_ms_(97.51%)_Space_56.4_MB_(31.27%)

public class Solution {
public long countAlternatingSubarrays(int[] nums) {
long count = 0;
long length;
int start = 0;
int end = 1;
while (end < nums.length) {
if (nums[end] != nums[end - 1]) {
end++;
} else {
length = end - (long) start;
count += (length * (length + 1)) / 2;
start = end;
end++;
}
}
length = end - (long) start;
count += (length * (length + 1)) / 2;
return count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3101\. Count Alternating Subarrays

Medium

You are given a binary array `nums`.

We call a subarray **alternating** if **no** two **adjacent** elements in the subarray have the **same** value.

Return _the number of alternating subarrays in_ `nums`.

**Example 1:**

**Input:** nums = [0,1,1,1]

**Output:** 5

**Explanation:**

The following subarrays are alternating: `[0]`, `[1]`, `[1]`, `[1]`, and `[0,1]`.

**Example 2:**

**Input:** nums = [1,0,1,0]

**Output:** 10

**Explanation:**

Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* `nums[i]` is either `0` or `1`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package g3101_3200.s3102_minimize_manhattan_distances;

// #Hard #Array #Math #2024_04_20_Time_3_ms_(99.73%)_Space_82.4_MB_(44.95%)

public class Solution {
private int manhattan(int[][] points, int i, int j) {
return Math.abs(points[i][0] - points[j][0]) + Math.abs(points[i][1] - points[j][1]);
}

private int[] maxManhattanDistance(int[][] points, int remove) {
int n = points.length;
int maxSum = Integer.MIN_VALUE;
int minSum = Integer.MAX_VALUE;
int maxDiff = Integer.MIN_VALUE;
int minDiff = Integer.MAX_VALUE;
int maxSumIndex = -1;
int minSumIndex = -1;
int maxDiffIndex = -1;
int minDiffIndex = -1;
for (int i = 0; i < n; i++) {
if (i != remove) {
int sum = points[i][0] + points[i][1];
int diff = points[i][0] - points[i][1];
if (sum > maxSum) {
maxSumIndex = i;
maxSum = sum;
}
if (sum < minSum) {
minSumIndex = i;
minSum = sum;
}
if (diff > maxDiff) {
maxDiffIndex = i;
maxDiff = diff;
}
if (diff < minDiff) {
minDiffIndex = i;
minDiff = diff;
}
}
}
return Math.max(maxSum - minSum, maxDiff - minDiff) == maxSum - minSum
? new int[] {maxSumIndex, minSumIndex}
: new int[] {maxDiffIndex, minDiffIndex};
}

public int minimumDistance(int[][] points) {
int[] m = maxManhattanDistance(points, -1);
int[] m1 = maxManhattanDistance(points, m[0]);
int[] m2 = maxManhattanDistance(points, m[1]);
return Math.min(manhattan(points, m1[0], m1[1]), manhattan(points, m2[0], m2[1]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3102\. Minimize Manhattan Distances

Hard

You are given a array `points` representing integer coordinates of some points on a 2D plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.

The distance between two points is defined as their Manhattan distance.

Return _the **minimum** possible value for **maximum** distance between any two points by removing exactly one point_.

**Example 1:**

**Input:** points = [[3,10],[5,15],[10,2],[4,4]]

**Output:** 12

**Explanation:**

The maximum distance after removing each point is the following:

* After removing the 0<sup>th</sup> point the maximum distance is between points (5, 15) and (10, 2), which is `|5 - 10| + |15 - 2| = 18`.
* After removing the 1<sup>st</sup> point the maximum distance is between points (3, 10) and (10, 2), which is `|3 - 10| + |10 - 2| = 15`.
* After removing the 2<sup>nd</sup> point the maximum distance is between points (5, 15) and (4, 4), which is `|5 - 4| + |15 - 4| = 12`.
* After removing the 3<sup>rd</sup> point the maximum distance is between points (5, 15) and (10, 2), which is `|5 - 10| + |15 - 2| = 18`.

12 is the minimum possible maximum distance between any two points after removing exactly one point.

**Example 2:**

**Input:** points = [[1,1],[1,1],[1,1]]

**Output:** 0

**Explanation:**

Removing any of the points results in the maximum distance between any two points of 0.

**Constraints:**

* <code>3 <= points.length <= 10<sup>5</sup></code>
* `points[i].length == 2`
* <code>1 <= points[i][0], points[i][1] <= 10<sup>8</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3101_count_alternating_subarrays;

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

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countAlternatingSubarrays() {
assertThat(new Solution().countAlternatingSubarrays(new int[] {0, 1, 1, 1}), equalTo(5L));
}

@Test
void countAlternatingSubarrays2() {
assertThat(new Solution().countAlternatingSubarrays(new int[] {1, 0, 1, 0}), equalTo(10L));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g3101_3200.s3102_minimize_manhattan_distances;

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

import com_github_leetcode.CommonUtils;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minimumDistance() {
assertThat(
new Solution()
.minimumDistance(
CommonUtils
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
"[3,10],[5,15],[10,2],[4,4]")),
equalTo(12));
}

@Test
void minimumDistance2() {
assertThat(
new Solution()
.minimumDistance(
CommonUtils
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
"[1,1],[1,1],[1,1]")),
equalTo(0));
}
}
Loading