Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 90abc44

Browse files
authored
Added tasks 3731-3734
1 parent feb3661 commit 90abc44

File tree

12 files changed

+774
-0
lines changed

12 files changed

+774
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3701_3800.s3731_find_missing_elements;
2+
3+
// #Easy #Array #Hash_Table #Sorting #Weekly_Contest_474
4+
// #2025_11_05_Time_2_ms_(94.89%)_Space_46.54_MB_(95.16%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public List<Integer> findMissingElements(int[] nums) {
11+
int maxi = 0;
12+
int mini = 101;
13+
List<Integer> list = new ArrayList<>();
14+
boolean[] array = new boolean[101];
15+
for (int num : nums) {
16+
array[num] = true;
17+
if (maxi < num) {
18+
maxi = num;
19+
}
20+
if (mini > num) {
21+
mini = num;
22+
}
23+
}
24+
for (int index = mini + 1; index < maxi; index++) {
25+
if (array[index]) {
26+
continue;
27+
}
28+
list.add(index);
29+
}
30+
return list;
31+
}
32+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3701_3800.s3732_maximum_product_of_three_elements_after_one_replacement;
2+
3+
// #Medium #Array #Math #Sorting #Greedy #Weekly_Contest_474
4+
// #2025_11_05_Time_4_ms_(95.32%)_Space_97.32_MB_(28.84%)
5+
6+
public class Solution {
7+
public long maxProduct(int[] nums) {
8+
long a = 0;
9+
long b = 0;
10+
for (int x : nums) {
11+
long ax = Math.abs(x);
12+
if (ax >= a) {
13+
b = a;
14+
a = ax;
15+
} else if (ax > b) {
16+
b = ax;
17+
}
18+
}
19+
return 100000L * a * b;
20+
}
21+
}
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g3701_3800.s3733_minimum_time_to_complete_all_deliveries;
2+
3+
// #Medium #Math #Binary_Search #Weekly_Contest_474
4+
// #2025_11_05_Time_1_ms_(100.00%)_Space_44.16_MB_(55.61%)
5+
6+
public class Solution {
7+
private boolean pos(long k, long n1, long n2, int p1, int p2, long lVal) {
8+
long kP1 = k / p1;
9+
long kP2 = k / p2;
10+
long kL = k / lVal;
11+
long s1 = kP2 - kL;
12+
long s2 = kP1 - kL;
13+
long sB = k - kP1 - kP2 + kL;
14+
long w1 = Math.max(0L, n1 - s1);
15+
long w2 = Math.max(0L, n2 - s2);
16+
return (w1 + w2) <= sB;
17+
}
18+
19+
private long findGcd(long a, long b) {
20+
if (b == 0) {
21+
return a;
22+
}
23+
return findGcd(b, a % b);
24+
}
25+
26+
public long minimumTime(int[] d, int[] r) {
27+
long n1 = d[0];
28+
long n2 = d[1];
29+
int p1 = r[0];
30+
int p2 = r[1];
31+
long g = findGcd(p1, p2);
32+
long l = (long) p1 * p2 / g;
33+
long lo = n1 + n2;
34+
long hi = (n1 + n2) * Math.max(p1, p2);
35+
long res = hi;
36+
while (lo <= hi) {
37+
long mid = lo + (hi - lo) / 2;
38+
if (pos(mid, n1, n2, p1, p2, l)) {
39+
res = mid;
40+
hi = mid - 1;
41+
} else {
42+
lo = mid + 1;
43+
}
44+
}
45+
return res;
46+
}
47+
}
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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g3701_3800.s3734_lexicographically_smallest_palindromic_permutation_greater_than_target;
2+
3+
// #Hard #String #Two_Pointers #Enumeration #Weekly_Contest_474
4+
// #2025_11_05_Time_2_ms_(100.00%)_Space_46.34_MB_(84.73%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
boolean func(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+
}
66+
ans[l] = ans[r] = '#';
67+
return false;
68+
}
69+
70+
public String lexPalindromicPermutation(String s, String target) {
71+
int[] freq = new int[26];
72+
for (int i = 0; i < s.length(); i++) {
73+
freq[s.charAt(i) - 'a']++;
74+
}
75+
int oddc = 0;
76+
for (int i = 0; i < 26; i++) {
77+
if (freq[i] % 2 == 1) {
78+
oddc++;
79+
}
80+
}
81+
if (oddc > 1) {
82+
return "";
83+
}
84+
char[] ans = new char[s.length()];
85+
Arrays.fill(ans, '#');
86+
if (func(0, target, ans, 0, s.length() - 1, freq, false)) {
87+
return new String(ans);
88+
}
89+
return "";
90+
}
91+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /