Skip to content

Commit 4aedb54

Browse files
committed
Added tasks 3731-3734
1 parent 63820c4 commit 4aedb54

File tree

12 files changed

+511
-0
lines changed

12 files changed

+511
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3701_3800.s3731_find_missing_elements
2+
3+
// #Easy #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_48.81_MB_(11.11%)
4+
5+
class Solution {
6+
fun findMissingElements(nums: IntArray): MutableList<Int> {
7+
val list: MutableList<Int> = ArrayList()
8+
nums.sort()
9+
for (i in 0..<nums.size - 1) {
10+
if (nums[i + 1] - nums[i] > 1) {
11+
for (j in nums[i] + 1..<nums[i + 1]) {
12+
list.add(j)
13+
}
14+
}
15+
}
16+
return list
17+
}
18+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3731\. Find Missing Elements
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting of **unique** integers.
6+
7+
Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.
8+
9+
The **smallest** and **largest** integers of the original range are still present in `nums`.
10+
11+
Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,4,2,5]
16+
17+
**Output:** [3]
18+
19+
**Explanation:**
20+
21+
The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. Among these, only 3 is missing.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [7,8,6,9]
26+
27+
**Output:** []
28+
29+
**Explanation:**
30+
31+
The smallest integer is 6 and the largest is 9, so the full range is `[6,7,8,9]`. All integers are already present, so no integer is missing.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [5,1]
36+
37+
**Output:** [2,3,4]
38+
39+
**Explanation:**
40+
41+
The smallest integer is 1 and the largest is 5, so the full range should be `[1,2,3,4,5]`. The missing integers are 2, 3, and 4.
42+
43+
**Constraints:**
44+
45+
* `2 <= nums.length <= 100`
46+
* `1 <= nums[i] <= 100`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement
2+
3+
// #Medium #Weekly_Contest_474 #2025_11_03_Time_6_ms_(100.00%)_Space_75.20_MB_(76.92%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun maxProduct(nums: IntArray): Long {
9+
var a: Long = 0
10+
var b: Long = 0
11+
for (x in nums) {
12+
val ax = abs(x).toLong()
13+
if (ax >= a) {
14+
b = a
15+
a = ax
16+
} else if (ax > b) {
17+
b = ax
18+
}
19+
}
20+
return 100000L * a * b
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3732\. Maximum Product of Three Elements After One Replacement
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
You **must** replace **exactly one** element in the array with **any** integer value in the range <code>[-10<sup>5</sup>, 10<sup>5</sup>]</code> (inclusive).
8+
9+
After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array.
10+
11+
Return an integer denoting the **maximum product** achievable.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [-5,7,0]
16+
17+
**Output:** 3500000
18+
19+
**Explanation:**
20+
21+
Replacing 0 with -10<sup>5</sup> gives the array <code>[-5, 7, -10<sup>5</sup>]</code>, which has a product <code>(-5) * 7 * (-10<sup>5</sup>) = 3500000</code>. The maximum product is 3500000.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [-4,-2,-1,-3]
26+
27+
**Output:** 1200000
28+
29+
**Explanation:**
30+
31+
Two ways to achieve the maximum product include:
32+
33+
* `[-4, -2, -3]` → replace -2 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
34+
* `[-4, -1, -3]` → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
35+
36+
The maximum product is 1200000.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [0,10,0]
41+
42+
**Output:** 0
43+
44+
**Explanation:**
45+
46+
There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0.
47+
48+
**Constraints:**
49+
50+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
51+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3701_3800.s3733_minimum_time_to_complete_all_deliveries
2+
3+
// #Medium #Weekly_Contest_474 #2025_11_03_Time_2_ms_(100.00%)_Space_42.94_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
private fun func(mid: Long, d: IntArray, r: IntArray): Boolean {
9+
val lcm = r[0].toLong() * r[1] / gcd(r[0].toLong(), r[1].toLong())
10+
val a1 = mid / r[0]
11+
val a2 = mid / r[1]
12+
val a3 = mid / lcm
13+
val b = mid - a1 - a2 + a3
14+
val o1 = a2 - a3
15+
val o2 = a1 - a3
16+
val d0 = max(d[0] - o1, 0)
17+
val d1 = max(d[1] - o2, 0)
18+
return b >= d0 + d1
19+
}
20+
21+
private fun gcd(a: Long, b: Long): Long {
22+
if (b == 0L) {
23+
return a
24+
}
25+
return gcd(b, a % b)
26+
}
27+
28+
fun minimumTime(d: IntArray, r: IntArray): Long {
29+
var lo: Long = 0
30+
var hi = 1e12.toLong()
31+
var ans = Long.MAX_VALUE
32+
while (lo <= hi) {
33+
val mid = (lo + hi) / 2
34+
if (func(mid, d, r)) {
35+
ans = mid
36+
hi = mid - 1
37+
} else {
38+
lo = mid + 1
39+
}
40+
}
41+
return ans
42+
}
43+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3733\. Minimum Time to Complete All Deliveries
2+
3+
Medium
4+
5+
You are given two integer arrays of size 2: <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code> and <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>.
6+
7+
Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete <code>d<sub>i</sub></code> deliveries.
8+
9+
Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour.
10+
11+
Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone `i` must recharge every <code>r<sub>i</sub></code> hours (i.e. at hours that are multiples of <code>r<sub>i</sub></code>).
12+
13+
Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries.
14+
15+
**Example 1:**
16+
17+
**Input:** d = [3,1], r = [2,3]
18+
19+
**Output:** 5
20+
21+
**Explanation:**
22+
23+
* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).
24+
* The second drone delivers at hour 2 (recharges at hour 3).
25+
26+
**Example 2:**
27+
28+
**Input:** d = [1,3], r = [2,2]
29+
30+
**Output:** 7
31+
32+
**Explanation:**
33+
34+
* The first drone delivers at hour 3 (recharges at hours 2, 4, 6).
35+
* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).
36+
37+
**Example 3:**
38+
39+
**Input:** d = [2,1], r = [3,4]
40+
41+
**Output:** 3
42+
43+
**Explanation:**
44+
45+
* The first drone delivers at hours 1, 2 (recharges at hour 3).
46+
* The second drone delivers at hour 3.
47+
48+
**Constraints:**
49+
50+
* <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code>
51+
* <code>1 <= d<sub>i</sub> <= 10<sup>9</sup></code>
52+
* <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>
53+
* <code>2 <= r<sub>i</sub> <= 3 * 10<sup>4</sup></code>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target
2+
3+
// #Hard #Weekly_Contest_474 #2025_11_03_Time_12_ms_(100.00%)_Space_47.24_MB_(100.00%)
4+
5+
class Solution {
6+
fun func(i: Int, target: String, ans: CharArray, l: Int, r: Int, freq: IntArray, end: Boolean): Boolean {
7+
if (l > r) {
8+
return String(ans).compareTo(target) > 0
9+
}
10+
if (l == r) {
11+
var left = '#'
12+
for (k in 0..25) {
13+
if (freq[k] > 0) {
14+
left = (k + 'a'.code).toChar()
15+
}
16+
}
17+
freq[left.code - 'a'.code]--
18+
ans[l] = left
19+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
20+
return true
21+
}
22+
freq[left.code - 'a'.code]++
23+
ans[l] = '#'
24+
return false
25+
}
26+
if (end) {
27+
for (j in 0..25) {
28+
if (freq[j] > 1) {
29+
freq[j] -= 2
30+
ans[r] = (j + 'a'.code).toChar()
31+
ans[l] = ans[r]
32+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
33+
return true
34+
}
35+
ans[r] = '#'
36+
ans[l] = ans[r]
37+
freq[j] += 2
38+
}
39+
}
40+
return false
41+
}
42+
val curr = target.get(i)
43+
var next = '1'
44+
for (k in target.get(i).code - 'a'.code + 1..25) {
45+
if (freq[k] > 1) {
46+
next = (k + 'a'.code).toChar()
47+
break
48+
}
49+
}
50+
if (freq[curr.code - 'a'.code] > 1) {
51+
ans[r] = curr
52+
ans[l] = ans[r]
53+
freq[curr.code - 'a'.code] -= 2
54+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
55+
return true
56+
}
57+
freq[curr.code - 'a'.code] += 2
58+
}
59+
if (next != '1') {
60+
ans[r] = next
61+
ans[l] = ans[r]
62+
freq[next.code - 'a'.code] -= 2
63+
if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) {
64+
return true
65+
}
66+
freq[next.code - 'a'.code] += 2
67+
}
68+
ans[r] = '#'
69+
ans[l] = ans[r]
70+
return false
71+
}
72+
73+
fun lexPalindromicPermutation(s: String, target: String): String {
74+
val freq = IntArray(26)
75+
for (i in 0..<s.length) {
76+
freq[s.get(i).code - 'a'.code]++
77+
}
78+
var oddc = 0
79+
for (i in 0..25) {
80+
if (freq[i] % 2 == 1) {
81+
oddc++
82+
}
83+
}
84+
if (oddc > 1) {
85+
return ""
86+
}
87+
val ans = CharArray(s.length)
88+
ans.fill('#')
89+
if (func(0, target, ans, 0, s.length - 1, freq, false)) {
90+
return String(ans)
91+
}
92+
return ""
93+
}
94+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3734\. Lexicographically Smallest Palindromic Permutation Greater Than Target
2+
3+
Hard
4+
5+
You are given two strings `s` and `target`, each of length `n`, consisting of lowercase English letters.
6+
7+
Return the **lexicographically smallest string** that is **both** a **palindromic permutation** of `s` and **strictly** greater than `target`. If no such permutation exists, return an empty string.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "baba", target = "abba"
12+
13+
**Output:** "baab"
14+
15+
**Explanation:**
16+
17+
* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`.
18+
* The lexicographically smallest permutation that is strictly greater than `target` is `"baab"`.
19+
20+
**Example 2:**
21+
22+
**Input:** s = "baba", target = "bbaa"
23+
24+
**Output:** ""
25+
26+
**Explanation:**
27+
28+
* The palindromic permutations of `s` (in lexicographical order) are `"abba"` and `"baab"`.
29+
* None of them is lexicographically strictly greater than `target`. Therefore, the answer is `""`.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "abc", target = "abb"
34+
35+
**Output:** ""
36+
37+
**Explanation:**
38+
39+
`s` has no palindromic permutations. Therefore, the answer is `""`.
40+
41+
**Example 4:**
42+
43+
**Input:** s = "aac", target = "abb"
44+
45+
**Output:** "aca"
46+
47+
**Explanation:**
48+
49+
* The only palindromic permutation of `s` is `"aca"`.
50+
* `"aca"` is strictly greater than `target`. Therefore, the answer is `"aca"`.
51+
52+
**Constraints:**
53+
54+
* `1 <= n == s.length == target.length <= 300`
55+
* `s` and `target` consist of only lowercase English letters.

0 commit comments

Comments
 (0)