Skip to content

Commit f814a48

Browse files
committed
Added tasks 3572-3579
1 parent db87ee6 commit f814a48

File tree

10 files changed

+890
-2
lines changed
  • src/main/kotlin
    • g3201_3300/s3248_snake_in_matrix
    • g3501_3600
      • s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues
      • s3573_best_time_to_buy_and_sell_stock_v
      • s3574_maximize_subarray_gcd_score
      • s3575_maximum_good_subtree_score
      • s3576_transform_array_to_all_equal_elements
      • s3577_count_the_number_of_computer_unlocking_permutations
      • s3578_count_partitions_with_max_min_difference_at_most_k
      • s3579_minimum_steps_to_convert_string_with_operations

10 files changed

+890
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3579 |[Minimum Steps to Convert String with Operations](src/main/kotlin/g3501_3600/s3579_minimum_steps_to_convert_string_with_operations)| Hard | String, Dynamic_Programming, Greedy | 107 | 100.00
2092+
| 3578 |[Count Partitions With Max-Min Difference at Most K](src/main/kotlin/g3501_3600/s3578_count_partitions_with_max_min_difference_at_most_k)| Medium | Array, Dynamic_Programming, Prefix_Sum, Sliding_Window, Queue, Monotonic_Queue | 33 | 100.00
2093+
| 3577 |[Count the Number of Computer Unlocking Permutations](src/main/kotlin/g3501_3600/s3577_count_the_number_of_computer_unlocking_permutations)| Medium | Array, Math, Combinatorics, Brainteaser | 2 | 100.00
2094+
| 3576 |[Transform Array to All Equal Elements](src/main/kotlin/g3501_3600/s3576_transform_array_to_all_equal_elements)| Medium | Array, Greedy | 11 | 92.31
2095+
| 3575 |[Maximum Good Subtree Score](src/main/kotlin/g3501_3600/s3575_maximum_good_subtree_score)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Bit_Manipulation, Bitmask | 71 | 100.00
2096+
| 3574 |[Maximize Subarray GCD Score](src/main/kotlin/g3501_3600/s3574_maximize_subarray_gcd_score)| Hard | Array, Math, Enumeration, Number_Theory | 19 | 100.00
2097+
| 3573 |[Best Time to Buy and Sell Stock V](src/main/kotlin/g3501_3600/s3573_best_time_to_buy_and_sell_stock_v)| Medium | Array, Dynamic_Programming | 27 | 100.00
2098+
| 3572 |[Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values](src/main/kotlin/g3501_3600/s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues)| Medium | Array, Hash_Table, Sorting, Greedy, Heap_Priority_Queue | 5 | 100.00
20912099
| 3570 |[Find Books with No Available Copies](src/main/kotlin/g3501_3600/s3570_find_books_with_no_available_copies)| Easy | Database | 512 | 100.00
20922100
| 3569 |[Maximize Count of Distinct Primes After Split](src/main/kotlin/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split)| Hard | Array, Math, Segment_Tree, Number_Theory | 441 | 100.00
20932101
| 3568 |[Minimum Moves to Clean the Classroom](src/main/kotlin/g3501_3600/s3568_minimum_moves_to_clean_the_classroom)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix, Bit_Manipulation | 149 | 100.00

src/main/kotlin/g3201_3300/s3248_snake_in_matrix/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Return the position of the final cell where the snake ends up after executing `c
2121

2222
**Explanation:**
2323

24-
![image](image01.png)
24+
![image](https://leetcode-images.github.io/g3201_3300/s3248_snake_in_matrix/image01.png)
2525

2626
**Example 2:**
2727

@@ -31,7 +31,7 @@ Return the position of the final cell where the snake ends up after executing `c
3131

3232
**Explanation:**
3333

