Skip to content

Commit 27b4fb1

Browse files
authored
Added tasks 3627-3630
1 parent ebb28c3 commit 27b4fb1

File tree

12 files changed

+528
-0
lines changed

12 files changed

+528
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3601_3700.s3627_maximum_median_sum_of_subsequences_of_size_3
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_46_ms_(91.67%)_Space_133.87_MB_(16.67%)
4+
5+
class Solution {
6+
fun maximumMedianSum(nums: IntArray): Long {
7+
nums.sort()
8+
var i = 0
9+
var j = nums.size
10+
var sum = 0L
11+
while (i < j) {
12+
sum += nums[j - 2]
13+
j = j - 2
14+
i++
15+
}
16+
return sum
17+
}
18+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3627\. Maximum Median Sum of Subsequences of Size 3
2+
3+
Medium
4+
5+
You are given an integer array `nums` with a length divisible by 3.
6+
7+
You want to make the array empty in steps. In each step, you can select any three elements from the array, compute their **median**, and remove the selected elements from the array.
8+
9+
The **median** of an odd-length sequence is defined as the middle element of the sequence when it is sorted in non-decreasing order.
10+
11+
Return the **maximum** possible sum of the medians computed from the selected elements.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,1,3,2,1,3]
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
* In the first step, select elements at indices 2, 4, and 5, which have a median 3. After removing these elements, `nums` becomes `[2, 1, 2]`.
22+
* In the second step, select elements at indices 0, 1, and 2, which have a median 2. After removing these elements, `nums` becomes empty.
23+
24+
Hence, the sum of the medians is `3 + 2 = 5`.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1,1,10,10,10,10]
29+
30+
**Output:** 20
31+
32+
**Explanation:**
33+
34+
* In the first step, select elements at indices 0, 2, and 3, which have a median 10. After removing these elements, `nums` becomes `[1, 10, 10]`.
35+
* In the second step, select elements at indices 0, 1, and 2, which have a median 10. After removing these elements, `nums` becomes empty.
36+
37+
Hence, the sum of the medians is `10 + 10 = 20`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 5 * 10<sup>5</sup></code>
42+
* `nums.length % 3 == 0`
43+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3601_3700.s3628_maximum_number_of_subsequences_after_one_inserting
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_13_ms_(100.00%)_Space_48.00_MB_(75.00%)
4+
5+
class Solution {
6+
fun numOfSubsequences(s: String): Long {
7+
var tc: Long = 0
8+
val chs = s.toCharArray()
9+
for (c in chs) {
10+
tc += (if (c == 'T') 1 else 0).toLong()
11+
}
12+
var ls: Long = 0
13+
var cs: Long = 0
14+
var lcf: Long = 0
15+
var ctf: Long = 0
16+
var lct: Long = 0
17+
var ocg: Long = 0
18+
var tp: Long = 0
19+
for (curr in chs) {
20+
val rt = tc - tp
21+
val cg = ls * rt
22+
ocg = if (cg > ocg) cg else ocg
23+
if (curr == 'L') {
24+
ls++
25+
} else {
26+
if (curr == 'C') {
27+
cs++
28+
lcf += ls
29+
} else {
30+
if (curr == 'T') {
31+
lct += lcf
32+
ctf += cs
33+
tp++
34+
}
35+
}
36+
}
37+
}
38+
val fcg = ls * (tc - tp)
39+
ocg = if (fcg > ocg) fcg else ocg
40+
var maxi: Long = 0
41+
val bo = longArrayOf(lcf, ctf, ocg)
42+
for (op in bo) {
43+
maxi = if (op > maxi) op else maxi
44+
}
45+
return lct + maxi
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3628\. Maximum Number of Subsequences After One Inserting
2+
3+
Medium
4+
5+
You are given a string `s` consisting of uppercase English letters.
6+
7+
You are allowed to insert **at most one** uppercase English letter at **any** position (including the beginning or end) of the string.
8+
9+
Return the **maximum** number of `"LCT"` subsequences that can be formed in the resulting string after **at most one insertion**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "LMCT"
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
We can insert a `"L"` at the beginning of the string s to make `"LLMCT"`, which has 2 subsequences, at indices [0, 3, 4] and [1, 3, 4].
20+
21+
**Example 2:**
22+
23+
**Input:** s = "LCCT"
24+
25+
**Output:** 4
26+
27+
**Explanation:**
28+
29+
We can insert a `"L"` at the beginning of the string s to make `"LLCCT"`, which has 4 subsequences, at indices [0, 2, 4], [0, 3, 4], [1, 2, 4] and [1, 3, 4].
30+
31+
**Example 3:**
32+
33+
**Input:** s = "L"
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
Since it is not possible to obtain the subsequence `"LCT"` by inserting a single letter, the result is 0.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= s.length <= 10<sup>5</sup></code>
44+
* `s` consists of uppercase English letters.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package g3601_3700.s3629_minimum_jumps_to_reach_end_via_prime_teleportation
2+
3+
// #Medium #Weekly_Contest_460 #2025_07_27_Time_406_ms_(100.00%)_Space_153.64_MB_(100.00%)
4+
5+
import java.util.ArrayDeque
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun minJumps(nums: IntArray): Int {
10+
val n = nums.size
11+
if (n == 1) {
12+
return 0
13+
}
14+
var maxVal = 0
15+
for (v in nums) {
16+
maxVal = max(maxVal, v)
17+
}
18+
val isPrime = sieve(maxVal)
19+
val posOfValue: Array<ArrayList<Int>> = Array<ArrayList<Int>>(maxVal + 1) { ArrayList<Int>() }
20+
for (i in 0..<n) {
21+
val v = nums[i]
22+
posOfValue[v].add(i)
23+
}
24+
val primeProcessed = BooleanArray(maxVal + 1)
25+
val dist = IntArray(n)
26+
dist.fill(-1)
27+
val q = ArrayDeque<Int>()
28+
q.add(0)
29+
dist[0] = 0
30+
while (q.isNotEmpty()) {
31+
val i: Int = q.poll()!!
32+
val d = dist[i]
33+
if (i == n - 1) {
34+
return d
35+
}
36+
if (i + 1 < n && dist[i + 1] == -1) {
37+
dist[i + 1] = d + 1
38+
q.add(i + 1)
39+
}
40+
if (i - 1 >= 0 && dist[i - 1] == -1) {
41+
dist[i - 1] = d + 1
42+
q.add(i - 1)
43+
}
44+
val v = nums[i]
45+
if (v <= maxVal && isPrime[v] && !primeProcessed[v]) {
46+
var mult = v
47+
while (mult <= maxVal) {
48+
val list = posOfValue[mult]
49+
for (idx in list) {
50+
if (dist[idx] == -1) {
51+
dist[idx] = d + 1
52+
q.add(idx)
53+
}
54+
}
55+
mult += v
56+
}
57+
primeProcessed[v] = true
58+
}
59+
}
60+
return -1
61+
}
62+
63+
private fun sieve(n: Int): BooleanArray {
64+
val prime = BooleanArray(n + 1)
65+
if (n >= 2) {
66+
prime.fill(true)
67+
}
68+
if (n >= 0) {
69+
prime[0] = false
70+
}
71+
if (n >= 1) {
72+
prime[1] = false
73+
}
74+
var i = 2
75+
while (i.toLong() * i <= n) {
76+
if (prime[i]) {
77+
var j = i * i
78+
while (j <= n) {
79+
prime[j] = false
80+
j += i
81+
}
82+
}
83+
i++
84+
}
85+
return prime
86+
}
87+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3629\. Minimum Jumps to Reach End via Prime Teleportation
2+
3+
Medium
4+
5+
You are given an integer array `nums` of length `n`.
6+
7+
You start at index 0, and your goal is to reach index `n - 1`.
8+
9+
From any index `i`, you may perform one of the following operations:
10+
11+
* **Adjacent Step**: Jump to index `i + 1` or `i - 1`, if the index is within bounds.
12+
* **Prime Teleportation**: If `nums[i]` is a prime number `p`, you may instantly jump to any index `j != i` such that `nums[j] % p == 0`.
13+
14+
Return the **minimum** number of jumps required to reach index `n - 1`.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2,4,6]
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
One optimal sequence of jumps is:
25+
26+
* Start at index `i = 0`. Take an adjacent step to index 1.
27+
* At index `i = 1`, `nums[1] = 2` is a prime number. Therefore, we teleport to index `i = 3` as `nums[3] = 6` is divisible by 2.
28+
29+
Thus, the answer is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,3,4,7,9]
34+
35+
**Output:** 2
36+
37+
**Explanation:**
38+
39+
One optimal sequence of jumps is:
40+
41+
* Start at index `i = 0`. Take an adjacent step to index `i = 1`.
42+
* At index `i = 1`, `nums[1] = 3` is a prime number. Therefore, we teleport to index `i = 4` since `nums[4] = 9` is divisible by 3.
43+
44+
Thus, the answer is 2.
45+
46+
**Example 3:**
47+
48+
**Input:** nums = [4,6,5,8]
49+
50+
**Output:** 3
51+
52+
**Explanation:**
53+
54+
* Since no teleportation is possible, we move through `0 → 1 → 2 → 3`. Thus, the answer is 3.
55+
56+
**Constraints:**
57+
58+
* <code>1 <= n == nums.length <= 10<sup>5</sup></code>
59+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3601_3700.s3630_partition_array_for_maximum_xor_and_and
2+
3+
// #Hard #Array #Math #Greedy #Enumeration #Weekly_Contest_460
4+
// #2025_08_03_Time_57_ms_(100.00%)_Space_51.81_MB_(100.00%)
5+
6+
class Solution {
7+
fun maximizeXorAndXor(nums: IntArray): Long {
8+
val n = nums.size
9+
val full = 1 shl n
10+
val xorMask = IntArray(full)
11+
val andMask = IntArray(full)
12+
val orMask = IntArray(full)
13+
for (mask in 1..<full) {
14+
val lb = mask and -mask
15+
val i = Integer.numberOfTrailingZeros(lb)
16+
val prev = mask xor lb
17+
xorMask[mask] = xorMask[prev] xor nums[i]
18+
andMask[mask] = if (prev == 0) nums[i] else andMask[prev] and nums[i]
19+
orMask[mask] = orMask[prev] or nums[i]
20+
}
21+
var best: Long = 0
22+
val all = full - 1
23+
for (b in 0..<full) {
24+
val andB = andMask[b].toLong()
25+
val rest = all xor b
26+
if (andB + 2L * orMask[rest] <= best) {
27+
continue
28+
}
29+
var a = rest
30+
while (true) {
31+
val c = rest xor a
32+
val sum = xorMask[a] + andB + xorMask[c]
33+
if (sum > best) {
34+
best = sum
35+
}
36+
if (a == 0) {
37+
break
38+
}
39+
a = (a - 1) and rest
40+
}
41+
}
42+
return best
43+
}
44+
}

0 commit comments

Comments
 (0)