Skip to content

Commit 436306c

Browse files
committed
Added tasks 3731-3734
1 parent ce4ee82 commit 436306c

File tree

5 files changed

+416
-0
lines changed
  • src/main/kotlin/g3701_3800
    • s3731_find_missing_elements
    • s3732_maximum_product_of_three_elements_after_one_replacement
    • s3733_minimum_time_to_complete_all_deliveries
    • s3734_lexicographically_smallest_palindromic_permutation_greater_than_target

5 files changed

+416
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|---------
2091+
| 3734 |[Lexicographically Smallest Palindromic Permutation Greater Than Target](src/main/kotlin/g3701_3800/s3734_lexicographically_smallest_palindromic_permutation_greater_than_target)| Hard | String, Two_Pointers, Enumeration, Weekly_Contest_474 | 4 | 100.00
2092+
| 3733 |[Minimum Time to Complete All Deliveries](src/main/kotlin/g3701_3800/s3733_minimum_time_to_complete_all_deliveries)| Medium | Math, Binary_Search, Weekly_Contest_474 | 2 | 100.00
2093+
| 3732 |[Maximum Product of Three Elements After One Replacement](src/main/kotlin/g3701_3800/s3732_maximum_product_of_three_elements_after_one_replacement)| Medium | Array, Math, Sorting, Greedy, Weekly_Contest_474 | 6 | 88.00
2094+
| 3731 |[Find Missing Elements](src/main/kotlin/g3701_3800/s3731_find_missing_elements)| Easy | Array, Hash_Table, Sorting, Weekly_Contest_474 | 2 | 100.00
20912095
| 3729 |[Count Distinct Subarrays Divisible by K in Sorted Array](src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array)| Hard | Array, Hash_Table, Prefix_Sum, Weekly_Contest_473 | 61 | 100.00
20922096
| 3728 |[Stable Subarrays With Equal Boundary and Interior Sum](src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum)| Medium | Array, Hash_Table, Prefix_Sum, Weekly_Contest_473 | 109 | 100.00
20932097
| 3727 |[Maximum Alternating Sum of Squares](src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares)| Medium | Array, Sorting, Greedy, Weekly_Contest_473 | 9 | 100.00
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3731\. Find Missing Elements
5+
6+
Easy
7+
8+
You are given an integer array `nums` consisting of **unique** integers.
9+
10+
Originally, `nums` contained **every integer** within a certain range. However, some integers might have gone **missing** from the array.
11+
12+
The **smallest** and **largest** integers of the original range are still present in `nums`.
13+
14+
Return a **sorted** list of all the missing integers in this range. If no integers are missing, return an **empty** list.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,4,2,5]
19+
20+
**Output:** [3]
21+
22+
**Explanation:**
23+
24+
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.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [7,8,6,9]
29+
30+
**Output:** []
31+
32+
**Explanation:**
33+
34+
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.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [5,1]
39+
40+
**Output:** [2,3,4]
41+
42+
**Explanation:**
43+
44+
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.
45+
46+
**Constraints:**
47+
48+
* `2 <= nums.length <= 100`
49+
* `1 <= nums[i] <= 100`
50+
51+
## Solution
52+
53+
```kotlin
54+
class Solution {
55+
fun findMissingElements(nums: IntArray): List<Int> {
56+
var maxi = 0
57+
var mini = 101
58+
val list: MutableList<Int> = ArrayList()
59+
val array = BooleanArray(101)
60+
for (num in nums) {
61+
array[num] = true
62+
if (maxi < num) {
63+
maxi = num
64+
}
65+
if (mini > num) {
66+
mini = num
67+
}
68+
}
69+
for (index in mini + 1..<maxi) {
70+
if (array[index]) {
71+
continue
72+
}
73+
list.add(index)
74+
}
75+
return list
76+
}
77+
}
78+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3732\. Maximum Product of Three Elements After One Replacement
5+
6+
Medium
7+
8+
You are given an integer array `nums`.
9+
10+
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).
11+
12+
After performing this single replacement, determine the **maximum possible product** of **any three** elements at **distinct indices** from the modified array.
13+
14+
Return an integer denoting the **maximum product** achievable.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [-5,7,0]
19+
20+
**Output:** 3500000
21+
22+
**Explanation:**
23+
24+
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.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [-4,-2,-1,-3]
29+
30+
**Output:** 1200000
31+
32+
**Explanation:**
33+
34+
Two ways to achieve the maximum product include:
35+
36+
* `[-4, -2, -3]` → replace -2 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
37+
* `[-4, -1, -3]` → replace -1 with 10<sup>5</sup> → product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.
38+
39+
The maximum product is 1200000.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [0,10,0]
44+
45+
**Output:** 0
46+
47+
**Explanation:**
48+
49+
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.
50+
51+
**Constraints:**
52+
53+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
54+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
55+
56+
## Solution
57+
58+
```kotlin
59+
import kotlin.math.abs
60+
61+
class Solution {
62+
fun maxProduct(nums: IntArray): Long {
63+
var a: Long = 0
64+
var b: Long = 0
65+
for (x in nums) {
66+
val ax = abs(x).toLong()
67+
if (ax >= a) {
68+
b = a
69+
a = ax
70+
} else if (ax > b) {
71+
b = ax
72+
}
73+
}
74+
return 100000L * a * b
75+
}
76+
}
77+
```
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3733\. Minimum Time to Complete All Deliveries
5+
6+
Medium
7+
8+
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>.
9+
10+
Two delivery drones are tasked with completing a specific number of deliveries. Drone `i` must complete <code>d<sub>i</sub></code> deliveries.
11+
12+
Each delivery takes **exactly** one hour and **only one** drone can make a delivery at any given hour.
13+
14+
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>).
15+
16+
Return an integer denoting the **minimum** total time (in hours) required to complete all deliveries.
17+
18+
**Example 1:**
19+
20+
**Input:** d = [3,1], r = [2,3]
21+
22+
**Output:** 5
23+
24+
**Explanation:**
25+
26+
* The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).
27+
* The second drone delivers at hour 2 (recharges at hour 3).
28+
29+
**Example 2:**
30+
31+
**Input:** d = [1,3], r = [2,2]
32+
33+
**Output:** 7
34+
35+
**Explanation:**
36+
37+
* The first drone delivers at hour 3 (recharges at hours 2, 4, 6).
38+
* The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).
39+
40+
**Example 3:**
41+
42+
**Input:** d = [2,1], r = [3,4]
43+
44+
**Output:** 3
45+
46+
**Explanation:**
47+
48+
* The first drone delivers at hours 1, 2 (recharges at hour 3).
49+
* The second drone delivers at hour 3.
50+
51+
**Constraints:**
52+
53+
* <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code>
54+
* <code>1 <= d<sub>i</sub> <= 10<sup>9</sup></code>
55+
* <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>
56+
* <code>2 <= r<sub>i</sub> <= 3 * 10<sup>4</sup></code>
57+
58+
## Solution
59+
60+
```kotlin
61+
import kotlin.math.max
62+
63+
class Solution {
64+
private fun pos(k: Long, n1: Long, n2: Long, p1: Int, p2: Int, lVal: Long): Boolean {
65+
val kP1 = k / p1
66+
val kP2 = k / p2
67+
val kL = k / lVal
68+
val s1 = kP2 - kL
69+
val s2 = kP1 - kL
70+
val sB = k - kP1 - kP2 + kL
71+
val w1 = max(0L, n1 - s1)
72+
val w2 = max(0L, n2 - s2)
73+
return (w1 + w2) <= sB
74+
}
75+
76+
private fun findGcd(a: Long, b: Long): Long {
77+
if (b == 0L) {
78+
return a
79+
}
80+
return findGcd(b, a % b)
81+
}
82+
83+
fun minimumTime(d: IntArray, r: IntArray): Long {
84+
val n1 = d[0].toLong()
85+
val n2 = d[1].toLong()
86+
val p1 = r[0]
87+
val p2 = r[1]
88+
val g = findGcd(p1.toLong(), p2.toLong())
89+
val l = p1.toLong() * p2 / g
90+
var lo = n1 + n2
91+
var hi = (n1 + n2) * max(p1, p2)
92+
var res = hi
93+
while (lo <= hi) {
94+
val mid = lo + (hi - lo) / 2
95+
if (pos(mid, n1, n2, p1, p2, l)) {
96+
res = mid
97+
hi = mid - 1
98+
} else {
99+
lo = mid + 1
100+
}
101+
}
102+
return res
103+
}
104+
}
105+
```

0 commit comments

Comments
 (0)