Skip to content

Added tasks 3101, 3102 #631

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 1 commit into from
Apr 23, 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_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
}
}
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,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<IntArray>, 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<IntArray>, 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<IntArray>): 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])
)
}
}
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,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)
)
}
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
}