Skip to content

Commit c4e3739

Browse files
authored
Added tasks 2407, 2409, 2410, 2411.
1 parent 9b75e89 commit c4e3739

File tree

13 files changed

+442
-0
lines changed

13 files changed

+442
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,10 @@ implementation 'com.github.javadev:leetcode-in-java:1.14'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2411 |[Smallest Subarrays With Maximum Bitwise OR](src/main/java/g2401_2500/s2411_smallest_subarrays_with_maximum_bitwise_or/Solution.java)| |||
1852+
| 2410 |[Maximum Matching of Players With Trainers](src/main/java/g2401_2500/s2410_maximum_matching_of_players_with_trainers/Solution.java)| Medium | Array, Sorting, Greedy, Two_Pointers | 28 | 98.31
1853+
| 2409 |[Count Days Spent Together](src/main/java/g2401_2500/s2409_count_days_spent_together/Solution.java)| Easy | String, Math | 0 | 100.00
1854+
| 2407 |[Longest Increasing Subsequence II](src/main/java/g2401_2500/s2407_longest_increasing_subsequence_ii/Solution.java)| Hard | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Segment_Tree, Binary_Indexed_Tree, Monotonic_Queue | 24 | 99.21
18511855
| 2406 |[Divide Intervals Into Minimum Number of Groups](src/main/java/g2401_2500/s2406_divide_intervals_into_minimum_number_of_groups/Solution.java)| Medium | Array, Sorting, Greedy, Two_Pointers, Prefix_Sum, Heap_Priority_Queue | 144 | 71.27
18521856
| 2405 |[Optimal Partition of String](src/main/java/g2401_2500/s2405_optimal_partition_of_string/Solution.java)| Medium | String, Hash_Table, Greedy | 7 | 99.40
18531857
| 2404 |[Most Frequent Even Element](src/main/java/g2401_2500/s2404_most_frequent_even_element/Solution.java)| Easy | Array, Hash_Table, Counting | 32 | 81.60
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g2401_2500.s2407_longest_increasing_subsequence_ii;
2+
3+
// #Hard #Array #Dynamic_Programming #Divide_and_Conquer #Queue #Segment_Tree #Binary_Indexed_Tree
4+
// #Monotonic_Queue #2022_10_24_Time_24_ms_(99.21%)_Space_50.3_MB_(98.08%)
5+
6+
public class Solution {
7+
private static class SegTree {
8+
private int[] arr;
9+
private int n;
10+
11+
public SegTree(int n) {
12+
this.n = n;
13+
arr = new int[2 * n];
14+
}
15+
16+
public int query(int l, int r) {
17+
l += n;
18+
r += n;
19+
int ans = 0;
20+
while (l < r) {
21+
if ((l & 1) == 1) {
22+
ans = Math.max(ans, arr[l]);
23+
l += 1;
24+
}
25+
if ((r & 1) == 1) {
26+
r -= 1;
27+
ans = Math.max(ans, arr[r]);
28+
}
29+
l = l >> 1;
30+
r = r >> 1;
31+
}
32+
return ans;
33+
}
34+
35+
public void update(int i, int val) {
36+
i += n;
37+
arr[i] = val;
38+
while (i > 0) {
39+
i = i >> 1;
40+
arr[i] = Math.max(arr[2 * i], arr[2 * i + 1]);
41+
}
42+
}
43+
}
44+
45+
public int lengthOfLIS(int[] nums, int k) {
46+
int max = 0;
47+
for (int n : nums) {
48+
max = Math.max(max, n);
49+
}
50+
SegTree seg = new SegTree(max);
51+
int ans = 0;
52+
for (int n : nums) {
53+
n -= 1;
54+
int temp = seg.query(Math.max(0, n - k), n) + 1;
55+
ans = Math.max(ans, temp);
56+
seg.update(n, temp);
57+
}
58+
return ans;
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2407\. Longest Increasing Subsequence II
2+
3+
Hard
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
Find the longest subsequence of `nums` that meets the following requirements:
8+
9+
* The subsequence is **strictly increasing** and
10+
* The difference between adjacent elements in the subsequence is **at most** `k`.
11+
12+
Return _the length of the **longest** **subsequence** that meets the requirements._
13+
14+
A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,2,1,4,3,4,5,8,15], k = 3
19+
20+
**Output:** 5
21+
22+
**Explanation:**
23+
24+
The longest subsequence that meets the requirements is [1,3,4,5,8].
25+
26+
The subsequence has a length of 5, so we return 5.
27+
28+
Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [7,4,5,1,8,12,4,7], k = 5
33+
34+
**Output:** 4
35+
36+
**Explanation:**
37+
38+
The longest subsequence that meets the requirements is [4,5,8,12].
39+
40+
The subsequence has a length of 4, so we return 4.
41+
42+
**Example 3:**
43+
44+
**Input:** nums = [1,5], k = 1
45+
46+
**Output:** 1
47+
48+
**Explanation:** The longest subsequence that meets the requirements is [1]. The subsequence has a length of 1, so we return 1.
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>1 <= nums[i], k <= 10<sup>5</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g2401_2500.s2409_count_days_spent_together;
2+
3+
// #Easy #String #Math #2022_10_24_Time_0_ms_(100.00%)_Space_40.3_MB_(91.71%)
4+
5+
public class Solution {
6+
private int[] dates = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
7+
8+
public int countDaysTogether(
9+
String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
10+
if (leaveAlice.compareTo(arriveBob) < 0 || leaveBob.compareTo(arriveAlice) < 0) {
11+
return 0;
12+
}
13+
String end = leaveAlice.compareTo(leaveBob) <= 0 ? leaveAlice : leaveBob;
14+
String start = arriveAlice.compareTo(arriveBob) <= 0 ? arriveBob : arriveAlice;
15+
String[] starts = start.split("-");
16+
String[] ends = end.split("-");
17+
Integer startMonth = Integer.valueOf(starts[0]);
18+
Integer endMonth = Integer.valueOf(ends[0]);
19+
int res = 0;
20+
if (startMonth.equals(endMonth)) {
21+
res += (Integer.valueOf(ends[1]) - Integer.valueOf(starts[1]) + 1);
22+
return res;
23+
}
24+
for (int i = startMonth; i <= endMonth; i++) {
25+
if (i == endMonth) {
26+
res += Integer.valueOf(ends[1]);
27+
} else if (i == startMonth) {
28+
res += dates[i] - Integer.valueOf(starts[1]) + 1;
29+
} else {
30+
res += dates[i];
31+
}
32+
}
33+
return res;
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2409\. Count Days Spent Together
2+
3+
Easy
4+
5+
Alice and Bob are traveling to Rome for separate business meetings.
6+
7+
You are given 4 strings `arriveAlice`, `leaveAlice`, `arriveBob`, and `leaveBob`. Alice will be in the city from the dates `arriveAlice` to `leaveAlice` (**inclusive**), while Bob will be in the city from the dates `arriveBob` to `leaveBob` (**inclusive**). Each will be a 5-character string in the format `"MM-DD"`, corresponding to the month and day of the date.
8+
9+
Return _the total number of days that Alice and Bob are in Rome together._
10+
11+
You can assume that all dates occur in the **same** calendar year, which is **not** a leap year. Note that the number of days per month can be represented as: `[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]`.
12+
13+
**Example 1:**
14+
15+
**Input:** arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19"
16+
17+
**Output:** 3
18+
19+
**Explanation:** Alice will be in Rome from August 15 to August 18. Bob will be in Rome from August 16 to August 19. They are both in Rome together on August 16th, 17th, and 18th, so the answer is 3.
20+
21+
**Example 2:**
22+
23+
**Input:** arriveAlice = "10-01", leaveAlice = "10-31", arriveBob = "11-01", leaveBob = "12-31"
24+
25+
**Output:** 0
26+
27+
**Explanation:** There is no day when Alice and Bob are in Rome together, so we return 0.
28+
29+
**Constraints:**
30+
31+
* All dates are provided in the format `"MM-DD"`.
32+
* Alice and Bob's arrival dates are **earlier than or equal to** their leaving dates.
33+
* The given dates are valid dates of a **non-leap** year.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2401_2500.s2410_maximum_matching_of_players_with_trainers;
2+
3+
// #Medium #Array #Sorting #Greedy #Two_Pointers
4+
// #2022_10_24_Time_28_ms_(98.31%)_Space_60.2_MB_(94.06%)
5+
6+
import java.util.Arrays;
7+
8+
@SuppressWarnings("java:S135")
9+
public class Solution {
10+
public int matchPlayersAndTrainers(int[] players, int[] trainers) {
11+
Arrays.sort(players);
12+
Arrays.sort(trainers);
13+
int i = 0;
14+
int j = 0;
15+
int count = 0;
16+
i = 0;
17+
while (i < players.length) {
18+
while (trainers[j] < players[i]) {
19+
j++;
20+
if (j >= trainers.length) {
21+
break;
22+
}
23+
}
24+
if (j >= trainers.length) {
25+
break;
26+
}
27+
if (trainers[j] >= players[i]) {
28+
count++;
29+
}
30+
i++;
31+
j++;
32+
if (j >= trainers.length || i >= players.length) {
33+
break;
34+
}
35+
}
36+
return count;
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2410\. Maximum Matching of Players With Trainers
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `players`, where `players[i]` represents the **ability** of the <code>i<sup>th</sup></code> player. You are also given a **0-indexed** integer array `trainers`, where `trainers[j]` represents the **training capacity** of the <code>j<sup>th</sup></code> trainer.
6+
7+
The <code>i<sup>th</sup></code> player can **match** with the <code>j<sup>th</sup></code> trainer if the player's ability is **less than or equal to** the trainer's training capacity. Additionally, the <code>i<sup>th</sup></code> player can be matched with at most one trainer, and the <code>j<sup>th</sup></code> trainer can be matched with at most one player.
8+
9+
Return _the **maximum** number of matchings between_ `players` _and_ `trainers` _that satisfy these conditions._
10+
11+
**Example 1:**
12+
13+
**Input:** players = [4,7,9], trainers = [8,2,5,8]
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
One of the ways we can form two matchings is as follows:
20+
21+
- players[0] can be matched with trainers[0] since 4 <= 8.
22+
23+
- players[1] can be matched with trainers[3] since 7 <= 8.
24+
25+
It can be proven that 2 is the maximum number of matchings that can be formed.
26+
27+
**Example 2:**
28+
29+
**Input:** players = [1,1,1], trainers = [10]
30+
31+
**Output:** 1
32+
33+
**Explanation:**
34+
35+
The trainer can be matched with any of the 3 players.
36+
37+
Each player can only be matched with one trainer, so the maximum answer is 1.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= players.length, trainers.length <= 10<sup>5</sup></code>
42+
* <code>1 <= players[i], trainers[j] <= 10<sup>9</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2401_2500.s2411_smallest_subarrays_with_maximum_bitwise_or;
2+
3+
public class Solution {
4+
public int[] smallestSubarrays(int[] nums) {
5+
int[] res = new int[nums.length];
6+
int[] bitPosition = new int[30];
7+
for (int i = nums.length - 1; i >= 0; i--) {
8+
res[i] = 1;
9+
for (int j = 0; j < 30; j++) {
10+
if ((nums[i] & (1 << j)) > 0) {
11+
bitPosition[j] = i;
12+
}
13+
res[i] = Math.max(res[i], bitPosition[j] - i + 1);
14+
}
15+
}
16+
return res;
17+
}
18+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2411\. Smallest Subarrays With Maximum Bitwise OR
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` of length `n`, consisting of non-negative integers. For each index `i` from `0` to `n - 1`, you must determine the size of the **minimum sized** non-empty subarray of `nums` starting at `i` (**inclusive**) that has the **maximum** possible **bitwise OR**.
6+
7+
* In other words, let <code>B<sub>ij</sub></code> be the bitwise OR of the subarray `nums[i...j]`. You need to find the smallest subarray starting at `i`, such that bitwise OR of this subarray is equal to <code>max(B<sub>ik</sub>)</code> where `i <= k <= n - 1`.
8+
9+
The bitwise OR of an array is the bitwise OR of all the numbers in it.
10+
11+
Return _an integer array_ `answer` _of size_ `n` _where_ `answer[i]` _is the length of the **minimum** sized subarray starting at_ `i` _with **maximum** bitwise OR._
12+
13+
A **subarray** is a contiguous non-empty sequence of elements within an array.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,0,2,1,3]
18+
19+
**Output:** [3,3,2,2,1]
20+
21+
**Explanation:**
22+
23+
The maximum possible bitwise OR starting at any index is 3.
24+
25+
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
26+
27+
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
28+
29+
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
30+
31+
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
32+
33+
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
34+
35+
Therefore, we return [3,3,2,2,1].
36+
37+
**Example 2:**
38+
39+
**Input:** nums = [1,2]
40+
41+
**Output:** [2,1]
42+
43+
**Explanation:**
44+
45+
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
46+
47+
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
48+
49+
Therefore, we return [2,1].
50+
51+
**Constraints:**
52+
53+
* `n == nums.length`
54+
* <code>1 <= n <= 10<sup>5</sup></code>
55+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2401_2500.s2407_longest_increasing_subsequence_ii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void lengthOfLIS() {
11+
assertThat(
12+
new Solution().lengthOfLIS(new int[] {4, 2, 1, 4, 3, 4, 5, 8, 15}, 3), equalTo(5));
13+
}
14+
15+
@Test
16+
void lengthOfLIS2() {
17+
assertThat(new Solution().lengthOfLIS(new int[] {7, 4, 5, 1, 8, 12, 4, 7}, 5), equalTo(4));
18+
}
19+
20+
@Test
21+
void lengthOfLIS3() {
22+
assertThat(new Solution().lengthOfLIS(new int[] {1, 5}, 1), equalTo(1));
23+
}
24+
}

0 commit comments

Comments
 (0)