Skip to content

Commit 3397ce1

Browse files
authored
Added tasks 3572-3579
1 parent dee89ed commit 3397ce1

File tree

24 files changed

+1068
-0
lines changed

24 files changed

+1068
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3501_3600.s3572_maximize_ysum_by_picking_a_triplet_of_distinct_xvalues
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue
4+
// #2025_06_10_Time_5_ms_(100.00%)_Space_82.11_MB_(56.00%)
5+
6+
class Solution {
7+
fun maxSumDistinctTriplet(x: IntArray, y: IntArray): Int {
8+
var index = -1
9+
var max = -1
10+
var sum = 0
11+
for (i in y.indices) {
12+
if (y[i] > max) {
13+
max = y[i]
14+
index = i
15+
}
16+
}
17+
sum += max
18+
if (max == -1) {
19+
return -1
20+
}
21+
var index2 = -1
22+
max = -1
23+
for (i in y.indices) {
24+
if (y[i] > max && x[i] != x[index]) {
25+
max = y[i]
26+
index2 = i
27+
}
28+
}
29+
sum += max
30+
if (max == -1) {
31+
return -1
32+
}
33+
max = -1
34+
for (i in y.indices) {
35+
if (y[i] > max && x[i] != x[index] && x[i] != x[index2]) {
36+
max = y[i]
37+
}
38+
}
39+
if (max == -1) {
40+
return -1
41+
}
42+
sum += max
43+
return sum
44+
}
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3572\. Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values
2+
3+
Medium
4+
5+
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:
6+
7+
* `x[i] != x[j]`
8+
* `x[j] != x[k]`
9+
* `x[k] != x[i]`
10+
11+
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.
12+
13+
If no such triplet exists, return -1.
14+
15+
**Example 1:**
16+
17+
**Input:** x = [1,2,1,3,2], y = [5,3,4,6,2]
18+
19+
**Output:** 14
20+
21+
**Explanation:**
22+
23+
* 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`).
24+
* All three values chosen from `x` are distinct. `5 + 3 + 6 = 14` is the maximum we can obtain. Hence, the output is 14.
25+
26+
**Example 2:**
27+
28+
**Input:** x = [1,2,1,2], y = [4,5,6,7]
29+
30+
**Output:** \-1
31+
32+
**Explanation:**
33+
34+
* There are only two distinct values in `x`. Hence, the output is -1.
35+
36+
**Constraints:**
37+
38+
* `n == x.length == y.length`
39+
* <code>3 <= n <= 10<sup>5</sup></code>
40+
* <code>1 <= x[i], y[i] <= 10<sup>6</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g3501_3600.s3573_best_time_to_buy_and_sell_stock_v
2+
3+
// #Medium #Array #Dynamic_Programming #2025_06_10_Time_27_ms_(100.00%)_Space_48.69_MB_(80.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumProfit(prices: IntArray, k: Int): Long {
9+
val n = prices.size
10+
var prev = LongArray(n)
11+
var curr = LongArray(n)
12+
for (t in 1..k) {
13+
var bestLong = -prices[0].toLong()
14+
var bestShort = prices[0].toLong()
15+
curr[0] = 0
16+
for (i in 1..<n) {
17+
var res = curr[i - 1]
18+
res = max(res, prices[i] + bestLong)
19+
res = max(res, -prices[i] + bestShort)
20+
curr[i] = res
21+
bestLong = max(bestLong, prev[i - 1] - prices[i])
22+
bestShort = max(bestShort, prev[i - 1] + prices[i])
23+
}
24+
val tmp = prev
25+
prev = curr
26+
curr = tmp
27+
}
28+
return prev[n - 1]
29+
}
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3573\. Best Time to Buy and Sell Stock V
2+
3+
Medium
4+
5+
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`.
6+
7+
You are allowed to make at most `k` transactions, where each transaction can be either of the following:
8+
9+
* **Normal transaction**: Buy on day `i`, then sell on a later day `j` where `i < j`. You profit `prices[j] - prices[i]`.
10+
11+
* **Short selling transaction**: Sell on day `i`, then buy back on a later day `j` where `i < j`. You profit `prices[i] - prices[j]`.
12+
13+
14+
**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.
15+
16+
Return the **maximum** total profit you can earn by making **at most** `k` transactions.
17+
18+
**Example 1:**
19+
20+
**Input:** prices = [1,7,9,8,2], k = 2
21+
22+
**Output:** 14
23+
24+
**Explanation:**
25+
26+
We can make $14 of profit through 2 transactions:
27+
28+
* A normal transaction: buy the stock on day 0 for $1 then sell it on day 2 for $9.
29+
* A short selling transaction: sell the stock on day 3 for $8 then buy back on day 4 for $2.
30+
31+
**Example 2:**
32+
33+
**Input:** prices = [12,16,19,19,8,1,19,13,9], k = 3
34+
35+
**Output:** 36
36+
37+
**Explanation:**
38+
39+
We can make $36 of profit through 3 transactions:
40+
41+
* A normal transaction: buy the stock on day 0 for $12 then sell it on day 2 for $19.
42+
* A short selling transaction: sell the stock on day 3 for $19 then buy back on day 4 for $8.
43+
* A normal transaction: buy the stock on day 5 for $1 then sell it on day 6 for $19.
44+
45+
**Constraints:**
46+
47+
* <code>2 <= prices.length <= 10<sup>3</sup></code>
48+
* <code>1 <= prices[i] <= 10<sup>9</sup></code>
49+
* `1 <= k <= prices.length / 2`
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package g3501_3600.s3574_maximize_subarray_gcd_score
2+
3+
// #Hard #Array #Math #Enumeration #Number_Theory
4+
// #2025_06_10_Time_19_ms_(100.00%)_Space_50.12_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun maxGCDScore(nums: IntArray, k: Int): Long {
10+
var mx = 0
11+
for (x in nums) {
12+
mx = max(mx, x)
13+
}
14+
val width = 32 - Integer.numberOfLeadingZeros(mx)
15+
val lowBitPos: Array<MutableList<Int>> = Array<MutableList<Int>>(width) { _ -> ArrayList<Int>() }
16+
val intervals = Array<IntArray>(width + 1) { IntArray(3) }
17+
var size = 0
18+
var ans: Long = 0
19+
for (i in nums.indices) {
20+
val x = nums[i]
21+
val tz = Integer.numberOfTrailingZeros(x)
22+
lowBitPos[tz].add(i)
23+
for (j in 0..<size) {
24+
intervals[j][0] = gcd(intervals[j][0], x)
25+
}
26+
intervals[size][0] = x
27+
intervals[size][1] = i - 1
28+
intervals[size][2] = i
29+
size++
30+
var idx = 1
31+
for (j in 1..<size) {
32+
if (intervals[j][0] != intervals[j - 1][0]) {
33+
intervals[idx][0] = intervals[j][0]
34+
intervals[idx][1] = intervals[j][1]
35+
intervals[idx][2] = intervals[j][2]
36+
idx++
37+
} else {
38+
intervals[idx - 1][2] = intervals[j][2]
39+
}
40+
}
41+
size = idx
42+
for (j in 0..<size) {
43+
val g = intervals[j][0]
44+
val l = intervals[j][1]
45+
val r = intervals[j][2]
46+
ans = max(ans, g.toLong() * (i - l))
47+
val pos = lowBitPos[Integer.numberOfTrailingZeros(g)]
48+
val minL = if (pos.size > k) max(l, pos[pos.size - k - 1]) else l
49+
if (minL < r) {
50+
ans = max(ans, g.toLong() * 2 * (i - minL))
51+
}
52+
}
53+
}
54+
return ans
55+
}
56+
57+
private fun gcd(a: Int, b: Int): Int {
58+
var a = a
59+
var b = b
60+
while (a != 0) {
61+
val tmp = a
62+
a = b % a
63+
b = tmp
64+
}
65+
return b
66+
}
67+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3574\. Maximize Subarray GCD Score
2+
3+
Hard
4+
5+
You are given an array of positive integers `nums` and an integer `k`.
6+
7+
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.
8+
9+
The **score** of a contiguous **subarray** is defined as the **product** of its length and the _greatest common divisor (GCD)_ of all its elements.
10+
11+
Your task is to return the **maximum** **score** that can be achieved by selecting a contiguous subarray from the modified array.
12+
13+
**Note:**
14+
15+
* The **greatest common divisor (GCD)** of an array is the largest integer that evenly divides all the array elements.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,4], k = 1
20+
21+
**Output:** 8
22+
23+
**Explanation:**
24+
25+
* Double `nums[0]` to 4 using one operation. The modified array becomes `[4, 4]`.
26+
* The GCD of the subarray `[4, 4]` is 4, and the length is 2.
27+
* Thus, the maximum possible score is `2 × 4 = 8`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [3,5,7], k = 2
32+
33+
**Output:** 14
34+
35+
**Explanation:**
36+
37+
* Double `nums[2]` to 14 using one operation. The modified array becomes `[3, 5, 14]`.
38+
* The GCD of the subarray `[14]` is 14, and the length is 1.
39+
* Thus, the maximum possible score is `1 × 14 = 14`.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [5,5,5], k = 1
44+
45+
**Output:** 15
46+
47+
**Explanation:**
48+
49+
* The subarray `[5, 5, 5]` has a GCD of 5, and its length is 3.
50+
* Since doubling any element doesn't improve the score, the maximum score is `3 × 5 = 15`.
51+
52+
**Constraints:**
53+
54+
* `1 <= n == nums.length <= 1500`
55+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
56+
* `1 <= k <= n`
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package g3501_3600.s3575_maximum_good_subtree_score
2+
3+
// #Hard #Array #Dynamic_Programming #Depth_First_Search #Tree #Bit_Manipulation #Bitmask
4+
// #2025_06_10_Time_71_ms_(100.00%)_Space_78.07_MB_(0.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private val digits = 10
10+
private val full = 1 shl digits
11+
private val neg = Long.Companion.MIN_VALUE / 4
12+
private val mod = 1e9.toLong() + 7
13+
private lateinit var tree: Array<ArrayList<Int>>
14+
private lateinit var `val`: IntArray
15+
private lateinit var mask: IntArray
16+
private lateinit var isOk: BooleanArray
17+
private var res: Long = 0
18+
19+
fun goodSubtreeSum(vals: IntArray, par: IntArray): Int {
20+
val n = vals.size
21+
`val` = vals
22+
mask = IntArray(n)
23+
isOk = BooleanArray(n)
24+
for (i in 0..<n) {
25+
var m = 0
26+
var v = vals[i]
27+
var valid = true
28+
while (v > 0) {
29+
val d = v % 10
30+
if (((m shr d) and 1) == 1) {
31+
valid = false
32+
break
33+
}
34+
m = m or (1 shl d)
35+
v /= 10
36+
}
37+
mask[i] = m
38+
isOk[i] = valid
39+
}
40+
tree = Array(n) { initialCapacity: Int -> ArrayList(initialCapacity) }
41+
val root = 0
42+
for (i in 1..<n) {
43+
tree[par[i]].add(i)
44+
}
45+
dfs(root)
46+
return (res % mod).toInt()
47+
}
48+
49+
private fun dfs(u: Int): LongArray {
50+
var dp = LongArray(full)
51+
dp.fill(neg)
52+
dp[0] = 0
53+
if (isOk[u]) {
54+
dp[mask[u]] = `val`[u].toLong()
55+
}
56+
for (v in tree[u]) {
57+
val child = dfs(v)
58+
val newDp = dp.copyOf(full)
59+
for (m1 in 0..<full) {
60+
if (dp[m1] < 0) {
61+
continue
62+
}
63+
val remain = full - 1 - m1
64+
var m2 = remain
65+
while (m2 > 0) {
66+
if (child[m2] < 0) {
67+
m2 = (m2 - 1) and remain
68+
continue
69+
}
70+
val newM = m1 or m2
71+
newDp[newM] = max(newDp[newM], dp[m1] + child[m2])
72+
m2 = (m2 - 1) and remain
73+
}
74+
}
75+
dp = newDp
76+
}
77+
var best: Long = 0
78+
for (v in dp) {
79+
best = max(best, v)
80+
}
81+
res = (res + best) % mod
82+
return dp
83+
}
84+
}

0 commit comments

Comments
 (0)