Skip to content

Commit a27f73d

Browse files
committed
Added tasks 3375-3378
1 parent 22455a9 commit a27f73d

File tree

12 files changed

+498
-0
lines changed

12 files changed

+498
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k;
2+
3+
// #Easy #2024_12_07_Time_3_ms_(100.00%)_Space_44.5_MB_(100.00%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int minOperations(int[] nums, int k) {
10+
Set<Integer> s = new HashSet<>();
11+
for (int i : nums) {
12+
s.add(i);
13+
}
14+
int res = 0;
15+
for (int i : s) {
16+
if (i > k) {
17+
res++;
18+
} else if (i < k) {
19+
return -1;
20+
}
21+
}
22+
return res;
23+
}
24+
}
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3301_3400.s3376_minimum_time_to_break_locks_i;
2+
3+
// #Medium #2024_12_07_Time_760_ms_(100.00%)_Space_45_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int findMinimumTime(List<Integer> strength, int k) {
11+
List<Integer> perm = new ArrayList<>(strength);
12+
Collections.sort(perm);
13+
int minTime = Integer.MAX_VALUE;
14+
do {
15+
int time = 0;
16+
int factor = 1;
17+
for (int required : perm) {
18+
int neededTime = (required + factor - 1) / factor;
19+
time += neededTime;
20+
factor += k;
21+
}
22+
minTime = Math.min(minTime, time);
23+
} while (nextPermutation(perm));
24+
return minTime;
25+
}
26+
27+
private boolean nextPermutation(List<Integer> nums) {
28+
int i = nums.size() - 2;
29+
while (i >= 0 && nums.get(i) >= nums.get(i + 1)) {
30+
i--;
31+
}
32+
if (i < 0) {
33+
return false;
34+
}
35+
int j = nums.size() - 1;
36+
while (nums.get(j) <= nums.get(i)) {
37+
j--;
38+
}
39+
Collections.swap(nums, i, j);
40+
Collections.reverse(nums.subList(i + 1, nums.size()));
41+
return true;
42+
}
43+
}
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package g3301_3400.s3377_digit_operations_to_make_two_integers_equal;
2+
3+
// #Medium #2024_12_07_Time_255_ms_(100.00%)_Space_44.9_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
public int minOperations(int n, int m) {
10+
int limit = 100000;
11+
boolean[] sieve = new boolean[limit + 1];
12+
boolean[] visited = new boolean[limit];
13+
Arrays.fill(sieve, true);
14+
sieve[0] = false;
15+
sieve[1] = false;
16+
for (int i = 2; i * i <= limit; i++) {
17+
if (sieve[i]) {
18+
for (int j = i * i; j <= limit; j += i) {
19+
sieve[j] = false;
20+
}
21+
}
22+
}
23+
if (sieve[n]) {
24+
return -1;
25+
}
26+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
27+
visited[n] = true;
28+
pq.add(new int[] {n, n});
29+
while (!pq.isEmpty()) {
30+
int[] current = pq.poll();
31+
int cost = current[0];
32+
int num = current[1];
33+
char[] temp = Integer.toString(num).toCharArray();
34+
if (num == m) {
35+
return cost;
36+
}
37+
for (int j = 0; j < temp.length; j++) {
38+
char old = temp[j];
39+
for (int i = -1; i <= 1; i++) {
40+
int digit = old - '0';
41+
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) {
42+
continue;
43+
}
44+
temp[j] = (char) (i + digit + '0');
45+
int newnum = Integer.parseInt(new String(temp));
46+
if (!sieve[newnum] && !visited[newnum]) {
47+
visited[newnum] = true;
48+
pq.add(new int[] {cost + newnum, newnum});
49+
}
50+
}
51+
temp[j] = old;
52+
}
53+
}
54+
return -1;
55+
}
56+
}
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: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package g3301_3400.s3378_count_connected_components_in_lcm_graph;
2+
3+
// #Hard #2024_12_07_Time_48_ms_(100.00%)_Space_59.5_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private static class Unionfind {
11+
int[] parent;
12+
int[] rank;
13+
int totalComponents;
14+
15+
public Unionfind(int n) {
16+
parent = new int[n];
17+
rank = new int[n];
18+
totalComponents = n;
19+
for (int i = 0; i < n; i++) {
20+
parent[i] = i;
21+
}
22+
}
23+
24+
public int find(int u) {
25+
if (parent[u] == u) {
26+
return u;
27+
}
28+
return parent[u] = find(parent[u]);
29+
}
30+
31+
public void union(int u, int v) {
32+
int parentU = find(u);
33+
int parentV = find(v);
34+
35+
if (parentU != parentV) {
36+
totalComponents--;
37+
if (rank[parentU] == rank[parentV]) {
38+
parent[parentV] = parentU;
39+
rank[parentU]++;
40+
} else if (rank[parentU] > rank[parentV]) {
41+
parent[parentV] = parentU;
42+
} else {
43+
parent[parentU] = parentV;
44+
}
45+
}
46+
}
47+
}
48+
49+
public int countComponents(int[] nums, int threshold) {
50+
List<Integer> goodNums = new ArrayList<>();
51+
int totalNums = nums.length;
52+
for (int num : nums) {
53+
if (num <= threshold) {
54+
goodNums.add(num);
55+
}
56+
}
57+
if (goodNums.isEmpty()) {
58+
return totalNums;
59+
}
60+
Unionfind uf = new Unionfind(goodNums.size());
61+
int[] presentElements = new int[threshold + 1];
62+
Arrays.fill(presentElements, -1);
63+
for (int i = 0; i < goodNums.size(); i++) {
64+
presentElements[goodNums.get(i)] = i;
65+
}
66+
for (int d : goodNums) {
67+
for (int i = d; i <= threshold; i += d) {
68+
if (presentElements[i] == -1) {
69+
presentElements[i] = presentElements[d];
70+
} else if (presentElements[i] != presentElements[d]) {
71+
uf.union(presentElements[i], presentElements[d]);
72+
}
73+
}
74+
}
75+
return uf.totalComponents + totalNums - goodNums.size();
76+
}
77+
}

0 commit comments

Comments
 (0)