Skip to content

Commit f560023

Browse files
authored
Added tasks 3101, 3102
1 parent ee9613e commit f560023

File tree

6 files changed

+219
-0
lines changed

6 files changed

+219
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3101_3200.s3101_count_alternating_subarrays
2+
3+
// #Medium #Array #Math #2024_04_23_Time_499_ms_(97.78%)_Space_70.3_MB_(80.00%)
4+
5+
class Solution {
6+
fun countAlternatingSubarrays(nums: IntArray): Long {
7+
var count: Long = 0
8+
var length: Long
9+
var start = 0
10+
var end = 1
11+
while (end < nums.size) {
12+
if (nums[end] != nums[end - 1]) {
13+
end++
14+
} else {
15+
length = end - start.toLong()
16+
count += (length * (length + 1)) / 2
17+
start = end
18+
end++
19+
}
20+
}
21+
length = end - start.toLong()
22+
count += (length * (length + 1)) / 2
23+
return count
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3101\. Count Alternating Subarrays
2+
3+
Medium
4+
5+
You are given a binary array `nums`.
6+
7+
We call a subarray **alternating** if **no** two **adjacent** elements in the subarray have the **same** value.
8+
9+
Return _the number of alternating subarrays in_ `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [0,1,1,1]
14+
15+
**Output:** 5
16+
17+
**Explanation:**
18+
19+
The following subarrays are alternating: `[0]`, `[1]`, `[1]`, `[1]`, and `[0,1]`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,0,1,0]
24+
25+
**Output:** 10
26+
27+
**Explanation:**
28+
29+
Every subarray of the array is alternating. There are 10 possible subarrays that we can choose.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
34+
* `nums[i]` is either `0` or `1`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g3101_3200.s3102_minimize_manhattan_distances
2+
3+
// #Hard #Array #Math #2024_04_23_Time_701_ms_(95.83%)_Space_100.4_MB_(75.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
private fun manhattan(points: Array<IntArray>, i: Int, j: Int): Int {
11+
return (
12+
abs(points[i][0] - points[j][0]) + abs(
13+
points[i][1] - points[j][1]
14+
)
15+
)
16+
}
17+
18+
private fun maxManhattanDistance(points: Array<IntArray>, remove: Int): IntArray {
19+
val n = points.size
20+
var maxSum = Int.MIN_VALUE
21+
var minSum = Int.MAX_VALUE
22+
var maxDiff = Int.MIN_VALUE
23+
var minDiff = Int.MAX_VALUE
24+
var maxSumIndex = -1
25+
var minSumIndex = -1
26+
var maxDiffIndex = -1
27+
var minDiffIndex = -1
28+
for (i in 0 until n) {
29+
if (i != remove) {
30+
val sum = points[i][0] + points[i][1]
31+
val diff = points[i][0] - points[i][1]
32+
if (sum > maxSum) {
33+
maxSumIndex = i
34+
maxSum = sum
35+
}
36+
if (sum < minSum) {
37+
minSumIndex = i
38+
minSum = sum
39+
}
40+
if (diff > maxDiff) {
41+
maxDiffIndex = i
42+
maxDiff = diff
43+
}
44+
if (diff < minDiff) {
45+
minDiffIndex = i
46+
minDiff = diff
47+
}
48+
}
49+
}
50+
return if (max(maxSum - minSum, maxDiff - minDiff) == maxSum - minSum
51+
) intArrayOf(maxSumIndex, minSumIndex)
52+
else intArrayOf(maxDiffIndex, minDiffIndex)
53+
}
54+
55+
fun minimumDistance(points: Array<IntArray>): Int {
56+
val m = maxManhattanDistance(points, -1)
57+
val m1 = maxManhattanDistance(points, m[0])
58+
val m2 = maxManhattanDistance(points, m[1])
59+
return min(
60+
manhattan(points, m1[0], m1[1]),
61+
manhattan(points, m2[0], m2[1])
62+
)
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3102\. Minimize Manhattan Distances
2+
3+
Hard
4+
5+
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>.
6+
7+
The distance between two points is defined as their Manhattan distance.
8+
9+
Return _the **minimum** possible value for **maximum** distance between any two points by removing exactly one point_.
10+
11+
**Example 1:**
12+
13+
**Input:** points = [[3,10],[5,15],[10,2],[4,4]]
14+
15+
**Output:** 12
16+
17+
**Explanation:**
18+
19+
The maximum distance after removing each point is the following:
20+
21+
* 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`.
22+
* 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`.
23+
* 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`.
24+
* 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`.
25+
26+
12 is the minimum possible maximum distance between any two points after removing exactly one point.
27+
28+
**Example 2:**
29+
30+
**Input:** points = [[1,1],[1,1],[1,1]]
31+
32+
**Output:** 0
33+
34+
**Explanation:**
35+
36+
Removing any of the points results in the maximum distance between any two points of 0.
37+
38+
**Constraints:**
39+
40+
* <code>3 <= points.length <= 10<sup>5</sup></code>
41+
* `points[i].length == 2`
42+
* <code>1 <= points[i][0], points[i][1] <= 10<sup>8</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3101_3200.s3101_count_alternating_subarrays
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun countAlternatingSubarrays() {
10+
assertThat(Solution().countAlternatingSubarrays(intArrayOf(0, 1, 1, 1)), equalTo(5L))
11+
}
12+
13+
@Test
14+
fun countAlternatingSubarrays2() {
15+
assertThat(
16+
Solution().countAlternatingSubarrays(intArrayOf(1, 0, 1, 0)),
17+
equalTo(10L)
18+
)
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g3101_3200.s3102_minimize_manhattan_distances
2+
3+
import com_github_leetcode.CommonUtils
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun minimumDistance() {
11+
assertThat(
12+
Solution()
13+
.minimumDistance(
14+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
15+
"[3,10],[5,15],[10,2],[4,4]"
16+
)
17+
),
18+
equalTo(12)
19+
)
20+
}
21+
22+
@Test
23+
fun minimumDistance2() {
24+
assertThat(
25+
Solution()
26+
.minimumDistance(
27+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
28+
"[1,1],[1,1],[1,1]"
29+
)
30+
),
31+
equalTo(0)
32+
)
33+
}
34+
}

0 commit comments

Comments
 (0)