diff --git a/src/main/java/g3101_3200/s3101_count_alternating_subarrays/Solution.java b/src/main/java/g3101_3200/s3101_count_alternating_subarrays/Solution.java new file mode 100644 index 000000000..693a6c543 --- /dev/null +++ b/src/main/java/g3101_3200/s3101_count_alternating_subarrays/Solution.java @@ -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; + } +} diff --git a/src/main/java/g3101_3200/s3101_count_alternating_subarrays/readme.md b/src/main/java/g3101_3200/s3101_count_alternating_subarrays/readme.md new file mode 100644 index 000000000..8adf5d9dd --- /dev/null +++ b/src/main/java/g3101_3200/s3101_count_alternating_subarrays/readme.md @@ -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:** + +* 1 <= nums.length <= 105 +* `nums[i]` is either `0` or `1`. \ No newline at end of file diff --git a/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/Solution.java b/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/Solution.java new file mode 100644 index 000000000..e5d6a0318 --- /dev/null +++ b/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/Solution.java @@ -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])); + } +} diff --git a/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/readme.md b/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/readme.md new file mode 100644 index 000000000..3a8870a56 --- /dev/null +++ b/src/main/java/g3101_3200/s3102_minimize_manhattan_distances/readme.md @@ -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 points[i] = [xi, yi]. + +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 0th point the maximum distance is between points (5, 15) and (10, 2), which is `|5 - 10| + |15 - 2| = 18`. +* After removing the 1st point the maximum distance is between points (3, 10) and (10, 2), which is `|3 - 10| + |10 - 2| = 15`. +* After removing the 2nd point the maximum distance is between points (5, 15) and (4, 4), which is `|5 - 4| + |15 - 4| = 12`. +* After removing the 3rd 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:** + +* 3 <= points.length <= 105 +* `points[i].length == 2` +* 1 <= points[i][0], points[i][1] <= 108 \ No newline at end of file diff --git a/src/test/java/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.java b/src/test/java/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.java new file mode 100644 index 000000000..6ce6e1793 --- /dev/null +++ b/src/test/java/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.java @@ -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)); + } +} diff --git a/src/test/java/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.java b/src/test/java/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.java new file mode 100644 index 000000000..443c28085 --- /dev/null +++ b/src/test/java/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.java @@ -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)); + } +}