Skip to content

Commit ed9a1dd

Browse files
committed
Added tasks 3678-3686
1 parent baab228 commit ed9a1dd

File tree

10 files changed

+656
-1
lines changed
  • src/main/kotlin/g3601_3700
    • s3675_minimum_operations_to_transform_string
    • s3678_smallest_absent_positive_greater_than_average
    • s3679_minimum_discards_to_balance_inventory
    • s3680_generate_schedule
    • s3681_maximum_xor_of_subsequences
    • s3683_earliest_time_to_finish_one_task
    • s3684_maximize_sum_of_at_most_k_distinct_elements
    • s3685_subsequence_sum_after_capping_elements
    • s3686_number_of_stable_subsequences

10 files changed

+656
-1
lines changed

README.md

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

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3686 |[Number of Stable Subsequences](src/main/kotlin/g3601_3700/s3686_number_of_stable_subsequences)| Hard | Weekly_Contest_467 | 11 | 100.00
2092+
| 3685 |[Subsequence Sum After Capping Elements](src/main/kotlin/g3601_3700/s3685_subsequence_sum_after_capping_elements)| Medium | Weekly_Contest_467 | 33 | 100.00
2093+
| 3684 |[Maximize Sum of At Most K Distinct Elements](src/main/kotlin/g3601_3700/s3684_maximize_sum_of_at_most_k_distinct_elements)| Easy | Weekly_Contest_467 | 12 | 92.59
2094+
| 3683 |[Earliest Time to Finish One Task](src/main/kotlin/g3601_3700/s3683_earliest_time_to_finish_one_task)| Easy | Weekly_Contest_467 | 1 | 100.00
2095+
| 3681 |[Maximum XOR of Subsequences](src/main/kotlin/g3601_3700/s3681_maximum_xor_of_subsequences)| Hard | Biweekly_Contest_165 | 26 | 100.00
2096+
| 3680 |[Generate Schedule](src/main/kotlin/g3601_3700/s3680_generate_schedule)| Medium | Biweekly_Contest_165 | 3 | 100.00
2097+
| 3679 |[Minimum Discards to Balance Inventory](src/main/kotlin/g3601_3700/s3679_minimum_discards_to_balance_inventory)| Medium | Biweekly_Contest_165 | 6 | 100.00
2098+
| 3678 |[Smallest Absent Positive Greater Than Average](src/main/kotlin/g3601_3700/s3678_smallest_absent_positive_greater_than_average)| Easy | Biweekly_Contest_165 | 3 | 100.00
20912099
| 3677 |[Count Binary Palindromic Numbers](src/main/kotlin/g3601_3700/s3677_count_binary_palindromic_numbers)| Hard | Weekly_Contest_466 | 1 | 100.00
20922100
| 3676 |[Count Bowl Subarrays](src/main/kotlin/g3601_3700/s3676_count_bowl_subarrays)| Medium | Weekly_Contest_466 | 3 | 100.00
20932101
| 3675 |[Minimum Operations to Transform String](src/main/kotlin/g3601_3700/s3675_minimum_operations_to_transform_string)| Medium | Weekly_Contest_466 | 6 | 97.92

