Skip to content

Commit 11eddef

Browse files
authored
Added tasks 3375-3389
1 parent e63949d commit 11eddef

File tree

13 files changed

+1210
-0
lines changed
  • src/main/kotlin/g3301_3400
    • s3375_minimum_operations_to_make_array_values_equal_to_k
    • s3376_minimum_time_to_break_locks_i
    • s3377_digit_operations_to_make_two_integers_equal
    • s3378_count_connected_components_in_lcm_graph
    • s3379_transformed_array
    • s3380_maximum_area_rectangle_with_point_constraints_i
    • s3381_maximum_subarray_sum_with_length_divisible_by_k
    • s3382_maximum_area_rectangle_with_point_constraints_ii
    • s3386_button_with_longest_push_time
    • s3387_maximize_amount_after_two_days_of_conversions
    • s3388_count_beautiful_splits_in_an_array
    • s3389_minimum_operations_to_make_character_frequencies_equal

13 files changed

+1210
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,18 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3389 |[Minimum Operations to Make Character Frequencies Equal](src/main/kotlin/g3301_3400/s3389_minimum_operations_to_make_character_frequencies_equal)| Hard | String, Hash_Table, Dynamic_Programming, Counting, Enumeration | 9 | 78.95
1820+
| 3388 |[Count Beautiful Splits in an Array](src/main/kotlin/g3301_3400/s3388_count_beautiful_splits_in_an_array)| Medium | Array, Dynamic_Programming | 155 | 100.00
1821+
| 3387 |[Maximize Amount After Two Days of Conversions](src/main/kotlin/g3301_3400/s3387_maximize_amount_after_two_days_of_conversions)| Medium | Array, String, Depth_First_Search, Breadth_First_Search, Graph | 13 | 88.46
1822+
| 3386 |[Button with Longest Push Time](src/main/kotlin/g3301_3400/s3386_button_with_longest_push_time)| Easy | Array | 1 | 100.00
1823+
| 3382 |[Maximum Area Rectangle With Point Constraints II](src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii)| Hard | Array, Math, Sorting, Geometry, Segment_Tree, Binary_Indexed_Tree | 518 | 100.00
1824+
| 3381 |[Maximum Subarray Sum With Length Divisible by K](src/main/kotlin/g3301_3400/s3381_maximum_subarray_sum_with_length_divisible_by_k)| Medium | Array, Hash_Table, Prefix_Sum | 6 | 100.00
1825+
| 3380 |[Maximum Area Rectangle With Point Constraints I](src/main/kotlin/g3301_3400/s3380_maximum_area_rectangle_with_point_constraints_i)| Medium | Array, Math, Sorting, Enumeration, Geometry, Segment_Tree, Binary_Indexed_Tree | 10 | 94.74
1826+
| 3379 |[Transformed Array](src/main/kotlin/g3301_3400/s3379_transformed_array)| Easy | Array, Simulation | 206 | 84.38
1827+
| 3378 |[Count Connected Components in LCM Graph](src/main/kotlin/g3301_3400/s3378_count_connected_components_in_lcm_graph)| Hard | Array, Hash_Table, Math, Union_Find, Number_Theory | 58 | 100.00
1828+
| 3377 |[Digit Operations to Make Two Integers Equal](src/main/kotlin/g3301_3400/s3377_digit_operations_to_make_two_integers_equal)| Medium | Math, Heap_Priority_Queue, Graph, Shortest_Path, Number_Theory | 215 | 100.00
1829+
| 3376 |[Minimum Time to Break Locks I](src/main/kotlin/g3301_3400/s3376_minimum_time_to_break_locks_i)| Medium | Array, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 202 | 100.00
1830+
| 3375 |[Minimum Operations to Make Array Values Equal to K](src/main/kotlin/g3301_3400/s3375_minimum_operations_to_make_array_values_equal_to_k)| Easy | Array, Hash_Table | 191 | 100.00
18191831
| 3373 |[Maximize the Number of Target Nodes After Connecting Trees II](src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii)| Hard | Depth_First_Search, Breadth_First_Search, Tree | 26 | 98.75
18201832
| 3372 |[Maximize the Number of Target Nodes After Connecting Trees I](src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i)| Medium | Depth_First_Search, Breadth_First_Search, Tree | 50 | 99.49
18211833
| 3371 |[Identify the Largest Outlier in an Array](src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array)| Medium | Array, Hash_Table, Counting, Enumeration | 5 | 100.00
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3375\. Minimum Operations to Make Array Values Equal to K
5+
6+
Easy
7+
8+
You are given an integer array `nums` and an integer `k`.
9+
10+
An integer `h` is called **valid** if all values in the array that are **strictly greater** than `h` are _identical_.
11+
12+
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.
13+
14+
You are allowed to perform the following operation on `nums`:
15+
16+
* Select an integer `h` that is _valid_ for the **current** values in `nums`.
17+
* For each index `i` where `nums[i] > h`, set `nums[i]` to `h`.
18+
19+
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.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [5,2,5,4,5], k = 2
24+
25+
**Output:** 2
26+
27+
**Explanation:**
28+
29+
The operations can be performed in order using valid integers 4 and then 2.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,1,2], k = 2
34+
35+
**Output:** \-1
36+
37+
**Explanation:**
38+
39+
It is impossible to make all the values equal to 2.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [9,7,5,3], k = 1
44+
45+
**Output:** 4
46+
47+
**Explanation:**
48+
49+
The operations can be performed using valid integers in the order 7, 5, 3, and 1.
50+
51+
**Constraints:**
52+
53+
* `1 <= nums.length <= 100`
54+
* `1 <= nums[i] <= 100`
55+
* `1 <= k <= 100`
56+
57+
## Solution
58+
59+
```kotlin
60+
class Solution {
61+
fun minOperations(nums: IntArray, k: Int): Int {
62+
val s: MutableSet<Int?> = HashSet<Int?>()
63+
for (i in nums) {
64+
s.add(i)
65+
}
66+
var res = 0
67+
for (i in s) {
68+
if (i!! > k) {
69+
res++
70+
} else if (i < k) {
71+
return -1
72+
}
73+
}
74+
return res
75+
}
76+
}
77+
```
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3376\. Minimum Time to Break Locks I
5+
6+
Medium
7+
8+
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.
9+
10+
To break a lock, Bob uses a sword with the following characteristics:
11+
12+
* The initial energy of the sword is 0.
13+
* The initial factor `X` by which the energy of the sword increases is 1.
14+
* Every minute, the energy of the sword increases by the current factor `X`.
15+
* To break the <code>i<sup>th</sup></code> lock, the energy of the sword must reach **at least** `strength[i]`.
16+
* After breaking a lock, the energy of the sword resets to 0, and the factor `X` increases by a given value `K`.
17+
18+
Your task is to determine the **minimum** time in minutes required for Bob to break all `n` locks and escape the dungeon.
19+
20+
Return the **minimum** time required for Bob to break all `n` locks.
21+
22+
**Example 1:**
23+
24+
**Input:** strength = [3,4,1], K = 1
25+
26+
**Output:** 4
27+
28+
**Explanation:**
29+
30+
| Time | Energy | X | Action | Updated X |
31+
|------|--------|---|----------------------|-----------|
32+
| 0 | 0 | 1 | Nothing | 1 |
33+
| 1 | 1 | 1 | Break 3rd Lock | 2 |
34+
| 2 | 2 | 2 | Nothing | 2 |
35+
| 3 | 4 | 2 | Break 2nd Lock | 3 |
36+
| 4 | 3 | 3 | Break 1st Lock | 3 |
37+
38+
The locks cannot be broken in less than 4 minutes; thus, the answer is 4.
39+
40+
**Example 2:**
41+
42+
**Input:** strength = [2,5,4], K = 2
43+
44+
**Output:** 5
45+
46+
**Explanation:**
47+
48+
| Time | Energy | X | Action | Updated X |
49+
|------|--------|---|----------------------|-----------|
50+
| 0 | 0 | 1 | Nothing | 1 |
51+
| 1 | 1 | 1 | Nothing | 1 |
52+
| 2 | 2 | 1 | Break 1st Lock | 3 |
53+
| 3 | 3 | 3 | Nothing | 3 |
54+
| 4 | 6 | 3 | Break 2nd Lock | 5 |
55+
| 5 | 5 | 5 | Break 3rd Lock | 7 |
56+
57+
The locks cannot be broken in less than 5 minutes; thus, the answer is 5.
58+
59+
**Constraints:**
60+
61+
* `n == strength.length`
62+
* `1 <= n <= 8`
63+
* `1 <= K <= 10`
64+
* <code>1 <= strength[i] <= 10<sup>6</sup></code>
65+
66+
## Solution
67+
68+
```kotlin
69+
import kotlin.math.min
70+
71+
class Solution {
72+
fun findMinimumTime(strength: List<Int>, k: Int): Int {
73+
val perm: MutableList<Int> = ArrayList<Int>(strength)
74+
perm.sort()
75+
var minTime = Int.Companion.MAX_VALUE
76+
do {
77+
var time = 0
78+
var factor = 1
79+
for (required in perm) {
80+
val neededTime = (required + factor - 1) / factor
81+
time += neededTime
82+
factor += k
83+
}
84+
minTime = min(minTime, time)
85+
} while (nextPermutation(perm))
86+
return minTime
87+
}
88+
89+
private fun nextPermutation(nums: MutableList<Int>): Boolean {
90+
var i = nums.size - 2
91+
while (i >= 0 && nums[i] >= nums[i + 1]) {
92+
i--
93+
}
94+
if (i < 0) {
95+
return false
96+
}
97+
var j = nums.size - 1
98+
while (nums[j] <= nums[i]) {
99+
j--
100+
}
101+
val temp = nums[i]
102+
nums[i] = nums[j]
103+
nums[j] = temp
104+
nums.subList(i + 1, nums.size).reverse()
105+
return true
106+
}
107+
}
108+
```
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3377\. Digit Operations to Make Two Integers Equal
5+
6+
Medium
7+
8+
You are given two integers `n` and `m` that consist of the **same** number of digits.
9+
10+
You can perform the following operations **any** number of times:
11+
12+
* Choose **any** digit from `n` that is not 9 and **increase** it by 1.
13+
* Choose **any** digit from `n` that is not 0 and **decrease** it by 1.
14+
15+
The integer `n` must not be a **prime** number at any point, including its original value and after each operation.
16+
17+
The cost of a transformation is the sum of **all** values that `n` takes throughout the operations performed.
18+
19+
Return the **minimum** cost to transform `n` into `m`. If it is impossible, return -1.
20+
21+
A prime number is a natural number greater than 1 with only two factors, 1 and itself.
22+
23+
**Example 1:**
24+
25+
**Input:** n = 10, m = 12
26+
27+
**Output:** 85
28+
29+
**Explanation:**
30+
31+
We perform the following operations:
32+
33+
* Increase the first digit, now <code>n = <ins>**2**</ins>0</code>.
34+
* Increase the second digit, now <code>n = 2**<ins>1</ins>**</code>.
35+
* Increase the second digit, now <code>n = 2**<ins>2</ins>**</code>.
36+
* Decrease the first digit, now <code>n = **<ins>1</ins>**2</code>.
37+
38+
**Example 2:**
39+
40+
**Input:** n = 4, m = 8
41+
42+
**Output:** \-1
43+
44+
**Explanation:**
45+
46+
It is impossible to make `n` equal to `m`.
47+
48+
**Example 3:**
49+
50+
**Input:** n = 6, m = 2
51+
52+
**Output:** \-1
53+
54+
**Explanation:**
55+
56+
Since 2 is already a prime, we can't make `n` equal to `m`.
57+
58+
**Constraints:**
59+
60+
* <code>1 <= n, m < 10<sup>4</sup></code>
61+
* `n` and `m` consist of the same number of digits.
62+
63+
## Solution
64+
65+
```kotlin
66+
import java.util.PriorityQueue
67+
68+
class Solution {
69+
fun minOperations(n: Int, m: Int): Int {
70+
val limit = 100000
71+
val sieve = BooleanArray(limit + 1)
72+
val visited = BooleanArray(limit)
73+
sieve.fill(true)
74+
sieve[0] = false
75+
sieve[1] = false
76+
var i = 2
77+
while (i * i <= limit) {
78+
if (sieve[i]) {
79+
var j = i * i
80+
while (j <= limit) {
81+
sieve[j] = false
82+
j += i
83+
}
84+
}
85+
i++
86+
}
87+
if (sieve[n]) {
88+
return -1
89+
}
90+
val pq = PriorityQueue<IntArray>(Comparator { a: IntArray, b: IntArray -> a[0] - b[0] })
91+
visited[n] = true
92+
pq.add(intArrayOf(n, n))
93+
while (pq.isNotEmpty()) {
94+
val current = pq.poll()
95+
val cost = current[0]
96+
val num = current[1]
97+
val temp = num.toString().toCharArray()
98+
if (num == m) {
99+
return cost
100+
}
101+
for (j in temp.indices) {
102+
val old = temp[j]
103+
for (i in -1..1) {
104+
val digit = old.code - '0'.code
105+
if ((digit == 9 && i == 1) || (digit == 0 && i == -1)) {
106+
continue
107+
}
108+
temp[j] = (i + digit + '0'.code).toChar()
109+
val newNum = String(temp).toInt()
110+
if (!sieve[newNum] && !visited[newNum]) {
111+
visited[newNum] = true
112+
pq.add(intArrayOf(cost + newNum, newNum))
113+
}
114+
}
115+
temp[j] = old
116+
}
117+
}
118+
return -1
119+
}
120+
}
121+
```

0 commit comments

Comments
 (0)