Skip to content

Commit 8bde88b

Browse files
committed
Added tasks 3375-3378
1 parent 5409436 commit 8bde88b

File tree

12 files changed

+495
-0
lines changed

12 files changed

+495
-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.s3375_minimum_operations_to_make_array_values_equal_to_k
2+
3+
// #Easy #2024_12_08_Time_191_ms_(100.00%)_Space_39.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun minOperations(nums: IntArray, k: Int): Int {
7+
val s: MutableSet<Int?> = HashSet<Int?>()
8+
for (i in nums) {
9+
s.add(i)
10+
}
11+
var res = 0
12+
for (i in s) {
13+
if (i!! > k) {
14+
res++
15+
} else if (i < k) {
16+
return -1
17+
}
18+
}
19+
return res
20+
}
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3375\. Minimum Operations to Make Array Values Equal to K
2+
3+
Easy
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
An integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are _identical_.
8+
9+
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.
10+
11+
You are allowed to perform the following operation on `nums`:
12+
13+
* Select an integer `h` that is _valid_ for the **current** values in `nums`.
14+
* For each index `i` where `nums[i] > h`, set `nums[i]` to `h`.
15+
16+
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.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [5,2,5,4,5], k = 2
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
The operations can be performed in order using valid integers 4 and then 2.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,1,2], k = 2
31+
32+
**Output:** \-1
33+
34+
**Explanation:**
35+
36+
It is impossible to make all the values equal to 2.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [9,7,5,3], k = 1
41+
42+
**Output:** 4
43+
44+
**Explanation:**
45+
46+
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
47+
48+
**Constraints:**
49+
50+
* `1 <= nums.length <= 100`
51+
* `1 <= nums[i] <= 100`
52+
* `1 <= k <= 100`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3301_3400.s3376_minimum_time_to_break_locks_i
2+
3+
// #Medium #2024_12_08_Time_202_ms_(100.00%)_Space_40_MB_(100.00%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun findMinimumTime(strength: List<Int>, k: Int): Int {
9+
val perm: MutableList<Int> = ArrayList<Int>(strength)
10+
perm.sort()
11+
var minTime = Int.Companion.MAX_VALUE
12+
do {
13+
var time = 0
14+
var factor = 1
15+
for (required in perm) {
16+
val neededTime = (required + factor - 1) / factor
17+
time += neededTime
18+
factor += k
19+
}
20+
minTime = min(minTime, time)
21+
} while (nextPermutation(perm))
22+
return minTime
23+
}
24+
25+
private fun nextPermutation(nums: MutableList<Int>): Boolean {
26+
var i = nums.size - 2
27+
while (i >= 0 && nums[i] >= nums[i + 1]) {
28+
i--
29+
}
30+
if (i < 0) {
31+
return false
32+
}
33+
var j = nums.size - 1
34+
while (nums[j] <= nums[i]) {
35+
j--
36+
}
37+
nums[i] = nums.set(j, nums[i])
38+
nums.subList(i + 1, nums.size).reverse()
39+
return true
40+
}
41+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
3376\. Minimum Time to Break Locks I
2+
3+
Medium
4+
5+
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.
6+
7+
To break a lock, Bob uses a sword with the following characteristics:
8+
9+
* The initial energy of the sword is 0.
10+
* The initial factor `X` by which the energy of the sword increases is 1.
11+
* Every minute, the energy of the sword increases by the current factor `X`.
12+
* To break the <code>i<sup>th</sup></code> lock, the energy of the sword must reach **at least** `strength[i]`.
13+
* After breaking a lock, the energy of the sword resets to 0, and the factor `X` increases by a given value `K`.
14+
15+
Your task is to determine the **minimum** time in minutes required for Bob to break all `n` locks and escape the dungeon.
16+
17+
Return the **minimum** time required for Bob to break all `n` locks.
18+
19+
**Example 1:**
20+
21+
**Input:** strength = [3,4,1], K = 1
22+
23+
**Output:** 4
24+
25+
**Explanation:**
26+
27+
| Time | Energy | X | Action | Updated X |
28+
|------|--------|---|----------------------|-----------|
29+
| 0 | 0 | 1 | Nothing | 1 |
30+
| 1 | 1 | 1 | Break 3rd Lock | 2 |
31+
| 2 | 2 | 2 | Nothing | 2 |
32+
| 3 | 4 | 2 | Break 2nd Lock | 3 |
33+
| 4 | 3 | 3 | Break 1st Lock | 3 |
34+
35+
The locks cannot be broken in less than 4 minutes; thus, the answer is 4.
36+
37+
**Example 2:**
38+
39+
**Input:** strength = [2,5,4], K = 2
40+
41+
**Output:** 5
42+
43+
**Explanation:**
44+
45+
| Time | Energy | X | Action | Updated X |
46+
|------|--------|---|----------------------|-----------|
47+
| 0 | 0 | 1 | Nothing | 1 |
48+
| 1 | 1 | 1 | Nothing | 1 |
49+
| 2 | 2 | 1 | Break 1st Lock | 3 |
50+
| 3 | 3 | 3 | Nothing | 3 |
51+
| 4 | 6 | 3 | Break 2nd Lock | 5 |
52+
| 5 | 5 | 5 | Break 3rd Lock | 7 |
53+
54+
The locks cannot be broken in less than 5 minutes; thus, the answer is 5.
55+
56+
**Constraints:**
57+
58+
* `n == strength.length`
59+
* `1 <= n <= 8`
60+
* `1 <= K <= 10`
61+
* <code>1 <= strength[i] <= 10<sup>6</sup></code>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3301_3400.s3377_digit_operations_to_make_two_integers_equal
2+
3+
// #Medium #2024_12_08_Time_215_ms_(100.00%)_Space_40.7_MB_(100.00%)
4+
5+
import java.util.PriorityQueue
6+
7+
class Solution {
8+
fun minOperations(n: Int, m: Int): Int {
9+
val limit = 100000
10+
val sieve = BooleanArray(limit + 1)
11+
val visited = BooleanArray(limit)
12+
sieve.fill(true)
13+
sieve[0] = false
14+
sieve[1] = false
15+
var i = 2
16+
while (i * i <= limit) {
17+
if (sieve[i]) {
18+
var j = i * i
19+
while (j <= limit) {
20+
sieve[j] = false
21+
j += i
22+
}
23+
}
24+
i++
25+
}
26+
if (sieve[n]) {
27+
return -1
28+
}
29+
val pq = PriorityQueue<IntArray>(Comparator { a: IntArray, b: IntArray -> a[0] - b[0] })
30+
visited[n] = true
31+
pq.add(intArrayOf(n, n))
32+
while (!pq.isEmpty()) {
33+
val current = pq.poll()
34+
val cost = current[0]
35+
val num = current[1]
36+
val temp = num.toString().toCharArray()
37+
if (num == m) {
38+
return cost
39+
}
40+
for (j in temp.indices) {
41+
val old = temp[j]
42+
for (i in -1..1) {
43+
val digit = old.code - '0'.code
44+
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) {
45+
continue
46+
}
47+
temp[j] = (i + digit + '0'.code).toChar()
48+
val newNum = String(temp).toInt()
49+
if (!sieve[newNum] && !visited[newNum]) {
50+
visited[newNum] = true
51+
pq.add(intArrayOf(cost + newNum, newNum))
52+
}
53+
}
54+
temp[j] = old
55+
}
56+
}
57+
return -1
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3377\. Digit Operations to Make Two Integers Equal
2+
3+
Medium
4+
5+
You are given two integers `n` and `m` that consist of the **same** number of digits.
6+
7+
You can perform the following operations **any** number of times:
8+
9+
* Choose **any** digit from `n` that is not 9 and **increase** it by 1.
10+
* Choose **any** digit from `n` that is not 0 and **decrease** it by 1.
11+
12+
The integer `n` must not be a **prime** number at any point, including its original value and after each operation.
13+
14+
The cost of a transformation is the sum of **all** values that `n` takes throughout the operations performed.
15+
16+
Return the **minimum** cost to transform `n` into `m`. If it is impossible, return -1.
17+
18+
A prime number is a natural number greater than 1 with only two factors, 1 and itself.
19+
20+
**Example 1:**
21+
22+
**Input:** n = 10, m = 12
23+
24+
**Output:** 85
25+
26+
**Explanation:**
27+
28+
We perform the following operations:
29+
30+
* Increase the first digit, now <code>n = <ins>**2**</ins>0</code>.
31+
* Increase the second digit, now <code>n = 2**<ins>1</ins>**</code>.
32+
* Increase the second digit, now <code>n = 2**<ins>2</ins>**</code>.
33+
* Decrease the first digit, now <code>n = **<ins>1</ins>**2</code>.
34+
35+
**Example 2:**
36+
37+
**Input:** n = 4, m = 8
38+
39+
**Output:** \-1
40+
41+
**Explanation:**
42+
43+
It is impossible to make `n` equal to `m`.
44+
45+
**Example 3:**
46+
47+
**Input:** n = 6, m = 2
48+
49+
**Output:** \-1
50+
51+
**Explanation:**
52+
53+
Since 2 is already a prime, we can't make `n` equal to `m`.
54+
55+
**Constraints:**
56+
57+
* <code>1 <= n, m < 10<sup>4</sup></code>
58+
* `n` and `m` consist of the same number of digits.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g3301_3400.s3378_count_connected_components_in_lcm_graph
2+
3+
// #Hard #2024_12_08_Time_58_ms_(100.00%)_Space_54.4_MB_(100.00%)
4+
5+
class Solution {
6+
private class UnionFind(n: Int) {
7+
var parent = IntArray(n) { it }
8+
var rank = IntArray(n)
9+
var totalComponents = n
10+
11+
fun find(u: Int): Int {
12+
if (parent[u] == u) {
13+
return u
14+
}
15+
parent[u] = find(parent[u])
16+
return parent[u]
17+
}
18+
19+
fun union(u: Int, v: Int) {
20+
val parentU = find(u)
21+
val parentV = find(v)
22+
if (parentU != parentV) {
23+
totalComponents--
24+
when {
25+
rank[parentU] == rank[parentV] -> {
26+
parent[parentV] = parentU
27+
rank[parentU]++
28+
}
29+
rank[parentU] > rank[parentV] -> parent[parentV] = parentU
30+
else -> parent[parentU] = parentV
31+
}
32+
}
33+
}
34+
}
35+
36+
fun countComponents(nums: IntArray, threshold: Int): Int {
37+
val goodNums = nums.filter { it <= threshold }
38+
val totalNums = nums.size
39+
if (goodNums.isEmpty()) {
40+
return totalNums
41+
}
42+
val uf = UnionFind(goodNums.size)
43+
val presentElements = IntArray(threshold + 1) { -1 }
44+
goodNums.forEachIndexed { index, num ->
45+
presentElements[num] = index
46+
}
47+
for (d in goodNums) {
48+
for (i in d..threshold step d) {
49+
if (presentElements[i] == -1) {
50+
presentElements[i] = presentElements[d]
51+
} else if (presentElements[i] != presentElements[d]) {
52+
uf.union(presentElements[i], presentElements[d])
53+
}
54+
}
55+
}
56+
return uf.totalComponents + totalNums - goodNums.size
57+
}
58+
}

0 commit comments

Comments
 (0)