Skip to content

Commit 79f5407

Browse files
committed
Added tasks 3731-3734
1 parent 187520f commit 79f5407

File tree

12 files changed

+478
-0
lines changed

12 files changed

+478
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3701_3800.s3731_find_missing_elements;
2+
3+
// #Easy #Weekly_Contest_474 #2025_11_02_Time_6_ms_(100.00%)_Space_47.09_MB_(_%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public List<Integer> findMissingElements(int[] nums) {
11+
List<Integer> list = new ArrayList<>();
12+
Arrays.sort(nums);
13+
for (int i = 0; i < nums.length - 1; i++) {
14+
if (nums[i + 1] - nums[i] > 1) {
15+
for (int j = nums[i] + 1; j < nums[i + 1]; j++) {
16+
list.add(j);
17+
}
18+
}
19+
}
20+
return list;
21+
}
22+
}
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement;
2+
3+
// #Medium #Weekly_Contest_474 #2025_11_02_Time_4_ms_(100.00%)_Space_96.67_MB_(_%)
4+
5+
public class Solution {
6+
public long maxProduct(int[] nums) {
7+
long a = 0;
8+
long b = 0;
9+
for (int x : nums) {
10+
long ax = Math.abs(x);
11+
if (ax >= a) {
12+
b = a;
13+
a = ax;
14+
} else if (ax > b) {
15+
b = ax;
16+
}
17+
}
18+
return 100000L * a * b;
19+
}
20+
}
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3701_3800.s3733_minimum_time_to_complete_all_deliveries;
2+
3+
// #Medium #Weekly_Contest_474 #2025_11_02_Time_2_ms_(100.00%)_Space_44.04_MB_(_%)
4+
5+
public class Solution {
6+
private boolean func(long mid, int[] d, int[] r) {
7+
long lcm = (long) r[0] * r[1] / gcd(r[0], r[1]);
8+
long a1 = mid / r[0];
9+
long a2 = mid / r[1];
10+
long a3 = mid / lcm;
11+
long b = mid - a1 - a2 + a3;
12+
long o1 = a2 - a3;
13+
long o2 = a1 - a3;
14+
long d0 = Math.max(d[0] - o1, 0);
15+
long d1 = Math.max(d[1] - o2, 0);
16+
return b >= d0 + d1;
17+
}
18+
19+
private long gcd(long a, long b) {
20+
if (b == 0) {
21+
return a;
22+
}
23+
return gcd(b, a % b);
24+
}
25+
26+
public long minimumTime(int[] d, int[] r) {
27+
long lo = 0;
28+
long hi = (long) 1e12;
29+
long ans = Long.MAX_VALUE;
30+
while (lo <= hi) {
31+
long mid = (lo + hi) / 2;
32+
if (func(mid, d, r)) {
33+
ans = mid;
34+
hi = mid - 1;
35+
} else {
36+
lo = mid + 1;
37+
}
38+
}
39+
return ans;
40+
}
41+
}
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: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target;
2+
3+
// #Hard #Weekly_Contest_474 #2025_11_02_Time_3_ms_(50.00%)_Space_46.55_MB_(_%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public boolean func(
9+
int i, String target, char[] ans, int l, int r, int[] freq, boolean end) {
10+
if (l > r) {
11+
return new String(ans).compareTo(target) > 0;
12+
}
13+
if (l == r) {
14+
char left = '#';
15+
for (int k = 0; k < 26; k++) {
16+
if (freq[k] > 0) {
17+
left = (char) (k + 'a');
18+
}
19+
}
20+
freq[left - 'a']--;
21+
ans[l] = left;
22+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
23+
return true;
24+
}
25+
freq[left - 'a']++;
26+
ans[l] = '#';
27+
return false;
28+
}
29+
if (end) {
30+
for (int j = 0; j < 26; j++) {
31+
if (freq[j] > 1) {
32+
freq[j] -= 2;
33+
ans[l] = ans[r] = (char) (j + 'a');
34+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
35+
return true;
36+
}
37+
ans[l] = ans[r] = '#';
38+
freq[j] += 2;
39+
}
40+
}
41+
return false;
42+
}
43+
char curr = target.charAt(i);
44+
char next = '1';
45+
for (int k = target.charAt(i) - 'a' + 1; k < 26; k++) {
46+
if (freq[k] > 1) {
47+
next = (char) (k + 'a');
48+
break;
49+
}
50+
}
51+
if (freq[curr - 'a'] > 1) {
52+
ans[l] = ans[r] = curr;
53+
freq[curr - 'a'] -= 2;
54+
if (func(i + 1, target, ans, l + 1, r - 1, freq, end)) {
55+
return true;
56+
}
57+
freq[curr - 'a'] += 2;
58+
}
59+
if (next != '1') {
60+
ans[l] = ans[r] = next;
61+
freq[next - 'a'] -= 2;
62+
if (func(i + 1, target, ans, l + 1, r - 1, freq, true)) {
63+
return true;
64+
}
65+
freq[next - 'a'] += 2;
66+
}
67+
ans[l] = ans[r] = '#';
68+
return false;
69+
}
70+
71+
public String lexPalindromicPermutation(String s, String target) {
72+
int[] freq = new int[26];
73+
for (int i = 0; i < s.length(); i++) {
74+
freq[s.charAt(i) - 'a']++;
75+
}
76+
int oddc = 0;
77+
for (int i = 0; i < 26; i++) {
78+
if (freq[i] % 2 == 1) {
79+
oddc++;
80+
}
81+
}
82+
if (oddc > 1) {
83+
return "";
84+
}
85+
char[] ans = new char[s.length()];
86+
Arrays.fill(ans, '#');
87+
if (func(0, target, ans, 0, s.length() - 1, freq, false)) {
88+
return new String(ans);
89+
}
90+
return "";
91+
}
92+
}
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)