forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
485 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/g2501_2600/s2600_k_items_with_the_maximum_sum/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/g2601_2700/s2601_prime_subtraction_operation/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
41 changes: 41 additions & 0 deletions
41
...n/kotlin/g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Long> { | ||
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<Long> = 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 | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
.../g2601_2700/s2602_minimum_operations_to_make_all_array_elements_equal/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>i<sup>th</sup></code> 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` | ||
* <code>1 <= n, m <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code> |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ArrayList<Int>?> | ||
private var sum = 0 | ||
private var ret = 0 | ||
fun collectTheCoins(coins: IntArray, edges: Array<IntArray>): 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] | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/g2601_2700/s2603_collect_coins_in_a_tree/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> 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` | ||
* <code>1 <= n <= 3 * 10<sup>4</sup></code> | ||
* `0 <= coins[i] <= 1` | ||
* `edges.length == n - 1` | ||
* `edges[i].length == 2` | ||
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code> | ||
* <code>a<sub>i</sub> != b<sub>i</sub></code> | ||
* `edges` represents a valid tree. |
20 changes: 20 additions & 0 deletions
20
src/main/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Int>() | ||
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) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...in/kotlin/g2601_2700/s2605_form_smallest_number_from_two_digit_arrays/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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**. |
Oops, something went wrong.