Skip to content

Commit e22d771

Browse files
authored
Added tasks 3375-3382
1 parent fbfddee commit e22d771

File tree

24 files changed

+995
-0
lines changed

24 files changed

+995
-0
lines changed
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 #Array #Hash_Table #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+
}
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`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3301_3400.s3376_minimum_time_to_break_locks_i
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2024_12_08_Time_202_ms_(100.00%)_Space_40_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun findMinimumTime(strength: List<Int>, k: Int): Int {
10+
val perm: MutableList<Int> = ArrayList<Int>(strength)
11+
perm.sort()
12+
var minTime = Int.Companion.MAX_VALUE
13+
do {
14+
var time = 0
15+
var factor = 1
16+
for (required in perm) {
17+
val neededTime = (required + factor - 1) / factor
18+
time += neededTime
19+
factor += k
20+
}
21+
minTime = min(minTime, time)
22+
} while (nextPermutation(perm))
23+
return minTime
24+
}
25+
26+
private fun nextPermutation(nums: MutableList<Int>): Boolean {
27+
var i = nums.size - 2
28+
while (i >= 0 && nums[i] >= nums[i + 1]) {
29+
i--
30+
}
31+
if (i < 0) {
32+
return false
33+
}
34+
var j = nums.size - 1
35+
while (nums[j] <= nums[i]) {
36+
j--
37+
}
38+
val temp = nums[i]
39+
nums[i] = nums[j]
40+
nums[j] = temp
41+
nums.subList(i + 1, nums.size).reverse()
42+
return true
43+
}
44+
}
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g3301_3400.s3377_digit_operations_to_make_two_integers_equal
2+
3+
// #Medium #Math #Heap_Priority_Queue #Graph #Shortest_Path #Number_Theory
4+
// #2024_12_08_Time_215_ms_(100.00%)_Space_40.7_MB_(100.00%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun minOperations(n: Int, m: Int): Int {
10+
val limit = 100000
11+
val sieve = BooleanArray(limit + 1)
12+
val visited = BooleanArray(limit)
13+
sieve.fill(true)
14+
sieve[0] = false
15+
sieve[1] = false
16+
var i = 2
17+
while (i * i <= limit) {
18+
if (sieve[i]) {
19+
var j = i * i
20+
while (j <= limit) {
21+
sieve[j] = false
22+
j += i
23+
}
24+
}
25+
i++
26+
}
27+
if (sieve[n]) {
28+
return -1
29+
}
30+
val pq = PriorityQueue<IntArray>(Comparator { a: IntArray, b: IntArray -> a[0] - b[0] })
31+
visited[n] = true
32+
pq.add(intArrayOf(n, n))
33+
while (pq.isNotEmpty()) {
34+
val current = pq.poll()
35+
val cost = current[0]
36+
val num = current[1]
37+
val temp = num.toString().toCharArray()
38+
if (num == m) {
39+
return cost
40+
}
41+
for (j in temp.indices) {
42+
val old = temp[j]
43+
for (i in -1..1) {
44+
val digit = old.code - '0'.code
45+
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) {
46+
continue
47+
}
48+
temp[j] = (i + digit + '0'.code).toChar()
49+
val newNum = String(temp).toInt()
50+
if (!sieve[newNum] && !visited[newNum]) {
51+
visited[newNum] = true
52+
pq.add(intArrayOf(cost + newNum, newNum))
53+
}
54+
}
55+
temp[j] = old
56+
}
57+
}
58+
return -1
59+
}
60+
}
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package g3301_3400.s3378_count_connected_components_in_lcm_graph
2+
3+
// #Hard #Array #Hash_Table #Math #Union_Find #Number_Theory
4+
// #2024_12_08_Time_58_ms_(100.00%)_Space_54.4_MB_(100.00%)
5+
6+
class Solution {
7+
private class UnionFind(n: Int) {
8+
var parent = IntArray(n) { it }
9+
var rank = IntArray(n)
10+
var totalComponents = n
11+
12+
fun find(u: Int): Int {
13+
if (parent[u] == u) {
14+
return u
15+
}
16+
parent[u] = find(parent[u])
17+
return parent[u]
18+
}
19+
20+
fun union(u: Int, v: Int) {
21+
val parentU = find(u)
22+
val parentV = find(v)
23+
if (parentU != parentV) {
24+
totalComponents--
25+
when {
26+
rank[parentU] == rank[parentV] -> {
27+
parent[parentV] = parentU
28+
rank[parentU]++
29+
}
30+
rank[parentU] > rank[parentV] -> parent[parentV] = parentU
31+
else -> parent[parentU] = parentV
32+
}
33+
}
34+
}
35+
}
36+
37+
fun countComponents(nums: IntArray, threshold: Int): Int {
38+
val goodNums = nums.filter { it <= threshold }
39+
val totalNums = nums.size
40+
if (goodNums.isEmpty()) {
41+
return totalNums
42+
}
43+
val uf = UnionFind(goodNums.size)
44+
val presentElements = IntArray(threshold + 1) { -1 }
45+
goodNums.forEachIndexed { index, num ->
46+
presentElements[num] = index
47+
}
48+
for (d in goodNums) {
49+
for (i in d..threshold step d) {
50+
if (presentElements[i] == -1) {
51+
presentElements[i] = presentElements[d]
52+
} else if (presentElements[i] != presentElements[d]) {
53+
uf.union(presentElements[i], presentElements[d])
54+
}
55+
}
56+
}
57+
return uf.totalComponents + totalNums - goodNums.size
58+
}
59+
}

0 commit comments

Comments
 (0)