Skip to content

Added tasks 3375-3382 #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Int?> = HashSet<Int?>()
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
}
}
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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<Int>, k: Int): Int {
val perm: MutableList<Int> = ArrayList<Int>(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<Int>): 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
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>i<sup>th</sup></code> 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 <code>i<sup>th</sup></code> 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`
* <code>1 <= strength[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -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<IntArray>(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
}
}
Original file line number Diff line number Diff line change
@@ -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 <code>n = <ins>**2**</ins>0</code>.
* Increase the second digit, now <code>n = 2**<ins>1</ins>**</code>.
* Increase the second digit, now <code>n = 2**<ins>2</ins>**</code>.
* Decrease the first digit, now <code>n = **<ins>1</ins>**2</code>.

**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:**

* <code>1 <= n, m < 10<sup>4</sup></code>
* `n` and `m` consist of the same number of digits.
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading
Loading