Skip to content

Commit 87417fe

Browse files
authored
Added tasks 3375-3382
1 parent 664f2ba commit 87417fe

File tree

24 files changed

+983
-0
lines changed

24 files changed

+983
-0
lines changed
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 #Array #Hash_Table #2024_12_10_Time_3_ms_(78.92%)_Space_44.6_MB_(67.39%)
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+
}
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,53 @@
1+
package g3301_3400.s3376_minimum_time_to_break_locks_i;
2+
3+
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2024_12_10_Time_3_ms_(99.63%)_Space_42_MB_(92.34%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public class Solution {
11+
public int findMinimumTime(List<Integer> strength, int k) {
12+
List<Integer> strengthLocal = new ArrayList<>(strength);
13+
Collections.sort(strengthLocal);
14+
int res = strengthLocal.get(0);
15+
strengthLocal.remove(0);
16+
int x = 1;
17+
while (!strengthLocal.isEmpty()) {
18+
x += k;
19+
int nextTime = (strengthLocal.get(0) - 1) / x + 1;
20+
int canBreak = nextTime * x;
21+
int indexRemove = findIndex(strengthLocal, canBreak);
22+
if (strengthLocal.size() > 1) {
23+
int nextTime1 = (strengthLocal.get(1) - 1) / x + 1;
24+
int canBreak1 = nextTime1 * x;
25+
int indexRemove1 = findIndex(strengthLocal, canBreak1);
26+
if (nextTime1 + (strengthLocal.get(0) - 1) / (x + k)
27+
< nextTime + (strengthLocal.get(1) - 1) / (x + k)) {
28+
nextTime = nextTime1;
29+
indexRemove = indexRemove1;
30+
}
31+
}
32+
res += nextTime;
33+
strengthLocal.remove(indexRemove);
34+
}
35+
return res;
36+
}
37+
38+
private int findIndex(List<Integer> strength, int canBreak) {
39+
int l = 0;
40+
int r = strength.size() - 1;
41+
int res = -1;
42+
while (l <= r) {
43+
int mid = (l + r) / 2;
44+
if (strength.get(mid) <= canBreak) {
45+
res = mid;
46+
l = mid + 1;
47+
} else {
48+
r = mid - 1;
49+
}
50+
}
51+
return res;
52+
}
53+
}
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,57 @@
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_10_Time_246_ms_(38.59%)_Space_45.2_MB_(73.52%)
5+
6+
import java.util.Arrays;
7+
import java.util.PriorityQueue;
8+
9+
public class Solution {
10+
public int minOperations(int n, int m) {
11+
int limit = 100000;
12+
boolean[] sieve = new boolean[limit + 1];
13+
boolean[] visited = new boolean[limit];
14+
Arrays.fill(sieve, true);
15+
sieve[0] = false;
16+
sieve[1] = false;
17+
for (int i = 2; i * i <= limit; i++) {
18+
if (sieve[i]) {
19+
for (int j = i * i; j <= limit; j += i) {
20+
sieve[j] = false;
21+
}
22+
}
23+
}
24+
if (sieve[n]) {
25+
return -1;
26+
}
27+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
28+
visited[n] = true;
29+
pq.add(new int[] {n, n});
30+
while (!pq.isEmpty()) {
31+
int[] current = pq.poll();
32+
int cost = current[0];
33+
int num = current[1];
34+
char[] temp = Integer.toString(num).toCharArray();
35+
if (num == m) {
36+
return cost;
37+
}
38+
for (int j = 0; j < temp.length; j++) {
39+
char old = temp[j];
40+
for (int i = -1; i <= 1; i++) {
41+
int digit = old - '0';
42+
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) {
43+
continue;
44+
}
45+
temp[j] = (char) (i + digit + '0');
46+
int newnum = Integer.parseInt(new String(temp));
47+
if (!sieve[newnum] && !visited[newnum]) {
48+
visited[newnum] = true;
49+
pq.add(new int[] {cost + newnum, newnum});
50+
}
51+
}
52+
temp[j] = old;
53+
}
54+
}
55+
return -1;
56+
}
57+
}
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,78 @@
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_10_Time_68_ms_(67.83%)_Space_59.8_MB_(62.24%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class Solution {
11+
private static class Unionfind {
12+
int[] parent;
13+
int[] rank;
14+
int totalComponents;
15+
16+
public Unionfind(int n) {
17+
parent = new int[n];
18+
rank = new int[n];
19+
totalComponents = n;
20+
for (int i = 0; i < n; i++) {
21+
parent[i] = i;
22+
}
23+
}
24+
25+
public int find(int u) {
26+
if (parent[u] == u) {
27+
return u;
28+
}
29+
parent[u] = find(parent[u]);
30+
return parent[u];
31+
}
32+
33+
public void union(int u, int v) {
34+
int parentU = find(u);
35+
int parentV = find(v);
36+
if (parentU != parentV) {
37+
totalComponents--;
38+
if (rank[parentU] == rank[parentV]) {
39+
parent[parentV] = parentU;
40+
rank[parentU]++;
41+
} else if (rank[parentU] > rank[parentV]) {
42+
parent[parentV] = parentU;
43+
} else {
44+
parent[parentU] = parentV;
45+
}
46+
}
47+
}
48+
}
49+
50+
public int countComponents(int[] nums, int threshold) {
51+
List<Integer> goodNums = new ArrayList<>();
52+
int totalNums = nums.length;
53+
for (int num : nums) {
54+
if (num <= threshold) {
55+
goodNums.add(num);
56+
}
57+
}
58+
if (goodNums.isEmpty()) {
59+
return totalNums;
60+
}
61+
Unionfind uf = new Unionfind(goodNums.size());
62+
int[] presentElements = new int[threshold + 1];
63+
Arrays.fill(presentElements, -1);
64+
for (int i = 0; i < goodNums.size(); i++) {
65+
presentElements[goodNums.get(i)] = i;
66+
}
67+
for (int d : goodNums) {
68+
for (int i = d; i <= threshold; i += d) {
69+
if (presentElements[i] == -1) {
70+
presentElements[i] = presentElements[d];
71+
} else if (presentElements[i] != presentElements[d]) {
72+
uf.union(presentElements[i], presentElements[d]);
73+
}
74+
}
75+
}
76+
return uf.totalComponents + totalNums - goodNums.size();
77+
}
78+
}

0 commit comments

Comments
 (0)