-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
983 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...in/java/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package g3301_3400.s3375_minimum_operations_to_make_array_values_equal_to_k; | ||
|
||
// #Easy #Array #Hash_Table #2024_12_10_Time_3_ms_(78.92%)_Space_44.6_MB_(67.39%) | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class Solution { | ||
public int minOperations(int[] nums, int k) { | ||
Set<Integer> s = new HashSet<>(); | ||
for (int i : nums) { | ||
s.add(i); | ||
} | ||
int res = 0; | ||
for (int i : s) { | ||
if (i > k) { | ||
res++; | ||
} else if (i < k) { | ||
return -1; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...a/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
53 changes: 53 additions & 0 deletions
53
src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package g3301_3400.s3376_minimum_time_to_break_locks_i; | ||
|
||
// #Medium #Array #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask | ||
// #2024_12_10_Time_3_ms_(99.63%)_Space_42_MB_(92.34%) | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Solution { | ||
public int findMinimumTime(List<Integer> strength, int k) { | ||
List<Integer> strengthLocal = new ArrayList<>(strength); | ||
Collections.sort(strengthLocal); | ||
int res = strengthLocal.get(0); | ||
strengthLocal.remove(0); | ||
int x = 1; | ||
while (!strengthLocal.isEmpty()) { | ||
x += k; | ||
int nextTime = (strengthLocal.get(0) - 1) / x + 1; | ||
int canBreak = nextTime * x; | ||
int indexRemove = findIndex(strengthLocal, canBreak); | ||
if (strengthLocal.size() > 1) { | ||
int nextTime1 = (strengthLocal.get(1) - 1) / x + 1; | ||
int canBreak1 = nextTime1 * x; | ||
int indexRemove1 = findIndex(strengthLocal, canBreak1); | ||
if (nextTime1 + (strengthLocal.get(0) - 1) / (x + k) | ||
< nextTime + (strengthLocal.get(1) - 1) / (x + k)) { | ||
nextTime = nextTime1; | ||
indexRemove = indexRemove1; | ||
} | ||
} | ||
res += nextTime; | ||
strengthLocal.remove(indexRemove); | ||
} | ||
return res; | ||
} | ||
|
||
private int findIndex(List<Integer> strength, int canBreak) { | ||
int l = 0; | ||
int r = strength.size() - 1; | ||
int res = -1; | ||
while (l <= r) { | ||
int mid = (l + r) / 2; | ||
if (strength.get(mid) <= canBreak) { | ||
res = mid; | ||
l = mid + 1; | ||
} else { | ||
r = mid - 1; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/g3301_3400/s3376_minimum_time_to_break_locks_i/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
57 changes: 57 additions & 0 deletions
57
src/main/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package g3301_3400.s3377_digit_operations_to_make_two_integers_equal; | ||
|
||
// #Medium #Math #Heap_Priority_Queue #Graph #Shortest_Path #Number_Theory | ||
// #2024_12_10_Time_246_ms_(38.59%)_Space_45.2_MB_(73.52%) | ||
|
||
import java.util.Arrays; | ||
import java.util.PriorityQueue; | ||
|
||
public class Solution { | ||
public int minOperations(int n, int m) { | ||
int limit = 100000; | ||
boolean[] sieve = new boolean[limit + 1]; | ||
boolean[] visited = new boolean[limit]; | ||
Arrays.fill(sieve, true); | ||
sieve[0] = false; | ||
sieve[1] = false; | ||
for (int i = 2; i * i <= limit; i++) { | ||
if (sieve[i]) { | ||
for (int j = i * i; j <= limit; j += i) { | ||
sieve[j] = false; | ||
} | ||
} | ||
} | ||
if (sieve[n]) { | ||
return -1; | ||
} | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); | ||
visited[n] = true; | ||
pq.add(new int[] {n, n}); | ||
while (!pq.isEmpty()) { | ||
int[] current = pq.poll(); | ||
int cost = current[0]; | ||
int num = current[1]; | ||
char[] temp = Integer.toString(num).toCharArray(); | ||
if (num == m) { | ||
return cost; | ||
} | ||
for (int j = 0; j < temp.length; j++) { | ||
char old = temp[j]; | ||
for (int i = -1; i <= 1; i++) { | ||
int digit = old - '0'; | ||
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) { | ||
continue; | ||
} | ||
temp[j] = (char) (i + digit + '0'); | ||
int newnum = Integer.parseInt(new String(temp)); | ||
if (!sieve[newnum] && !visited[newnum]) { | ||
visited[newnum] = true; | ||
pq.add(new int[] {cost + newnum, newnum}); | ||
} | ||
} | ||
temp[j] = old; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ain/java/g3301_3400/s3377_digit_operations_to_make_two_integers_equal/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
78 changes: 78 additions & 0 deletions
78
src/main/java/g3301_3400/s3378_count_connected_components_in_lcm_graph/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package g3301_3400.s3378_count_connected_components_in_lcm_graph; | ||
|
||
// #Hard #Array #Hash_Table #Math #Union_Find #Number_Theory | ||
// #2024_12_10_Time_68_ms_(67.83%)_Space_59.8_MB_(62.24%) | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Solution { | ||
private static class Unionfind { | ||
int[] parent; | ||
int[] rank; | ||
int totalComponents; | ||
|
||
public Unionfind(int n) { | ||
parent = new int[n]; | ||
rank = new int[n]; | ||
totalComponents = n; | ||
for (int i = 0; i < n; i++) { | ||
parent[i] = i; | ||
} | ||
} | ||
|
||
public int find(int u) { | ||
if (parent[u] == u) { | ||
return u; | ||
} | ||
parent[u] = find(parent[u]); | ||
return parent[u]; | ||
} | ||
|
||
public void union(int u, int v) { | ||
int parentU = find(u); | ||
int parentV = find(v); | ||
if (parentU != parentV) { | ||
totalComponents--; | ||
if (rank[parentU] == rank[parentV]) { | ||
parent[parentV] = parentU; | ||
rank[parentU]++; | ||
} else if (rank[parentU] > rank[parentV]) { | ||
parent[parentV] = parentU; | ||
} else { | ||
parent[parentU] = parentV; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public int countComponents(int[] nums, int threshold) { | ||
List<Integer> goodNums = new ArrayList<>(); | ||
int totalNums = nums.length; | ||
for (int num : nums) { | ||
if (num <= threshold) { | ||
goodNums.add(num); | ||
} | ||
} | ||
if (goodNums.isEmpty()) { | ||
return totalNums; | ||
} | ||
Unionfind uf = new Unionfind(goodNums.size()); | ||
int[] presentElements = new int[threshold + 1]; | ||
Arrays.fill(presentElements, -1); | ||
for (int i = 0; i < goodNums.size(); i++) { | ||
presentElements[goodNums.get(i)] = i; | ||
} | ||
for (int d : goodNums) { | ||
for (int i = d; i <= threshold; i += 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(); | ||
} | ||
} |
Oops, something went wrong.