diff --git a/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/Solution.kt b/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/Solution.kt new file mode 100644 index 000000000..c062dcb27 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/Solution.kt @@ -0,0 +1,20 @@ +package g2501_2600.s2600_k_items_with_the_maximum_sum + +// #Easy #Math #Greedy #2023_07_13_Time_145_ms_(100.00%)_Space_33.5_MB_(75.00%) + +@Suppress("UNUSED_PARAMETER") +class Solution { + fun kItemsWithMaximumSum(numOnes: Int, numZeros: Int, numNegOnes: Int, k: Int): Int { + if (k <= numOnes) { + return k + } + + if (k <= numOnes + numZeros) { + return numOnes + } + + val remainingSum = k - (numOnes + numZeros) + + return numOnes - remainingSum + } +} diff --git a/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/readme.md b/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/readme.md new file mode 100644 index 000000000..c791a9fd9 --- /dev/null +++ b/src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/readme.md @@ -0,0 +1,36 @@ +2600\. K Items With the Maximum Sum + +Easy + +There is a bag that consists of items, each item has a number `1`, `0`, or `-1` written on it. + +You are given four **non-negative** integers `numOnes`, `numZeros`, `numNegOnes`, and `k`. + +The bag initially contains: + +* `numOnes` items with `1`s written on them. +* `numZeroes` items with `0`s written on them. +* `numNegOnes` items with `-1`s written on them. + +We want to pick exactly `k` items among the available items. Return _the **maximum** possible sum of numbers written on the items_. + +**Example 1:** + +**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2 + +**Output:** 2 + +**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2. It can be proven that 2 is the maximum possible sum. + +**Example 2:** + +**Input:** numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4 + +**Output:** 3 + +**Explanation:** We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3. It can be proven that 3 is the maximum possible sum. + +**Constraints:** + +* `0 <= numOnes, numZeros, numNegOnes <= 50` +* `0 <= k <= numOnes + numZeros + numNegOnes` \ No newline at end of file diff --git a/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/Solution.kt b/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/Solution.kt new file mode 100644 index 000000000..122e47205 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/Solution.kt @@ -0,0 +1,44 @@ +package g2601_2700.s2601_prime_subtraction_operation + +// #Medium #Array #Math #Greedy #Binary_Search #Number_Theory +// #2023_07_13_Time_233_ms_(100.00%)_Space_38.4_MB_(100.00%) + +class Solution { + private fun primesUntil(n: Int): IntArray { + if (n < 2) return intArrayOf() + val primes = IntArray(200) + val composite = BooleanArray(n + 1) + primes[0] = 2 + var added = 1 + var i = 3 + while (i <= n) { + if (composite[i]) { + i += 2 + continue + } + primes[added++] = i + var j = i * i + while (j <= n) { + composite[j] = true + j += i + } + i += 2 + } + return primes.copyOf(added) + } + + fun primeSubOperation(nums: IntArray): Boolean { + var max = 0 + for (n in nums) { + max = max.coerceAtLeast(n) + } + val primes = primesUntil(max) + var prev = 0 + for (n in nums) { + val pos = primes.binarySearch(n - prev - 1) + if (pos == -1 && n <= prev) return false + prev = n - if (pos == -1) 0 else if (pos < 0) primes[-pos - 2] else primes[pos] + } + return true + } +} diff --git a/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/readme.md b/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/readme.md new file mode 100644 index 000000000..23dc5ce40 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/readme.md @@ -0,0 +1,43 @@ +2601\. Prime Subtraction Operation + +Medium + +You are given a **0-indexed** integer array `nums` of length `n`. + +You can perform the following operation as many times as you want: + +* Pick an index `i` that you haven’t picked before, and pick a prime `p` **strictly less than** `nums[i]`, then subtract `p` from `nums[i]`. + +Return _true if you can make `nums` a strictly increasing array using the above operation and false otherwise._ + +A **strictly increasing array** is an array whose each element is strictly greater than its preceding element. + +**Example 1:** + +**Input:** nums = [4,9,6,10] + +**Output:** true + +**Explanation:** In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10]. In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10]. After the second operation, nums is sorted in strictly increasing order, so the answer is true. + +**Example 2:** + +**Input:** nums = [6,8,11,12] + +**Output:** true + +**Explanation:** Initially nums is sorted in strictly increasing order, so we don't need to make any operations. + +**Example 3:** + +**Input:** nums = [5,8,3] + +**Output:** false + +**Explanation:** It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* `1 <= nums[i] <= 1000` +* `nums.length == n` \ No newline at end of file diff --git a/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/Solution.kt b/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/Solution.kt new file mode 100644 index 000000000..68881d44b --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/Solution.kt @@ -0,0 +1,41 @@ +package g2601_2700.s2602_minimum_operations_to_make_all_array_elements_equal + +// #Medium #Array #Sorting #Binary_Search #Prefix_Sum +// #2023_07_13_Time_790_ms_(100.00%)_Space_63.8_MB_(100.00%) + +class Solution { + fun minOperations(nums: IntArray, queries: IntArray): List { + nums.sort() + val sum = LongArray(nums.size) + sum[0] = nums[0].toLong() + for (i in 1 until nums.size) { + sum[i] = sum[i - 1] + nums[i].toLong() + } + val res: MutableList = ArrayList() + for (query in queries) { + res.add(getOperations(sum, nums, query)) + } + return res + } + + private fun getOperations(sum: LongArray, nums: IntArray, target: Int): Long { + var res: Long = 0 + val index = getIndex(nums, target) + val rightCounts = nums.size - 1 - index + if (index > 0) { + res += index.toLong() * target - sum[index - 1] + } + if (rightCounts > 0) { + res += sum[nums.size - 1] - sum[index] - rightCounts.toLong() * target + } + res += kotlin.math.abs(target - nums[index]).toLong() + return res + } + + private fun getIndex(nums: IntArray, target: Int): Int { + var index = nums.binarySearch(target) + if (index < 0) index = -(index + 1) + if (index == nums.size) --index + return index + } +} diff --git a/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/readme.md b/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/readme.md new file mode 100644 index 000000000..515f388ec --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/readme.md @@ -0,0 +1,49 @@ +2602\. Minimum Operations to Make All Array Elements Equal + +Medium + +You are given an array `nums` consisting of positive integers. + +You are also given an integer array `queries` of size `m`. For the ith query, you want to make all of the elements of `nums` equal to `queries[i]`. You can perform the following operation on the array **any** number of times: + +* **Increase** or **decrease** an element of the array by `1`. + +Return _an array_ `answer` _of size_ `m` _where_ `answer[i]` _is the **minimum** number of operations to make all elements of_ `nums` _equal to_ `queries[i]`. + +**Note** that after each query the array is reset to its original state. + +**Example 1:** + +**Input:** nums = [3,1,6,8], queries = [1,5] + +**Output:** [14,10] + +**Explanation:** For the first query we can do the following operations: +- Decrease nums[0] 2 times, so that nums = [1,1,6,8]. +- Decrease nums[2] 5 times, so that nums = [1,1,1,8]. +- Decrease nums[3] 7 times, so that nums = [1,1,1,1]. + +So the total number of operations for the first query is 2 + 5 + 7 = 14. + +For the second query we can do the following operations: +- Increase nums[0] 2 times, so that nums = [5,1,6,8]. +- Increase nums[1] 4 times, so that nums = [5,5,6,8]. +- Decrease nums[2] 1 time, so that nums = [5,5,5,8]. +- Decrease nums[3] 3 times, so that nums = [5,5,5,5]. + +So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10. + +**Example 2:** + +**Input:** nums = [2,9,6,3], queries = [10] + +**Output:** [20] + +**Explanation:** We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20. + +**Constraints:** + +* `n == nums.length` +* `m == queries.length` +* 1 <= n, m <= 105 +* 1 <= nums[i], queries[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/Solution.kt b/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/Solution.kt new file mode 100644 index 000000000..1652d2314 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/Solution.kt @@ -0,0 +1,50 @@ +package g2601_2700.s2603_collect_coins_in_a_tree + +// #Hard #Array #Tree #Graph #Topological_Sort +// #2023_07_13_Time_986_ms_(100.00%)_Space_67.7_MB_(100.00%) + +class Solution { + private lateinit var coins: IntArray + private var n = 0 + private lateinit var graph: Array?> + private var sum = 0 + private var ret = 0 + fun collectTheCoins(coins: IntArray, edges: Array): Int { + n = coins.size + this.coins = coins + graph = arrayOfNulls(n) + for (i in 0 until n) { + graph[i] = ArrayList() + } + for (edge in edges) { + graph[edge[0]]!!.add(edge[1]) + graph[edge[1]]!!.add(edge[0]) + } + for (coin in coins) { + sum += coin + } + dfs(0, -1) + return (2 * (ret - 1)).coerceAtLeast(0) + } + + private fun dfs(node: Int, pre: Int): Int { + var cnt = 0 + var s = 0 + for (nn in graph[node]!!) { + if (nn != pre) { + val r = dfs(nn, node) + if (r - coins[nn] > 0) cnt++ + s += r + } + } + + if (pre != -1 && sum - s - coins[node] - coins[pre] > 0) { + cnt++ + } + + if (cnt >= 2) { + ret++ + } + return s + coins[node] + } +} diff --git a/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/readme.md b/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/readme.md new file mode 100644 index 000000000..e43338609 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/readme.md @@ -0,0 +1,45 @@ +2603\. Collect Coins in a Tree + +Hard + +There exists an undirected and unrooted tree with `n` nodes indexed from `0` to `n - 1`. You are given an integer `n` and a 2D integer array edges of length `n - 1`, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given an array `coins` of size `n` where `coins[i]` can be either `0` or `1`, where `1` indicates the presence of a coin in the vertex `i`. + +Initially, you choose to start at any vertex in the tree. Then, you can perform the following operations any number of times: + +* Collect all the coins that are at a distance of at most `2` from the current vertex, or +* Move to any adjacent vertex in the tree. + +Find _the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex_. + +Note that if you pass an edge several times, you need to count it into the answer several times. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2023/03/01/graph-2.png) + +**Input:** coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]] + +**Output:** 2 + +**Explanation:** Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2. + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2023/03/02/graph-4.png) + +**Input:** coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]] + +**Output:** 2 + +**Explanation:** Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0. + +**Constraints:** + +* `n == coins.length` +* 1 <= n <= 3 * 104 +* `0 <= coins[i] <= 1` +* `edges.length == n - 1` +* `edges[i].length == 2` +* 0 <= ai, bi < n +* ai != bi +* `edges` represents a valid tree. \ No newline at end of file diff --git a/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/Solution.kt b/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/Solution.kt new file mode 100644 index 000000000..5f0e240f8 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/Solution.kt @@ -0,0 +1,20 @@ +package g2601_2700.s2605_form_smallest_number_from_two_digit_arrays + +// #Easy #Array #Hash_Table #Enumeration #2023_07_13_Time_161_ms_(100.00%)_Space_34.7_MB_(100.00%) + +class Solution { + fun minNumber(nums1: IntArray, nums2: IntArray): Int { + val set = HashSet() + var (min, min1, min2) = arrayOf(10, 10, 10) + for (num in nums1) { + min1 = minOf(min1, num) + set.add(num) + } + for (num in nums2) { + min2 = minOf(min2, num) + if (set.contains(num)) min = minOf(min, num) + } + if (min != 10) return min + return minOf(min1, min2) * 10 + maxOf(min1, min2) + } +} diff --git a/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/readme.md b/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/readme.md new file mode 100644 index 000000000..7243c63b9 --- /dev/null +++ b/src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/readme.md @@ -0,0 +1,27 @@ +2605\. Form Smallest Number From Two Digit Arrays + +Easy + +Given two arrays of **unique** digits `nums1` and `nums2`, return _the **smallest** number that contains **at least** one digit from each array_. + +**Example 1:** + +**Input:** nums1 = [4,1,3], nums2 = [5,7] + +**Output:** 15 + +**Explanation:** The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have. + +**Example 2:** + +**Input:** nums1 = [3,5,2,6], nums2 = [3,1,7] + +**Output:** 3 + +**Explanation:** The number 3 contains the digit 3 which exists in both arrays. + +**Constraints:** + +* `1 <= nums1.length, nums2.length <= 9` +* `1 <= nums1[i], nums2[i] <= 9` +* All digits in each array are **unique**. \ No newline at end of file diff --git a/src/test/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/SolutionTest.kt b/src/test/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/SolutionTest.kt new file mode 100644 index 000000000..8b67dd3d1 --- /dev/null +++ b/src/test/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/SolutionTest.kt @@ -0,0 +1,17 @@ +package g2501_2600.s2600_k_items_with_the_maximum_sum + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun kItemsWithMaximumSum() { + assertThat(Solution().kItemsWithMaximumSum(3, 2, 0, 2), equalTo(2)) + } + + @Test + fun kItemsWithMaximumSum2() { + assertThat(Solution().kItemsWithMaximumSum(3, 2, 0, 4), equalTo(3)) + } +} diff --git a/src/test/kotlin/g2601_2700/s2601_prime_subtraction_operation/SolutionTest.kt b/src/test/kotlin/g2601_2700/s2601_prime_subtraction_operation/SolutionTest.kt new file mode 100644 index 000000000..05a477f0b --- /dev/null +++ b/src/test/kotlin/g2601_2700/s2601_prime_subtraction_operation/SolutionTest.kt @@ -0,0 +1,22 @@ +package g2601_2700.s2601_prime_subtraction_operation + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun primeSubOperation() { + assertThat(Solution().primeSubOperation(intArrayOf(4, 9, 6, 10)), equalTo(true)) + } + + @Test + fun primeSubOperation2() { + assertThat(Solution().primeSubOperation(intArrayOf(6, 8, 11, 12)), equalTo(true)) + } + + @Test + fun primeSubOperation3() { + assertThat(Solution().primeSubOperation(intArrayOf(5, 8, 3)), equalTo(false)) + } +} diff --git a/src/test/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/SolutionTest.kt b/src/test/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/SolutionTest.kt new file mode 100644 index 000000000..b426a9ed8 --- /dev/null +++ b/src/test/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/SolutionTest.kt @@ -0,0 +1,17 @@ +package g2601_2700.s2602_minimum_operations_to_make_all_array_elements_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(intArrayOf(3, 1, 6, 8), intArrayOf(1, 5)), equalTo(listOf(14L, 10L))) + } + + @Test + fun minOperations2() { + assertThat(Solution().minOperations(intArrayOf(2, 9, 6, 3), intArrayOf(10)), equalTo(listOf(20L))) + } +} diff --git a/src/test/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/SolutionTest.kt b/src/test/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/SolutionTest.kt new file mode 100644 index 000000000..03708378b --- /dev/null +++ b/src/test/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/SolutionTest.kt @@ -0,0 +1,37 @@ +package g2601_2700.s2603_collect_coins_in_a_tree + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun collectTheCoins() { + assertThat( + Solution().collectTheCoins( + intArrayOf(1, 0, 0, 0, 0, 1), + arrayOf( + intArrayOf(0, 1), + intArrayOf(1, 2), intArrayOf(2, 3), + intArrayOf(3, 4), intArrayOf(4, 5) + ) + ), + equalTo(2) + ) + } + + @Test + fun collectTheCoins2() { + assertThat( + Solution().collectTheCoins( + intArrayOf(0, 0, 0, 1, 1, 0, 0, 1), + arrayOf( + intArrayOf(0, 1), + intArrayOf(0, 2), intArrayOf(1, 3), intArrayOf(1, 4), intArrayOf(2, 5), + intArrayOf(5, 6), intArrayOf(5, 7) + ) + ), + equalTo(2) + ) + } +} diff --git a/src/test/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/SolutionTest.kt b/src/test/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/SolutionTest.kt new file mode 100644 index 000000000..bbdc0e12e --- /dev/null +++ b/src/test/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/SolutionTest.kt @@ -0,0 +1,17 @@ +package g2601_2700.s2605_form_smallest_number_from_two_digit_arrays + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minNumber() { + assertThat(Solution().minNumber(intArrayOf(4, 1, 3), intArrayOf(5, 7)), equalTo(15)) + } + + @Test + fun minNumber2() { + assertThat(Solution().minNumber(intArrayOf(3, 5, 2, 6), intArrayOf(3, 1, 7)), equalTo(3)) + } +}