diff --git a/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.kt b/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.kt new file mode 100644 index 000000000..77b7c616d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.kt @@ -0,0 +1,21 @@ +package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k + +// #Easy #Array #Hash_Table #2024_12_08_Time_191_ms_(100.00%)_Space_39.9_MB_(100.00%) + +class Solution { + fun minOperations(nums: IntArray, k: Int): Int { + val s: MutableSet = HashSet() + for (i in nums) { + s.add(i) + } + var res = 0 + for (i in s) { + if (i!! > k) { + res++ + } else if (i < k) { + return -1 + } + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md b/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md new file mode 100644 index 000000000..80852a760 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md @@ -0,0 +1,52 @@ +3375\. Minimum Operations to Make Array Values Equal to K + +Easy + +You are given an integer array `nums` and an integer `k`. + +An integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are _identical_. + +For example, if `nums = [10, 8, 10, 8]`, a **valid** integer is `h = 9` because all `nums[i] > 9` are equal to 10, but 5 is not a **valid** integer. + +You are allowed to perform the following operation on `nums`: + +* Select an integer `h` that is _valid_ for the **current** values in `nums`. +* For each index `i` where `nums[i] > h`, set `nums[i]` to `h`. + +Return the **minimum** number of operations required to make every element in `nums` **equal** to `k`. If it is impossible to make all elements equal to `k`, return -1. + +**Example 1:** + +**Input:** nums = [5,2,5,4,5], k = 2 + +**Output:** 2 + +**Explanation:** + +The operations can be performed in order using valid integers 4 and then 2. + +**Example 2:** + +**Input:** nums = [2,1,2], k = 2 + +**Output:** \-1 + +**Explanation:** + +It is impossible to make all the values equal to 2. + +**Example 3:** + +**Input:** nums = [9,7,5,3], k = 1 + +**Output:** 4 + +**Explanation:** + +The operations can be performed using valid integers in the order 7, 5, 3, and 1. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` +* `1 <= k <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.kt b/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.kt new file mode 100644 index 000000000..46db4f9f3 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.kt @@ -0,0 +1,44 @@ +package g3301_3400.s3376_minimum_time_to_break_locks_i + +// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask +// #2024_12_08_Time_202_ms_(100.00%)_Space_40_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun findMinimumTime(strength: List, k: Int): Int { + val perm: MutableList = ArrayList(strength) + perm.sort() + var minTime = Int.Companion.MAX_VALUE + do { + var time = 0 + var factor = 1 + for (required in perm) { + val neededTime = (required + factor - 1) / factor + time += neededTime + factor += k + } + minTime = min(minTime, time) + } while (nextPermutation(perm)) + return minTime + } + + private fun nextPermutation(nums: MutableList): Boolean { + var i = nums.size - 2 + while (i >= 0 && nums[i] >= nums[i + 1]) { + i-- + } + if (i < 0) { + return false + } + var j = nums.size - 1 + while (nums[j] <= nums[i]) { + j-- + } + val temp = nums[i] + nums[i] = nums[j] + nums[j] = temp + nums.subList(i + 1, nums.size).reverse() + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md b/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md new file mode 100644 index 000000000..332149eea --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md @@ -0,0 +1,61 @@ +3376\. Minimum Time to Break Locks I + +Medium + +Bob is stuck in a dungeon and must break `n` locks, each requiring some amount of **energy** to break. The required energy for each lock is stored in an array called `strength` where `strength[i]` indicates the energy needed to break the ith lock. + +To break a lock, Bob uses a sword with the following characteristics: + +* The initial energy of the sword is 0. +* The initial factor `X` by which the energy of the sword increases is 1. +* Every minute, the energy of the sword increases by the current factor `X`. +* To break the ith lock, the energy of the sword must reach **at least** `strength[i]`. +* After breaking a lock, the energy of the sword resets to 0, and the factor `X` increases by a given value `K`. + +Your task is to determine the **minimum** time in minutes required for Bob to break all `n` locks and escape the dungeon. + +Return the **minimum** time required for Bob to break all `n` locks. + +**Example 1:** + +**Input:** strength = [3,4,1], K = 1 + +**Output:** 4 + +**Explanation:** + +| Time | Energy | X | Action | Updated X | +|------|--------|---|----------------------|-----------| +| 0 | 0 | 1 | Nothing | 1 | +| 1 | 1 | 1 | Break 3rd Lock | 2 | +| 2 | 2 | 2 | Nothing | 2 | +| 3 | 4 | 2 | Break 2nd Lock | 3 | +| 4 | 3 | 3 | Break 1st Lock | 3 | + +The locks cannot be broken in less than 4 minutes; thus, the answer is 4. + +**Example 2:** + +**Input:** strength = [2,5,4], K = 2 + +**Output:** 5 + +**Explanation:** + +| Time | Energy | X | Action | Updated X | +|------|--------|---|----------------------|-----------| +| 0 | 0 | 1 | Nothing | 1 | +| 1 | 1 | 1 | Nothing | 1 | +| 2 | 2 | 1 | Break 1st Lock | 3 | +| 3 | 3 | 3 | Nothing | 3 | +| 4 | 6 | 3 | Break 2nd Lock | 5 | +| 5 | 5 | 5 | Break 3rd Lock | 7 | + +The locks cannot be broken in less than 5 minutes; thus, the answer is 5. + +**Constraints:** + +* `n == strength.length` +* `1 <= n <= 8` +* `1 <= K <= 10` +* 1 <= strength[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.kt b/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.kt new file mode 100644 index 000000000..b5a0a8ec5 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.kt @@ -0,0 +1,60 @@ +package g3301_3400.s3377_digit_operations_to_make_two_integers_equal + +// #Medium #Math #Heap_Priority_Queue #Graph #Shortest_Path #Number_Theory +// #2024_12_08_Time_215_ms_(100.00%)_Space_40.7_MB_(100.00%) + +import java.util.PriorityQueue + +class Solution { + fun minOperations(n: Int, m: Int): Int { + val limit = 100000 + val sieve = BooleanArray(limit + 1) + val visited = BooleanArray(limit) + sieve.fill(true) + sieve[0] = false + sieve[1] = false + var i = 2 + while (i * i <= limit) { + if (sieve[i]) { + var j = i * i + while (j <= limit) { + sieve[j] = false + j += i + } + } + i++ + } + if (sieve[n]) { + return -1 + } + val pq = PriorityQueue(Comparator { a: IntArray, b: IntArray -> a[0] - b[0] }) + visited[n] = true + pq.add(intArrayOf(n, n)) + while (pq.isNotEmpty()) { + val current = pq.poll() + val cost = current[0] + val num = current[1] + val temp = num.toString().toCharArray() + if (num == m) { + return cost + } + for (j in temp.indices) { + val old = temp[j] + for (i in -1..1) { + val digit = old.code - '0'.code + if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) { + continue + } + temp[j] = (i + digit + '0'.code).toChar() + val newNum = String(temp).toInt() + if (!sieve[newNum] && !visited[newNum]) { + visited[newNum] = true + pq.add(intArrayOf(cost + newNum, newNum)) + } + } + temp[j] = old + } + } + return -1 + } +} diff --git a/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md b/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md new file mode 100644 index 000000000..bd6d91cc9 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md @@ -0,0 +1,58 @@ +3377\. Digit Operations to Make Two Integers Equal + +Medium + +You are given two integers `n` and `m` that consist of the **same** number of digits. + +You can perform the following operations **any** number of times: + +* Choose **any** digit from `n` that is not 9 and **increase** it by 1. +* Choose **any** digit from `n` that is not 0 and **decrease** it by 1. + +The integer `n` must not be a **prime** number at any point, including its original value and after each operation. + +The cost of a transformation is the sum of **all** values that `n` takes throughout the operations performed. + +Return the **minimum** cost to transform `n` into `m`. If it is impossible, return -1. + +A prime number is a natural number greater than 1 with only two factors, 1 and itself. + +**Example 1:** + +**Input:** n = 10, m = 12 + +**Output:** 85 + +**Explanation:** + +We perform the following operations: + +* Increase the first digit, now n = **2**0. +* Increase the second digit, now n = 2**1**. +* Increase the second digit, now n = 2**2**. +* Decrease the first digit, now n = **1**2. + +**Example 2:** + +**Input:** n = 4, m = 8 + +**Output:** \-1 + +**Explanation:** + +It is impossible to make `n` equal to `m`. + +**Example 3:** + +**Input:** n = 6, m = 2 + +**Output:** \-1 + +**Explanation:** + +Since 2 is already a prime, we can't make `n` equal to `m`. + +**Constraints:** + +* 1 <= n, m < 104 +* `n` and `m` consist of the same number of digits. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.kt b/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.kt new file mode 100644 index 000000000..951c5b3ab --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.kt @@ -0,0 +1,59 @@ +package g3301_3400.s3378_count_connected_components_in_lcm_graph + +// #Hard #Array #Hash_Table #Math #Union_Find #Number_Theory +// #2024_12_08_Time_58_ms_(100.00%)_Space_54.4_MB_(100.00%) + +class Solution { + private class UnionFind(n: Int) { + var parent = IntArray(n) { it } + var rank = IntArray(n) + var totalComponents = n + + fun find(u: Int): Int { + if (parent[u] == u) { + return u + } + parent[u] = find(parent[u]) + return parent[u] + } + + fun union(u: Int, v: Int) { + val parentU = find(u) + val parentV = find(v) + if (parentU != parentV) { + totalComponents-- + when { + rank[parentU] == rank[parentV] -> { + parent[parentV] = parentU + rank[parentU]++ + } + rank[parentU] > rank[parentV] -> parent[parentV] = parentU + else -> parent[parentU] = parentV + } + } + } + } + + fun countComponents(nums: IntArray, threshold: Int): Int { + val goodNums = nums.filter { it <= threshold } + val totalNums = nums.size + if (goodNums.isEmpty()) { + return totalNums + } + val uf = UnionFind(goodNums.size) + val presentElements = IntArray(threshold + 1) { -1 } + goodNums.forEachIndexed { index, num -> + presentElements[num] = index + } + for (d in goodNums) { + for (i in d..threshold step d) { + if (presentElements[i] == -1) { + presentElements[i] = presentElements[d] + } else if (presentElements[i] != presentElements[d]) { + uf.union(presentElements[i], presentElements[d]) + } + } + } + return uf.totalComponents + totalNums - goodNums.size + } +} diff --git a/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md b/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md new file mode 100644 index 000000000..a2103761b --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/readme.md @@ -0,0 +1,44 @@ +3378\. Count Connected Components in LCM Graph + +Hard + +You are given an array of integers `nums` of size `n` and a **positive** integer `threshold`. + +There is a graph consisting of `n` nodes with the ith node having a value of `nums[i]`. Two nodes `i` and `j` in the graph are connected via an **undirected** edge if `lcm(nums[i], nums[j]) <= threshold`. + +Return the number of **connected components** in this graph. + +A **connected component** is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph. + +The term `lcm(a, b)` denotes the **least common multiple** of `a` and `b`. + +**Example 1:** + +**Input:** nums = [2,4,8,3,9], threshold = 5 + +**Output:** 4 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/31/example0.png) + +The four connected components are `(2, 4)`, `(3)`, `(8)`, `(9)`. + +**Example 2:** + +**Input:** nums = [2,4,8,3,9,12], threshold = 10 + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/31/example1.png) + +The two connected components are `(2, 3, 4, 8, 9)`, and `(12)`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* All elements of `nums` are unique. +* 1 <= threshold <= 2 * 105 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3379_transformed_array/Solution.kt b/src/main/kotlin/g3301_3400/s3379_transformed_array/Solution.kt new file mode 100644 index 000000000..be30c6195 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3379_transformed_array/Solution.kt @@ -0,0 +1,21 @@ +package g3301_3400.s3379_transformed_array + +// #Easy #Array #Simulation #2024_12_10_Time_206_ms_(84.38%)_Space_38.6_MB_(75.00%) + +import kotlin.math.abs + +class Solution { + fun constructTransformedArray(nums: IntArray): IntArray { + val n = nums.size + val res = IntArray(n) + for (i in 0.. 0) { + res[i] = nums[(i + nums[i]) % n] + } else if (nums[i] < 0) { + val r: Int = abs(nums[i]) / n + res[i] = nums[abs(i + nums[i] + r * n + n) % n] + } + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3379_transformed_array/readme.md b/src/main/kotlin/g3301_3400/s3379_transformed_array/readme.md new file mode 100644 index 000000000..f2e5ad44a --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3379_transformed_array/readme.md @@ -0,0 +1,45 @@ +3379\. Transformed Array + +Easy + +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: + +For each index `i` (where `0 <= i < nums.length`), perform the following **independent** actions: + +* 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. +* 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. +* If `nums[i] == 0`: Set `result[i]` to `nums[i]`. + +Return the new array `result`. + +**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. + +**Example 1:** + +**Input:** nums = [3,-2,1,1] + +**Output:** [1,1,1,3] + +**Explanation:** + +* 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. +* 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. +* 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. +* 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. + +**Example 2:** + +**Input:** nums = [-1,4,-1] + +**Output:** [-1,-1,4] + +**Explanation:** + +* 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. +* 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. +* 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. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `-100 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.kt b/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.kt new file mode 100644 index 000000000..1187e08ad --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/Solution.kt @@ -0,0 +1,52 @@ +package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i + +// #Medium #Array #Math #Sorting #Enumeration #Geometry #Segment_Tree #Binary_Indexed_Tree +// #2024_12_10_Time_10_ms_(94.74%)_Space_40.1_MB_(84.21%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maxRectangleArea(points: Array): Int { + val set: MutableSet = HashSet() + for (p in points) { + set.add(p.contentToString()) + } + var maxArea = -1 + for (point in points) { + for (j in 1.., p1: IntArray, p2: IntArray): Boolean { + val top = max(p1[1], p2[1]) + val bot = min(p1[1], p2[1]) + val left = min(p1[0], p2[0]) + val right = max(p1[0], p2[0]) + for (p in points) { + val x = p[0] + val y = p[1] + if ((y == top || y == bot) && x > left && x < right || + (x == left || x == right) && y > bot && y < top || + (x > left && x < right && y > bot && y < top) + ) { + return false + } + } + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md b/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md new file mode 100644 index 000000000..8c15f8115 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/readme.md @@ -0,0 +1,56 @@ +3380\. Maximum Area Rectangle With Point Constraints I + +Medium + +You are given an array `points` where points[i] = [xi, yi] represents the coordinates of a point on an infinite plane. + +Your task is to find the **maximum** area of a rectangle that: + +* Can be formed using **four** of these points as its corners. +* Does **not** contain any other point inside or on its border. +* Has its edges **parallel** to the axes. + +Return the **maximum area** that you can obtain or -1 if no such rectangle is possible. + +**Example 1:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3]] + +**Output:** 4 + +**Explanation:** + +**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)** + +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. + +**Example 2:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3],[2,2]] + +**Output:** \-1 + +**Explanation:** + +**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)** + +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. + +**Example 3:** + +**Input:** points = [[1,1],[1,3],[3,1],[3,3],[1,2],[3,2]] + +**Output:** 2 + +**Explanation:** + +**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)** + +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. + +**Constraints:** + +* `1 <= points.length <= 10` +* `points[i].length == 2` +* 0 <= xi, yi <= 100 +* All the given points are **unique**. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.kt b/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.kt new file mode 100644 index 000000000..e48b271f3 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/Solution.kt @@ -0,0 +1,26 @@ +package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k + +// #Medium #Array #Hash_Table #Prefix_Sum #2024_12_10_Time_6_ms_(100.00%)_Space_83.5_MB_(69.23%) + +import kotlin.math.max + +class Solution { + fun maxSubarraySum(nums: IntArray, k: Int): Long { + val n = nums.size + val maxSum = LongArray(n) + var minSum: Long = 0 + for (i in n - 1 downTo n - k + 1) { + maxSum[i] = Int.Companion.MIN_VALUE.toLong() + minSum += nums[i] + } + minSum += nums[n - k] + maxSum[n - k] = minSum + var ans = minSum + for (i in n - k - 1 downTo 0) { + minSum = minSum + nums[i] - nums[i + k] + maxSum[i] = max(minSum, (minSum + maxSum[i + k])) + ans = max(maxSum[i], ans) + } + return ans + } +} diff --git a/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md b/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md new file mode 100644 index 000000000..e862ba6cd --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/readme.md @@ -0,0 +1,44 @@ +3381\. Maximum Subarray Sum With Length Divisible by K + +Medium + +You are given an array of integers `nums` and an integer `k`. + +Return the **maximum** sum of a **non-empty subarray** of `nums`, such that the size of the subarray is **divisible** by `k`. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [1,2], k = 1 + +**Output:** 3 + +**Explanation:** + +The subarray `[1, 2]` with sum 3 has length equal to 2 which is divisible by 1. + +**Example 2:** + +**Input:** nums = [-1,-2,-3,-4,-5], k = 4 + +**Output:** \-10 + +**Explanation:** + +The maximum sum subarray is `[-1, -2, -3, -4]` which has length equal to 4 which is divisible by 4. + +**Example 3:** + +**Input:** nums = [-5,1,2,-3,4], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum sum subarray is `[1, 2, -3, 4]` which has length equal to 4 which is divisible by 2. + +**Constraints:** + +* 1 <= k <= nums.length <= 2 * 105 +* -109 <= nums[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.kt new file mode 100644 index 000000000..76d4ff3b3 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/Solution.kt @@ -0,0 +1,52 @@ +package g3301_3400.s3382_maximum_area_rectangle_with_point_constraints_ii + +// #Hard #Array #Math #Sorting #Geometry #Segment_Tree #Binary_Indexed_Tree +// #2024_12_10_Time_518_ms_(100.00%)_Space_103.7_MB_(100.00%) + +import java.util.TreeSet +import kotlin.math.max + +class Solution { + fun maxRectangleArea(xCoord: IntArray, yCoord: IntArray): Long { + if (xCoord.size < 4) { + return -1 + } + val pair = xCoord.zip(yCoord) { x, y -> Pair(x, y) }.toTypedArray() + pair.sort() + val map = HashMap() + val yVals = TreeSet() + var best: Long = -1 + for (i in 0.. { + override fun compareTo(p: Pair): Int { + return if (x == p.x) y - p.y else x - p.x + } + } +} diff --git a/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md b/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md new file mode 100644 index 000000000..69de05a07 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md @@ -0,0 +1,55 @@ +3382\. Maximum Area Rectangle With Point Constraints II + +Hard + +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 ith point. + +Your task is to find the **maximum** area of a rectangle that: + +* Can be formed using **four** of these points as its corners. +* Does **not** contain any other point inside or on its border. +* Has its edges **parallel** to the axes. + +Return the **maximum area** that you can obtain or -1 if no such rectangle is possible. + +**Example 1:** + +**Input:** xCoord = [1,1,3,3], yCoord = [1,3,1,3] + +**Output:** 4 + +**Explanation:** + +**![Example 1 diagram](https://assets.leetcode.com/uploads/2024/11/02/example1.png)** + +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. + +**Example 2:** + +**Input:** xCoord = [1,1,3,3,2], yCoord = [1,3,1,3,2] + +**Output:** \-1 + +**Explanation:** + +**![Example 2 diagram](https://assets.leetcode.com/uploads/2024/11/02/example2.png)** + +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. + +**Example 3:** + +**Input:** xCoord = [1,1,3,3,1,3], yCoord = [1,3,1,3,2,2] + +**Output:** 2 + +**Explanation:** + +**![Example 3 diagram](https://assets.leetcode.com/uploads/2024/11/02/example3.png)** + +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. + +**Constraints:** + +* 1 <= xCoord.length == yCoord.length <= 2 * 105 +* 0 <= xCoord[i], yCoord[i] <= 8 * 107 +* All the given points are **unique**. \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.kt new file mode 100644 index 000000000..3ac5255d6 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minOperations() { + assertThat( + Solution().minOperations(intArrayOf(5, 2, 5, 4, 5), 2), + equalTo(2), + ) + } + + @Test + fun minOperations2() { + assertThat(Solution().minOperations(intArrayOf(2, 1, 2), 2), equalTo(-1)) + } + + @Test + fun minOperations3() { + assertThat( + Solution().minOperations(intArrayOf(9, 7, 5, 3), 1), + equalTo(4), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.kt new file mode 100644 index 000000000..d5ae8dd52 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3376_minimum_time_to_break_locks_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findMinimumTime() { + assertThat( + Solution().findMinimumTime(mutableListOf(3, 4, 1), 1), + equalTo(4), + ) + } + + @Test + fun findMinimumTime2() { + assertThat( + Solution().findMinimumTime(mutableListOf(2, 5, 4), 2), + equalTo(5), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.kt new file mode 100644 index 000000000..c666ff8b7 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/SolutionTest.kt @@ -0,0 +1,27 @@ +package g3301_3400.s3377_digit_operations_to_make_two_integers_equal + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minOperations() { + assertThat(Solution().minOperations(10, 12), equalTo(85)) + } + + @Test + fun minOperations2() { + assertThat(Solution().minOperations(4, 8), equalTo(-1)) + } + + @Test + fun minOperations3() { + assertThat(Solution().minOperations(6, 2), equalTo(-1)) + } + + @Test + fun minOperations4() { + assertThat(Solution().minOperations(17, 72), equalTo(-1)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.kt new file mode 100644 index 000000000..7b7fa1d25 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3378_count_connected_components_in_lcm_graph + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countComponents() { + assertThat( + Solution().countComponents(intArrayOf(2, 4, 8, 3, 9), 5), + equalTo(4), + ) + } + + @Test + fun countComponents2() { + assertThat( + Solution().countComponents(intArrayOf(2, 4, 8, 3, 9, 12), 10), + equalTo(2), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3379_transformed_array/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3379_transformed_array/SolutionTest.kt new file mode 100644 index 000000000..0da477931 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3379_transformed_array/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3379_transformed_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun constructTransformedArray() { + assertThat( + Solution().constructTransformedArray(intArrayOf(3, -2, 1, 1)), + equalTo(intArrayOf(1, 1, 1, 3)), + ) + } + + @Test + fun constructTransformedArray2() { + assertThat( + Solution().constructTransformedArray(intArrayOf(-1, 4, -1)), + equalTo(intArrayOf(-1, -1, 4)), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.kt new file mode 100644 index 000000000..e0ed9531b --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i/SolutionTest.kt @@ -0,0 +1,57 @@ +package g3301_3400.s3380_maximum_area_rectangle_with_point_constraints_i + +import org.hamcrest.CoreMatchers +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxRectangleArea() { + assertThat( + Solution().maxRectangleArea( + arrayOf( + intArrayOf(1, 1), + intArrayOf(1, 3), + intArrayOf(3, 1), + intArrayOf(3, 3), + ), + ), + CoreMatchers.equalTo(4), + ) + } + + @Test + fun maxRectangleArea2() { + assertThat( + Solution() + .maxRectangleArea( + arrayOf( + intArrayOf(1, 1), + intArrayOf(1, 3), + intArrayOf(3, 1), + intArrayOf(3, 3), + intArrayOf(2, 2), + ), + ), + CoreMatchers.equalTo(-1), + ) + } + + @Test + fun maxRectangleArea3() { + assertThat( + Solution() + .maxRectangleArea( + arrayOf( + intArrayOf(1, 1), + intArrayOf(1, 3), + intArrayOf(3, 1), + intArrayOf(3, 3), + intArrayOf(1, 2), + intArrayOf(3, 2), + ), + ), + CoreMatchers.equalTo(2), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.kt new file mode 100644 index 000000000..4ce9916cc --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3301_3400.s3381_maximum_subarray_sum_with_length_divisible_by_k + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSubarraySum() { + assertThat(Solution().maxSubarraySum(intArrayOf(1, 2), 1), equalTo(3L)) + } + + @Test + fun maxSubarraySum2() { + assertThat( + Solution().maxSubarraySum(intArrayOf(-1, -2, -3, -4, -5), 4), + equalTo(-10L), + ) + } + + @Test + fun maxSubarraySum3() { + assertThat( + Solution().maxSubarraySum(intArrayOf(-5, 1, 2, -3, 4), 2), + equalTo(4L), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.kt new file mode 100644 index 000000000..010555454 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/SolutionTest.kt @@ -0,0 +1,36 @@ +package g3301_3400.s3382_maximum_area_rectangle_with_point_constraints_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxRectangleArea() { + assertThat( + Solution().maxRectangleArea(intArrayOf(1, 1, 3, 3), intArrayOf(1, 3, 1, 3)), + equalTo(4L), + ) + } + + @Test + fun maxRectangleArea2() { + assertThat( + Solution() + .maxRectangleArea(intArrayOf(1, 1, 3, 3, 2), intArrayOf(1, 3, 1, 3, 2)), + equalTo(-1L), + ) + } + + @Test + fun maxRectangleArea3() { + assertThat( + Solution() + .maxRectangleArea( + intArrayOf(1, 1, 3, 3, 1, 3), + intArrayOf(1, 3, 1, 3, 2, 2), + ), + equalTo(2L), + ) + } +}