Skip to content

Commit b08e5c3

Browse files
committed
Added tasks 3379-3382
1 parent 38f9ae7 commit b08e5c3

File tree

12 files changed

+500
-0
lines changed

12 files changed

+500
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3301_3400.s3379_transformed_array
2+
3+
// #Easy #Array #Simulation #2024_12_10_Time_206_ms_(84.38%)_Space_38.6_MB_(75.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun constructTransformedArray(nums: IntArray): IntArray {
9+
val n = nums.size
10+
val res = IntArray(n)
11+
for (i in 0..<n) {
12+
if (nums[i] > 0) {
13+
res[i] = nums[(i + nums[i]) % n]
14+
} else if (nums[i] < 0) {
15+
val r: Int = abs(nums[i]) / n
16+
res[i] = nums[abs(i + nums[i] + r * n + n) % n]
17+
}
18+
}
19+
return res
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3379\. Transformed Array
2+
3+
Easy
4+
5+
You are given an integer array `nums` that represents a circular array. Your task is to create a new array `result` of the **same** size, following these rules:
6+
7+
For each index `i` (where `0 <= i < nums.length`), perform the following **independent** actions:
8+
9+
* If `nums[i] > 0`: Start at index `i` and move `nums[i]` steps to the **right** in the circular array. Set `result[i]` to the value of the index where you land.
10+
* If `nums[i] < 0`: Start at index `i` and move `abs(nums[i])` steps to the **left** in the circular array. Set `result[i]` to the value of the index where you land.
11+
* If `nums[i] == 0`: Set `result[i]` to `nums[i]`.
12+
13+
Return the new array `result`.
14+
15+
**Note:** Since `nums` is circular, moving past the last element wraps around to the beginning, and moving before the first element wraps back to the end.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,-2,1,1]
20+
21+
**Output:** [1,1,1,3]
22+
23+
**Explanation:**
24+
25+
* For `nums[0]` that is equal to 3, If we move 3 steps to right, we reach `nums[3]`. So `result[0]` should be 1.
26+
* For `nums[1]` that is equal to -2, If we move 2 steps to left, we reach `nums[3]`. So `result[1]` should be 1.
27+
* For `nums[2]` that is equal to 1, If we move 1 step to right, we reach `nums[3]`. So `result[2]` should be 1.
28+
* For `nums[3]` that is equal to 1, If we move 1 step to right, we reach `nums[0]`. So `result[3]` should be 3.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [-1,4,-1]
33+
34+
**Output:** [-1,-1,4]
35+
36+
**Explanation:**
37+
38+
* For `nums[0]` that is equal to -1, If we move 1 step to left, we reach `nums[2]`. So `result[0]` should be -1.
39+
* For `nums[1]` that is equal to 4, If we move 4 steps to right, we reach `nums[2]`. So `result[1]` should be -1.
40+
* For `nums[2]` that is equal to -1, If we move 1 step to left, we reach `nums[1]`. So `result[2]` should be 4.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 100`
45+
* `-100 <= nums[i] <= 100`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i
2+
3+
// #Medium #Array #Math #Sorting #Enumeration #Geometry #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_12_10_Time_10_ms_(94.74%)_Space_40.1_MB_(84.21%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun maxRectangleArea(points: Array<IntArray>): Int {
11+
val set: MutableSet<String?> = HashSet<String?>()
12+
for (p in points) {
13+
set.add(p.contentToString())
14+
}
15+
var maxArea = -1
16+
for (point in points) {
17+
for (j in 1..<points.size) {
18+
val p2 = points[j]
19+
if (point[0] == p2[0] || point[1] == p2[1] || !set.contains(
20+
intArrayOf(
21+
point[0],
22+
p2[1],
23+
).contentToString(),
24+
) || !set.contains(intArrayOf(p2[0], point[1]).contentToString()) ||
25+
!validate(points, point, p2)
26+
) {
27+
continue
28+
}
29+
maxArea = max(maxArea, (p2[1] - point[1]) * (p2[0] - point[0]))
30+
}
31+
}
32+
return maxArea
33+
}
34+
35+
private fun validate(points: Array<IntArray>, p1: IntArray, p2: IntArray): Boolean {
36+
val top = max(p1[1], p2[1])
37+
val bot = min(p1[1], p2[1])
38+
val left = min(p1[0], p2[0])
39+
val right = max(p1[0], p2[0])
40+
for (p in points) {
41+
val x = p[0]
42+
val y = p[1]
43+
if ((y == top || y == bot) && x > left && x < right ||
44+
(x == left || x == right) && y > bot && y < top ||
45+
(x > left && x < right && y > bot && y < top)
46+
) {
47+
return false
48+
}
49+
}
50+
return true
51+
}
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3380\. Maximum Area Rectangle With Point Constraints I
2+
3+
Medium
4+
5+
You are given an array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinates of a point on an infinite plane.
6+
7+
Your task is to find the **maximum** area of a rectangle that:
8+
9+
* Can be formed using **four** of these points as its corners.
10+
* Does **not** contain any other point inside or on its border.
11+
* Has its edges **parallel** to the axes.
12+
13+
Return the **maximum area** that you can obtain or -1 if no such rectangle is possible.
14+
15+
**Example 1:**
16+
17+
**Input:** points = [[1,1],[1,3],[3,1],[3,3]]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)**
24+
25+
We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.
26+
27+
**Example 2:**
28+
29+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]]
30+
31+
**Output:** \-1
32+
33+
**Explanation:**
34+
35+
**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)**
36+
37+
There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1.
38+
39+
**Example 3:**
40+
41+
**Input:** points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]]
42+
43+
**Output:** 2
44+
45+
**Explanation:**
46+
47+
**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)**
48+
49+
The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area.
50+
51+
**Constraints:**
52+
53+
* `1 <= points.length <= 10`
54+
* `points[i].length == 2`
55+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 100</code>
56+
* All the given points are **unique**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #2024_12_10_Time_6_ms_(100.00%)_Space_83.5_MB_(69.23%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maxSubarraySum(nums: IntArray, k: Int): Long {
9+
val n = nums.size
10+
val maxSum = LongArray(n)
11+
var minSum: Long = 0
12+
for (i in n - 1 downTo n - k + 1) {
13+
maxSum[i] = Int.Companion.MIN_VALUE.toLong()
14+
minSum += nums[i]
15+
}
16+
minSum += nums[n - k]
17+
maxSum[n - k] = minSum
18+
var ans = minSum
19+
for (i in n - k - 1 downTo 0) {
20+
minSum = minSum + nums[i] - nums[i + k]
21+
maxSum[i] = max(minSum, (minSum + maxSum[i + k]))
22+
ans = max(maxSum[i], ans)
23+
}
24+
return ans
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3381\. Maximum Subarray Sum With Length Divisible by K
2+
3+
Medium
4+
5+
You are given an array of integers `nums` and an integer `k`.
6+
7+
Return the **maximum** sum of a **non-empty subarray** of `nums`, such that the size of the subarray is **divisible** by `k`.
8+
9+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2], k = 1
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
The subarray `[1, 2]` with sum 3 has length equal to 2 which is divisible by 1.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [-1,-2,-3,-4,-5], k = 4
24+
25+
**Output:** \-10
26+
27+
**Explanation:**
28+
29+
The maximum sum subarray is `[-1, -2, -3, -4]` which has length equal to 4 which is divisible by 4.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [-5,1,2,-3,4], k = 2
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
39+
The maximum sum subarray is `[1, 2, -3, 4]` which has length equal to 4 which is divisible by 2.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= k <= nums.length <= 2 * 10<sup>5</sup></code>
44+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g3301_3400.s3382_maximum_area_rectangle_with_point_constraints_ii
2+
3+
// #Hard #Array #Math #Sorting #Geometry #Segment_Tree #Binary_Indexed_Tree
4+
// #2024_12_10_Time_518_ms_(100.00%)_Space_103.7_MB_(100.00%)
5+
6+
import java.util.TreeSet
7+
import kotlin.math.max
8+
9+
class Solution {
10+
fun maxRectangleArea(xCoord: IntArray, yCoord: IntArray): Long {
11+
if (xCoord.size < 4) {
12+
return -1
13+
}
14+
val pair = Array<Pair>(xCoord.size) { i -> Pair(0, 0) }
15+
for (i in xCoord.indices) {
16+
val x0 = xCoord[i]
17+
val y0 = yCoord[i]
18+
pair[i] = Pair(x0, y0)
19+
}
20+
pair.sort()
21+
val map = HashMap<Int?, Pair>()
22+
val yVals = TreeSet<Int?>()
23+
var best: Long = -1
24+
for (i in 0..<pair.size - 1) {
25+
if (!yVals.isEmpty()) {
26+
val y0 = pair[i].y
27+
var y1 = yVals.floor(y0)
28+
while (y1 != null) {
29+
val p1: Pair = map.get(y1)!!
30+
if (p1.y < y0) {
31+
break
32+
}
33+
if (y1 == y0 && pair[i + 1].x == pair[i].x && pair[i + 1].y == p1.y) {
34+
val dY = p1.y - y0.toLong()
35+
val dX = pair[i].x - p1.x.toLong()
36+
best = max((dY * dX).toDouble(), best.toDouble()).toLong()
37+
}
38+
if (p1.x != pair[i].x) {
39+
yVals.remove(y1)
40+
}
41+
y1 = yVals.lower(y1)
42+
}
43+
}
44+
if (pair[i].x == pair[i + 1].x) {
45+
yVals.add(pair[i].y)
46+
map.put(pair[i].y, pair[i + 1])
47+
}
48+
}
49+
return best
50+
}
51+
52+
private class Pair(val x: Int, val y: Int) : Comparable<Pair> {
53+
override fun compareTo(p: Pair): Int {
54+
return if (x == p.x) y - p.y else x - p.x
55+
}
56+
}
57+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3382\. Maximum Area Rectangle With Point Constraints II
2+
3+
Hard
4+
5+
There are n points on an infinite plane. You are given two integer arrays `xCoord` and `yCoord` where `(xCoord[i], yCoord[i])` represents the coordinates of the <code>i<sup>th</sup></code> point.
6+
7+
Your task is to find the **maximum** area of a rectangle that:
8+
9+
* Can be formed using **four** of these points as its corners.
10+
* Does **not** contain any other point inside or on its border.
11+
* Has its edges **parallel** to the axes.
12+
13+
Return the **maximum area** that you can obtain or -1 if no such rectangle is possible.
14+
15+
**Example 1:**
16+
17+
**Input:** xCoord = [1,1,3,3], yCoord = [1,3,1,3]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)**
24+
25+
We can make a rectangle with these 4 points as corners and there is no other point that lies inside or on the border. Hence, the maximum possible area would be 4.
26+
27+
**Example 2:**
28+
29+
**Input:** xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2]
30+
31+
**Output:** \-1
32+
33+
**Explanation:**
34+
35+
**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)**
36+
37+
There is only one rectangle possible is with points `[1,1], [1,3], [3,1]` and `[3,3]` but `[2,2]` will always lie inside it. Hence, returning -1.
38+
39+
**Example 3:**
40+
41+
**Input:** xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,2]
42+
43+
**Output:** 2
44+
45+
**Explanation:**
46+
47+
**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)**
48+
49+
The maximum area rectangle is formed by the points `[1,3], [1,2], [3,2], [3,3]`, which has an area of 2. Additionally, the points `[1,1], [1,2], [3,1], [3,2]` also form a valid rectangle with the same area.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= xCoord.length == yCoord.length <= 2 * 10<sup>5</sup></code>
54+
* <code>0 <= xCoord[i], yCoord[i] <= 8 * 10<sup>7</sup></code>
55+
* All the given points are **unique**.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3379_transformed_array
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 constructTransformedArray() {
10+
assertThat<IntArray>(
11+
Solution().constructTransformedArray(intArrayOf(3, -2, 1, 1)),
12+
equalTo<IntArray>(intArrayOf(1, 1, 1, 3)),
13+
)
14+
}
15+
16+
@Test
17+
fun constructTransformedArray2() {
18+
assertThat<IntArray>(
19+
Solution().constructTransformedArray(intArrayOf(-1, 4, -1)),
20+
equalTo<IntArray>(intArrayOf(-1, -1, 4)),
21+
)
22+
}
23+
}

0 commit comments

Comments
 (0)