Skip to content

Commit cb1055c

Browse files
committed
Added tasks 3572-3579
1 parent 74fa51c commit cb1055c

File tree

9 files changed

+862
-0
lines changed
  • src/main/java/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

9 files changed

+862
-0
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/java/g3501_3600/s3579_minimum_steps_to_convert_string_with_operations)| Hard | String, Dynamic_Programming, Greedy | 50 | 98.37
2092+
| 3578 |[Count Partitions With Max-Min Difference at Most K](src/main/java/g3501_3600/s3578_count_partitions_with_max_min_difference_at_most_k)| Medium | Array, Dynamic_Programming, Prefix_Sum, Sliding_Window, Queue, Monotonic_Queue | 16 | 99.88
2093+
| 3577 |[Count the Number of Computer Unlocking Permutations](src/main/java/g3501_3600/s3577_count_the_number_of_computer_unlocking_permutations)| Medium | Array, Math, Combinatorics, Brainteaser | 1 | 100.00
2094+
| 3576 |[Transform Array to All Equal Elements](src/main/java/g3501_3600/s3576_transform_array_to_all_equal_elements)| Medium | Array, Greedy | 7 | 99.81
2095+
| 3575 |[Maximum Good Subtree Score](src/main/java/g3501_3600/s3575_maximum_good_subtree_score)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree, Bit_Manipulation, Bitmask | 92 | 98.73
2096+
| 3574 |[Maximize Subarray GCD Score](src/main/java/g3501_3600/s3574_maximize_subarray_gcd_score)| Hard | Array, Math, Enumeration, Number_Theory | 13 | 100.00
2097+
| 3573 |[Best Time to Buy and Sell Stock V](src/main/java/g3501_3600/s3573_best_time_to_buy_and_sell_stock_v)| Medium | Array, Dynamic_Programming | 10 | 99.46
2098+
| 3572 |[Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values](src/main/java/g3501_3600/s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues)| Medium | Array, Hash_Table, Sorting, Greedy, Heap_Priority_Queue | 2 | 100.00
20912099
| 3570 |[Find Books with No Available Copies](src/main/java/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/java/g3501_3600/s3569_maximize_count_of_distinct_primes_after_split)| Hard | Array, Math, Segment_Tree, Number_Theory | 280 | 97.30
20932101
| 3568 |[Minimum Moves to Clean the Classroom](src/main/java/g3501_3600/s3568_minimum_moves_to_clean_the_classroom)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix, Bit_Manipulation | 94 | 99.86
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-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
48+
public class Solution {
49+
public int maxSumDistinctTriplet(int[] x, int[] y) {
50+
int index = -1;
51+
int max = -1;
52+
int sum = 0;
53+
for (int i = 0; i < y.length; i++) {
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+
int index2 = -1;
64+
max = -1;
65+
for (int i = 0; i < y.length; i++) {
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 (int i = 0; i < y.length; i++) {
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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
57+
public class Solution {
58+
public long maximumProfit(int[] prices, int k) {
59+
int n = prices.length;
60+
long[] prev = new long[n];
61+
long[] curr = new long[n];
62+
for (int t = 1; t <= k; t++) {
63+
long bestLong = -prices[0];
64+
long bestShort = prices[0];
65+
curr[0] = 0;
66+
for (int i = 1; i < n; i++) {
67+
long res = curr[i - 1];
68+
res = Math.max(res, prices[i] + bestLong);
69+
res = Math.max(res, -prices[i] + bestShort);
70+
curr[i] = res;
71+
bestLong = Math.max(bestLong, prev[i - 1] - prices[i]);
72+
bestShort = Math.max(bestShort, prev[i - 1] + prices[i]);
73+
}
74+
long[] tmp = prev;
75+
prev = curr;
76+
curr = tmp;
77+
}
78+
return prev[n - 1];
79+
}
80+
}
81+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/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+
```java
64+
import java.util.ArrayList;
65+
import java.util.Arrays;
66+
import java.util.List;
67+
68+
@SuppressWarnings("unchecked")
69+
public class Solution {
70+
public long maxGCDScore(int[] nums, int k) {
71+
int mx = 0;
72+
for (int x : nums) {
73+
mx = Math.max(mx, x);
74+
}
75+
int width = 32 - Integer.numberOfLeadingZeros(mx);
76+
List<Integer>[] lowbitPos = new List[width];
77+
Arrays.setAll(lowbitPos, i -> new ArrayList<>());
78+
int[][] intervals = new int[width + 1][3];
79+
int size = 0;
80+
long ans = 0;
81+
for (int i = 0; i < nums.length; i++) {
82+
int x = nums[i];
83+
int tz = Integer.numberOfTrailingZeros(x);
84+
lowbitPos[tz].add(i);
85+
for (int j = 0; j < size; j++) {
86+
intervals[j][0] = gcd(intervals[j][0], x);
87+
}
88+
intervals[size][0] = x;
89+
intervals[size][1] = i - 1;
90+
intervals[size][2] = i;
91+
size++;
92+
int idx = 1;
93+
for (int j = 1; j < size; j++) {
94+
if (intervals[j][0] != intervals[j - 1][0]) {
95+
intervals[idx][0] = intervals[j][0];
96+
intervals[idx][1] = intervals[j][1];
97+
intervals[idx][2] = intervals[j][2];
98+
idx++;
99+
} else {
100+
intervals[idx - 1][2] = intervals[j][2];
101+
}
102+
}
103+
size = idx;
104+
for (int j = 0; j < size; j++) {
105+
int g = intervals[j][0];
106+
int l = intervals[j][1];
107+
int r = intervals[j][2];
108+
ans = Math.max(ans, (long) g * (i - l));
109+
List<Integer> pos = lowbitPos[Integer.numberOfTrailingZeros(g)];
110+
int minL = pos.size() > k ? Math.max(l, pos.get(pos.size() - k - 1)) : l;
111+
if (minL < r) {
112+
ans = Math.max(ans, (long) g * 2 * (i - minL));
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
119+
private int gcd(int a, int b) {
120+
while (a != 0) {
121+
int tmp = a;
122+
a = b % a;
123+
b = tmp;
124+
}
125+
return b;
126+
}
127+
}
128+
```

0 commit comments

Comments
 (0)