34-
![image](image02.png)
34+
![image](https://leetcode-images.github.io/g3201_3300/s3248_snake_in_matrix/image02.png)
3535

3636
**Constraints:**
3737

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 3572\. Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values
5+
6+
Medium
7+
8+
You are given two integer arrays `x` and `y`, each of length `n`. You must choose three **distinct** indices `i`, `j`, and `k` such that:
9+
10+
* `x[i] != x[j]`
11+
* `x[j] != x[k]`
12+
* `x[k] != x[i]`
13+
14+
Your goal is to **maximize** the value of `y[i] + y[j] + y[k]` under these conditions. Return the **maximum** possible sum that can be obtained by choosing such a triplet of indices.
15+
16+
If no such triplet exists, return -1.
17+
18+
**Example 1:**
19+
20+
**Input:** x = [1,2,1,3,2], y = [5,3,4,6,2]
21+
22+
**Output:** 14
23+
24+
**Explanation:**
25+
26+
* Choose `i = 0` (`x[i] = 1`, `y[i] = 5`), `j = 1` (`x[j] = 2`, `y[j] = 3`), `k = 3` (`x[k] = 3`, `y[k] = 6`).
27+
* All three values chosen from `x` are distinct. `5 + 3 + 6 = 14` is the maximum we can obtain. Hence, the output is 14.
28+
29+
**Example 2:**
30+
31+
**Input:** x = [1,2,1,2], y = [4,5,6,7]
32+
33+
**Output:** \-1
34+
35+
**Explanation:**
36+
37+
* There are only two distinct values in `x`. Hence, the output is -1.
38+
39+
**Constraints:**
40+
41+
* `n == x.length == y.length`
42+
* <code>3 <= n <= 10<sup>5</sup></code>
43+
* <code>1 <= x[i], y[i] <= 10<sup>6</sup></code>
44+
45+
## Solution
46+
47+
```kotlin
48+
class Solution {
49+
fun maxSumDistinctTriplet(x: IntArray, y: IntArray): Int {
50+
var index = -1
51+
var max = -1
52+
var sum = 0
53+
for (i in y.indices) {
54+
if (y[i] > max) {
55+
max = y[i]
56+
index = i
57+
}
58+
}
59+
sum += max
60+
if (max == -1) {
61+
return -1
62+
}
63+
var index2 = -1
64+
max = -1
65+
for (i in y.indices) {
66+
if (y[i] > max && x[i] != x[index]) {
67+
max = y[i]
68+
index2 = i
69+
}
70+
}
71+
sum += max
72+
if (max == -1) {
73+
return -1
74+
}
75+
max = -1
76+
for (i in y.indices) {
77+
if (y[i] > max && x[i] != x[index] && x[i] != x[index2]) {
78+
max = y[i]
79+
}
80+
}
81+
if (max == -1) {
82+
return -1
83+
}
84+
sum += max
85+
return sum
86+
}
87+
}
88+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
## 3573\. Best Time to Buy and Sell Stock V
5+
6+
Medium
7+
8+
You are given an integer array `prices` where `prices[i]` is the price of a stock in dollars on the <code>i<sup>th</sup></code> day, and an integer `k`.
9+
10+
You are allowed to make at most `k` transactions, where each transaction can be either of the following:
11+
12+
* **Normal transaction**: Buy on day `i`, then sell on a later day `j` where `i < j`. You profit `prices[j] - prices[i]`.
13+
14+
* **Short selling transaction**: Sell on day `i`, then buy back on a later day `j` where `i < j`. You profit `prices[i] - prices[j]`.
15+
16+
17+
**Note** that you must complete each transaction before starting another. Additionally, you can't buy or sell on the same day you are selling or buying back as part of a previous transaction.
18+
19+
Return the **maximum** total profit you can earn by making **at most** `k` transactions.
20+
21+
**Example 1:**
22+
23+
**Input:** prices = [1,7,9,8,2], k = 2
24+
25+
**Output:** 14
26+
27+
**Explanation:**
28+
29+
We can make $14 of profit through 2 transactions:
30+
31+
* A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.
32+
* A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.
33+
34+
**Example 2:**
35+
36+
**Input:** prices = [12,16,19,19,8,1,19,13,9], k = 3
37+
38+
**Output:** 36
39+
40+
**Explanation:**
41+
42+
We can make $36 of profit through 3 transactions:
43+
44+
* A normal transaction: buy the stock on day 0 for $12 then sell it on day 2 for $19.
45+
* A short selling transaction: sell the stock on day 3 for $19 then buy back on day 4 for $8.
46+
* A normal transaction: buy the stock on day 5 for $1 then sell it on day 6 for $19.
47+
48+
**Constraints:**
49+
50+
* <code>2 <= prices.length <= 10<sup>3</sup></code>
51+
* <code>1 <= prices[i] <= 10<sup>9</sup></code>
52+
* `1 <= k <= prices.length / 2`
53+
54+
## Solution
55+
56+
```kotlin
57+
import kotlin.math.max
58+
59+
class Solution {
60+
fun maximumProfit(prices: IntArray, k: Int): Long {
61+
val n = prices.size
62+
var prev = LongArray(n)
63+
var curr = LongArray(n)
64+
for (t in 1..k) {
65+
var bestLong = -prices[0].toLong()
66+
var bestShort = prices[0].toLong()
67+
curr[0] = 0
68+
for (i in 1..<n) {
69+
var res = curr[i - 1]
70+
res = max(res, prices[i] + bestLong)
71+
res = max(res, -prices[i] + bestShort)
72+
curr[i] = res
73+
bestLong = max(bestLong, prev[i - 1] - prices[i])
74+
bestShort = max(bestShort, prev[i - 1] + prices[i])
75+
}
76+
val tmp = prev
77+
prev = curr
78+
curr = tmp
79+
}
80+
return prev[n - 1]
81+
}
82+
}
83+
```
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
## 3574\. Maximize Subarray GCD Score
5+
6+
Hard
7+
8+
You are given an array of positive integers `nums` and an integer `k`.
9+
10+
You may perform at most `k` operations. In each operation, you can choose one element in the array and **double** its value. Each element can be doubled **at most** once.
11+
12+
The **score** of a contiguous **subarray** is defined as the **product** of its length and the _greatest common divisor (GCD)_ of all its elements.
13+
14+
Your task is to return the **maximum** **score** that can be achieved by selecting a contiguous subarray from the modified array.
15+
16+
**Note:**
17+
18+
* The **greatest common divisor (GCD)** of an array is the largest integer that evenly divides all the array elements.
19+
20+
**Example 1:**
21+
22+
**Input:** nums = [2,4], k = 1
23+
24+
**Output:** 8
25+
26+
**Explanation:**
27+
28+
* Double `nums[0]` to 4 using one operation. The modified array becomes `[4, 4]`.
29+
* The GCD of the subarray `[4, 4]` is 4, and the length is 2.
30+
* Thus, the maximum possible score is `2 × 4 = 8`.
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [3,5,7], k = 2
35+
36+
**Output:** 14
37+
38+
**Explanation:**
39+
40+
* Double `nums[2]` to 14 using one operation. The modified array becomes `[3, 5, 14]`.
41+
* The GCD of the subarray `[14]` is 14, and the length is 1.
42+
* Thus, the maximum possible score is `1 × 14 = 14`.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [5,5,5], k = 1
47+
48+
**Output:** 15
49+
50+
**Explanation:**
51+
52+
* The subarray `[5, 5, 5]` has a GCD of 5, and its length is 3.
53+
* Since doubling any element doesn't improve the score, the maximum score is `3 × 5 = 15`.
54+
55+
**Constraints:**
56+
57+
* `1 <= n == nums.length <= 1500`
58+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
59+
* `1 <= k <= n`
60+
61+
## Solution
62+
63+
```kotlin
64+
import kotlin.math.max
65+
66+
class Solution {
67+
fun maxGCDScore(nums: IntArray, k: Int): Long {
68+
var mx = 0
69+
for (x in nums) {
70+
mx = max(mx, x)
71+
}
72+
val width = 32 - Integer.numberOfLeadingZeros(mx)
73+
val lowBitPos: Array<MutableList<Int>> = Array<MutableList<Int>>(width) { _ -> ArrayList<Int>() }
74+
val intervals = Array<IntArray>(width + 1) { IntArray(3) }
75+
var size = 0
76+
var ans: Long = 0
77+
for (i in nums.indices) {
78+
val x = nums[i]
79+
val tz = Integer.numberOfTrailingZeros(x)
80+
lowBitPos[tz].add(i)
81+
for (j in 0..<size) {
82+
intervals[j][0] = gcd(intervals[j][0], x)
83+
}
84+
intervals[size][0] = x
85+
intervals[size][1] = i - 1
86+
intervals[size][2] = i
87+
size++
88+
var idx = 1
89+
for (j in 1..<size) {
90+
if (intervals[j][0] != intervals[j - 1][0]) {
91+
intervals[idx][0] = intervals[j][0]
92+
intervals[idx][1] = intervals[j][1]
93+
intervals[idx][2] = intervals[j][2]
94+
idx++
95+
} else {
96+
intervals[idx - 1][2] = intervals[j][2]
97+
}
98+
}
99+
size = idx
100+
for (j in 0..<size) {
101+
val g = intervals[j][0]
102+
val l = intervals[j][1]
103+
val r = intervals[j][2]
104+
ans = max(ans, g.toLong() * (i - l))
105+
val pos = lowBitPos[Integer.numberOfTrailingZeros(g)]
106+
val minL = if (pos.size > k) max(l, pos[pos.size - k - 1]) else l
107+
if (minL < r) {
108+
ans = max(ans, g.toLong() * 2 * (i - minL))
109+
}
110+
}
111+
}
112+
return ans
113+
}
114+
115+
private fun gcd(a: Int, b: Int): Int {
116+
var a = a
117+
var b = b
118+
while (a != 0) {
119+
val tmp = a
120+
a = b % a
121+
b = tmp
122+
}
123+
return b
124+
}
125+
}
126+
```

0 commit comments

Comments
 (0)