diff --git a/src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays/Solution.kt b/src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays/Solution.kt new file mode 100644 index 000000000..d21e55130 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays/Solution.kt @@ -0,0 +1,25 @@ +package g3101_3200.s3101_count_alternating_subarrays + +// #Medium #Array #Math #2024_04_23_Time_499_ms_(97.78%)_Space_70.3_MB_(80.00%) + +class Solution { + fun countAlternatingSubarrays(nums: IntArray): Long { + var count: Long = 0 + var length: Long + var start = 0 + var end = 1 + while (end < nums.size) { + if (nums[end] != nums[end - 1]) { + end++ + } else { + length = end - start.toLong() + count += (length * (length + 1)) / 2 + start = end + end++ + } + } + length = end - start.toLong() + count += (length * (length + 1)) / 2 + return count + } +} diff --git a/src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays/readme.md b/src/main/kotlin/g3101_3200/s3101_count_alternating_subarrays/readme.md new file mode 100644 index 000000000..8adf5d9dd --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3101_3200/s3102_minimize_manhattan_distances/Solution.kt b/src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances/Solution.kt new file mode 100644 index 000000000..e8aa86599 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances/Solution.kt @@ -0,0 +1,64 @@ +package g3101_3200.s3102_minimize_manhattan_distances + +// #Hard #Array #Math #2024_04_23_Time_701_ms_(95.83%)_Space_100.4_MB_(75.00%) + +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + private fun manhattan(points: Array, i: Int, j: Int): Int { + return ( + abs(points[i][0] - points[j][0]) + abs( + points[i][1] - points[j][1] + ) + ) + } + + private fun maxManhattanDistance(points: Array, remove: Int): IntArray { + val n = points.size + var maxSum = Int.MIN_VALUE + var minSum = Int.MAX_VALUE + var maxDiff = Int.MIN_VALUE + var minDiff = Int.MAX_VALUE + var maxSumIndex = -1 + var minSumIndex = -1 + var maxDiffIndex = -1 + var minDiffIndex = -1 + for (i in 0 until n) { + if (i != remove) { + val sum = points[i][0] + points[i][1] + val 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 if (max(maxSum - minSum, maxDiff - minDiff) == maxSum - minSum + ) intArrayOf(maxSumIndex, minSumIndex) + else intArrayOf(maxDiffIndex, minDiffIndex) + } + + fun minimumDistance(points: Array): Int { + val m = maxManhattanDistance(points, -1) + val m1 = maxManhattanDistance(points, m[0]) + val m2 = maxManhattanDistance(points, m[1]) + return min( + manhattan(points, m1[0], m1[1]), + manhattan(points, m2[0], m2[1]) + ) + } +} diff --git a/src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances/readme.md b/src/main/kotlin/g3101_3200/s3102_minimize_manhattan_distances/readme.md new file mode 100644 index 000000000..3a8870a56 --- /dev/null +++ b/src/main/kotlin/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/kotlin/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.kt new file mode 100644 index 000000000..6ff116d0a --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3101_count_alternating_subarrays/SolutionTest.kt @@ -0,0 +1,20 @@ +package g3101_3200.s3101_count_alternating_subarrays + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countAlternatingSubarrays() { + assertThat(Solution().countAlternatingSubarrays(intArrayOf(0, 1, 1, 1)), equalTo(5L)) + } + + @Test + fun countAlternatingSubarrays2() { + assertThat( + Solution().countAlternatingSubarrays(intArrayOf(1, 0, 1, 0)), + equalTo(10L) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.kt new file mode 100644 index 000000000..3928a2a6e --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3102_minimize_manhattan_distances/SolutionTest.kt @@ -0,0 +1,34 @@ +package g3101_3200.s3102_minimize_manhattan_distances + +import com_github_leetcode.CommonUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minimumDistance() { + assertThat( + Solution() + .minimumDistance( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[3,10],[5,15],[10,2],[4,4]" + ) + ), + equalTo(12) + ) + } + + @Test + fun minimumDistance2() { + assertThat( + Solution() + .minimumDistance( + CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray( + "[1,1],[1,1],[1,1]" + ) + ), + equalTo(0) + ) + } +}