src/main/kotlin/g3601_3700/s3675_minimum_operations_to_transform_string/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Solution {
5151
val n = s.length
5252
var ans = 0
5353
for (i in 0..<n) {
54-
val c = s.get(i)
54+
val c = s[i]
5555
if (c != 'a') {
5656
val ops = 'z'.code - c.code + 1
5757
if (ops > ans) {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
## 3678\. Smallest Absent Positive Greater Than Average
5+
6+
Easy
7+
8+
You are given an integer array `nums`.
9+
10+
Return the **smallest absent positive** integer in `nums` such that it is **strictly greater** than the **average** of all elements in `nums`.
11+
12+
The **average** of an array is defined as the sum of all its elements divided by the number of elements.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [3,5]
17+
18+
**Output:** 6
19+
20+
**Explanation:**
21+
22+
* The average of `nums` is `(3 + 5) / 2 = 8 / 2 = 4`.
23+
* The smallest absent positive integer greater than 4 is 6.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [-1,1,2]
28+
29+
**Output:** 3
30+
31+
**Explanation:**
32+
33+
* The average of `nums` is `(-1 + 1 + 2) / 3 = 2 / 3 = 0.667`.
34+
* The smallest absent positive integer greater than 0.667 is 3.
35+
36+
**Example 3:**
37+
38+
**Input:** nums = [4,-1]
39+
40+
**Output:** 2
41+
42+
**Explanation:**
43+
44+
* The average of `nums` is `(4 + (-1)) / 2 = 3 / 2 = 1.50`.
45+
* The smallest absent positive integer greater than 1.50 is 2.
46+
47+
**Constraints:**
48+
49+
* `1 <= nums.length <= 100`
50+
* `-100 <= nums[i] <= 100`
51+
52+
## Solution
53+
54+
```kotlin
55+
class Solution {
56+
fun smallestAbsent(nums: IntArray): Int {
57+
var sum = 0
58+
for (j in nums) {
59+
sum += j
60+
}
61+
val avg = sum.toDouble() / nums.size
62+
var num: Int
63+
if (avg < 0) {
64+
num = 1
65+
} else {
66+
num = avg.toInt() + 1
67+
}
68+
while (true) {
69+
var flag = false
70+
for (j in nums) {
71+
if (num == j) {
72+
flag = true
73+
break
74+
}
75+
}
76+
if (!flag && num > avg) {
77+
return num
78+
}
79+
num++
80+
}
81+
}
82+
}
83+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
## 3679\. Minimum Discards to Balance Inventory
5+
6+
Medium
7+
8+
You are given two integers `w` and `m`, and an integer array `arrivals`, where `arrivals[i]` is the type of item arriving on day `i` (days are **1-indexed**).
9+
10+
Items are managed according to the following rules:
11+
12+
* Each arrival may be **kept** or **discarded**; an item may only be discarded on its arrival day.
13+
* For each day `i`, consider the window of days `[max(1, i - w + 1), i]` (the `w` most recent days up to day `i`):
14+
* For **any** such window, each item type may appear **at most** `m` times among kept arrivals whose arrival day lies in that window.
15+
* If keeping the arrival on day `i` would cause its type to appear **more than** `m` times in the window, that arrival **must** be discarded.
16+
17+
Return the **minimum** number of arrivals to be discarded so that every `w`\-day window contains at most `m` occurrences of each type.
18+
19+
**Example 1:**
20+
21+
**Input:** arrivals = [1,2,1,3,1], w = 4, m = 2
22+
23+
**Output:** 0
24+
25+
**Explanation:**
26+
27+
* On day 1, Item 1 arrives; the window contains no more than `m` occurrences of this type, so we keep it.
28+
* On day 2, Item 2 arrives; the window of days 1 - 2 is fine.
29+
* On day 3, Item 1 arrives, window `[1, 2, 1]` has item 1 twice, within limit.
30+
* On day 4, Item 3 arrives, window `[1, 2, 1, 3]` has item 1 twice, allowed.
31+
* On day 5, Item 1 arrives, window `[2, 1, 3, 1]` has item 1 twice, still valid.
32+
33+
There are no discarded items, so return 0.
34+
35+
**Example 2:**
36+
37+
**Input:** arrivals = [1,2,3,3,3,4], w = 3, m = 2
38+
39+
**Output:** 1
40+
41+
**Explanation:**
42+
43+
* On day 1, Item 1 arrives. We keep it.
44+
* On day 2, Item 2 arrives, window `[1, 2]` is fine.
45+
* On day 3, Item 3 arrives, window `[1, 2, 3]` has item 3 once.
46+
* On day 4, Item 3 arrives, window `[2, 3, 3]` has item 3 twice, allowed.
47+
* On day 5, Item 3 arrives, window `[3, 3, 3]` has item 3 three times, exceeds limit, so the arrival must be discarded.
48+
* On day 6, Item 4 arrives, window `[3, 4]` is fine.
49+
50+
Item 3 on day 5 is discarded, and this is the minimum number of arrivals to discard, so return 1.
51+
52+
**Constraints:**
53+
54+
* <code>1 <= arrivals.length <= 10<sup>5</sup></code>
55+
* <code>1 <= arrivals[i] <= 10<sup>5</sup></code>
56+
* `1 <= w <= arrivals.length`
57+
* `1 <= m <= w`
58+
59+
## Solution
60+
61+
```kotlin
62+
import kotlin.math.max
63+
64+
class Solution {
65+
fun minArrivalsToDiscard(arrivals: IntArray, w: Int, m: Int): Int {
66+
val n = arrivals.size
67+
var dis = 0
68+
val removed = BooleanArray(n)
69+
var maxVal = 0
70+
for (v in arrivals) {
71+
maxVal = max(maxVal, v)
72+
}
73+
val freq = IntArray(maxVal + 1)
74+
for (i in 0..<n) {
75+
val outIdx = i - w
76+
if (outIdx >= 0 && !removed[outIdx]) {
77+
val oldVal = arrivals[outIdx]
78+
freq[oldVal]--
79+
}
80+
val `val` = arrivals[i]
81+
if (freq[`val`] >= m) {
82+
dis++
83+
removed[i] = true
84+
} else {
85+
freq[`val`]++
86+
}
87+
}
88+
return dis
89+
}
90+
}
91+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
## 3680\. Generate Schedule
5+
6+
Medium
7+
8+
You are given an integer `n` representing `n` teams. You are asked to generate a schedule such that:
9+
10+
* Each team plays every other team **exactly twice**: once at home and once away.
11+
* There is **exactly one** match per day; the schedule is a list of **consecutive** days and `schedule[i]` is the match on day `i`.
12+
* No team plays on **consecutive** days.
13+
14+
Return a 2D integer array `schedule`, where `schedule[i][0]` represents the home team and `schedule[i][1]` represents the away team. If multiple schedules meet the conditions, return **any** one of them.
15+
16+
If no schedule exists that meets the conditions, return an empty array.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 3
21+
22+
**Output:** []
23+
24+
**Explanation:**
25+
26+
Since each team plays every other team exactly twice, a total of 6 matches need to be played: `[0,1],[0,2],[1,2],[1,0],[2,0],[2,1]`.
27+
28+
It's not possible to create a schedule without at least one team playing consecutive days.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 5
33+
34+
**Output:** [[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]
35+
36+
**Explanation:**
37+
38+
Since each team plays every other team exactly twice, a total of 20 matches need to be played.
39+
40+
The output shows one of the schedules that meet the conditions. No team plays on consecutive days.
41+
42+
**Constraints:**
43+
44+
* `2 <= n <= 50`
45+
46+
## Solution
47+
48+
```kotlin
49+
class Solution {
50+
fun generateSchedule(n: Int): Array<IntArray> {
51+
if (n < 5) {
52+
return Array<IntArray>(0) { IntArray(0) }
53+
}
54+
val res = Array<IntArray>(n * (n - 1)) { IntArray(2) }
55+
var idx = 0
56+
for (i in 2..<n - 1) {
57+
for (j in 0..<n) {
58+
res[idx++] = intArrayOf(j, (j + i) % n)
59+
}
60+
}
61+
for (i in 0..<n) {
62+
res[idx++] = intArrayOf(i, (i + 1) % n)
63+
res[idx++] = intArrayOf((i + 4) % n, (i + 3) % n)
64+
}
65+
return res
66+
}
67+
}
68+
```
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
## 3681\. Maximum XOR of Subsequences
5+
6+
Hard
7+
8+
You are given an integer array `nums` of length `n` where each element is a non-negative integer.
9+
10+
Select **two** **subsequences** of `nums` (they may be empty and are **allowed** to **overlap**), each preserving the original order of elements, and let:
11+
12+
* `X` be the bitwise XOR of all elements in the first subsequence.
13+
* `Y` be the bitwise XOR of all elements in the second subsequence.
14+
15+
Return the **maximum** possible value of `X XOR Y`.
16+
17+
**Note:** The XOR of an **empty** subsequence is 0.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,3]
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
Choose subsequences:
28+
29+
* First subsequence `[2]`, whose XOR is 2.
30+
* Second subsequence `[2,3]`, whose XOR is 1.
31+
32+
Then, XOR of both subsequences = `2 XOR 1 = 3`.
33+
34+
This is the maximum XOR value achievable from any two subsequences.
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [5,2]
39+
40+
**Output:** 7
41+
42+
**Explanation:**
43+
44+
Choose subsequences:
45+
46+
* First subsequence `[5]`, whose XOR is 5.
47+
* Second subsequence `[2]`, whose XOR is 2.
48+
49+
Then, XOR of both subsequences = `5 XOR 2 = 7`.
50+
51+
This is the maximum XOR value achievable from any two subsequences.
52+
53+
**Constraints:**
54+
55+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
56+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
57+
58+
## Solution
59+
60+
```kotlin
61+
import kotlin.math.max
62+
import kotlin.math.min
63+
64+
class Solution {
65+
fun maxXorSubsequences(nums: IntArray): Int {
66+
val n = nums.size
67+
if (n == 0) {
68+
return 0
69+
}
70+
var x = 0
71+
while (true) {
72+
var y = 0
73+
for (v in nums) {
74+
if (v > y) {
75+
y = v
76+
}
77+
}
78+
if (y == 0) {
79+
return x
80+
}
81+
x = max(x, x xor y)
82+
for (i in 0..<n) {
83+
val v = nums[i]
84+
nums[i] = min(v, v xor y)
85+
}
86+
}
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)