Skip to content

Commit 0ac9def

Browse files
authored
Added tasks 2402, 2404, 2405, 2406.
1 parent e9b3949 commit 0ac9def

File tree

13 files changed

+399
-0
lines changed

13 files changed

+399
-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+
| 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
1852+
| 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
1853+
| 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
1854+
| 2402 |[Meeting Rooms III](src/main/java/g2401_2500/s2402_meeting_rooms_iii/Solution.java)| Hard | Array, Sorting, Heap_Priority_Queue | 124 | 72.79
18511855
| 2401 |[Longest Nice Subarray](src/main/java/g2401_2500/s2401_longest_nice_subarray/Solution.java)| Medium | Array, Bit_Manipulation, Sliding_Window | 4 | 87.45
18521856
| 2400 |[Number of Ways to Reach a Position After Exactly k Steps](src.save/main/java/g2301_2400/s2400_number_of_ways_to_reach_a_position_after_exactly_k_steps/Solution.java)| Medium | Dynamic_Programming, Math, Combinatorics | 1 | 99.66
18531857
| 2399 |[Check Distances Between Same Letters](src.save/main/java/g2301_2400/s2399_check_distances_between_same_letters/Solution.java)| Easy | Array, String, Hash_Table | 1 | 99.88
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2401_2500.s2402_meeting_rooms_iii;
2+
3+
// #Hard #Array #Sorting #Heap_Priority_Queue
4+
// #2022_10_23_Time_124_ms_(72.79%)_Space_107_MB_(80.70%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public int mostBooked(int n, int[][] meetings) {
10+
int[] counts = new int[n];
11+
long[] endTimes = new long[n];
12+
Arrays.sort(meetings, (a, b) -> Integer.compare(a[0], b[0]));
13+
for (int[] meeting : meetings) {
14+
int id = findRoomId(endTimes, meeting[0]);
15+
counts[id]++;
16+
endTimes[id] = Math.max(endTimes[id], meeting[0]) + meeting[1] - meeting[0];
17+
}
18+
int res = 0;
19+
long count = 0;
20+
for (int i = 0; i < n; i++) {
21+
if (counts[i] > count) {
22+
count = counts[i];
23+
res = i;
24+
}
25+
}
26+
return res;
27+
}
28+
29+
private int findRoomId(long[] endTimes, int start) {
30+
int n = endTimes.length;
31+
// Find the first one
32+
for (int i = 0; i < n; i++) {
33+
if (endTimes[i] <= start) {
34+
return i;
35+
}
36+
}
37+
// Only when non is not delayed, then we find the smallest one
38+
int id = 0;
39+
long min = Long.MAX_VALUE;
40+
for (int i = 0; i < n; i++) {
41+
if (endTimes[i] < min) {
42+
min = endTimes[i];
43+
id = i;
44+
}
45+
}
46+
return id;
47+
}
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2402\. Meeting Rooms III
2+
3+
Hard
4+
5+
You are given an integer `n`. There are `n` rooms numbered from `0` to `n - 1`.
6+
7+
You are given a 2D integer array `meetings` where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the **half-closed** time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are **unique**.
8+
9+
Meetings are allocated to rooms in the following manner:
10+
11+
1. Each meeting will take place in the unused room with the **lowest** number.
12+
2. If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the **same** duration as the original meeting.
13+
3. When a room becomes unused, meetings that have an earlier original **start** time should be given the room.
14+
15+
Return _the **number** of the room that held the most meetings._ If there are multiple rooms, return _the room with the **lowest** number._
16+
17+
A **half-closed interval** `[a, b)` is the interval between `a` and `b` **including** `a` and **not including** `b`.
18+
19+
**Example 1:**
20+
21+
**Input:** n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
22+
23+
**Output:** 0
24+
25+
**Explanation:**
26+
27+
- At time 0, both rooms are not being used. The first meeting starts in room 0.
28+
29+
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
30+
31+
- At time 2, both rooms are being used. The third meeting is delayed.
32+
33+
- At time 3, both rooms are being used. The fourth meeting is delayed.
34+
35+
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
36+
37+
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
38+
39+
Both rooms 0 and 1 held 2 meetings, so we return 0.
40+
41+
**Example 2:**
42+
43+
**Input:** n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
44+
45+
**Output:** 1
46+
47+
**Explanation:**
48+
49+
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
50+
51+
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
52+
53+
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
54+
55+
- At time 4, all three rooms are being used. The fourth meeting is delayed.
56+
57+
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
58+
59+
- At time 6, all three rooms are being used. The fifth meeting is delayed.
60+
61+
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
62+
63+
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
64+
65+
**Constraints:**
66+
67+
* `1 <= n <= 100`
68+
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
69+
* `meetings[i].length == 2`
70+
* <code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code>
71+
* All the values of <code>start<sub>i</sub></code> are **unique**.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2401_2500.s2404_most_frequent_even_element;
2+
3+
// #Easy #Array #Hash_Table #Counting #2022_10_23_Time_32_ms_(81.60%)_Space_56.5_MB_(79.42%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int mostFrequentEven(int[] nums) {
10+
Map<Integer, Integer> frequencyMap = new HashMap<>();
11+
int mostFrequent = Integer.MAX_VALUE;
12+
int maxFrequency = 0;
13+
for (int num : nums) {
14+
if ((num & 1) == 0) {
15+
maxFrequency =
16+
Math.max(
17+
maxFrequency,
18+
frequencyMap.compute(num, (n, freq) -> freq == null ? 1 : ++freq));
19+
}
20+
}
21+
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
22+
if (entry.getValue() == maxFrequency) {
23+
mostFrequent = Math.min(mostFrequent, entry.getKey());
24+
}
25+
}
26+
return mostFrequent == Integer.MAX_VALUE ? -1 : mostFrequent;
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2404\. Most Frequent Even Element
2+
3+
Easy
4+
5+
Given an integer array `nums`, return _the most frequent even element_.
6+
7+
If there is a tie, return the **smallest** one. If there is no such element, return `-1`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [0,1,2,2,4,4,1]
12+
13+
**Output:** 2
14+
15+
**Explanation:**
16+
17+
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
18+
19+
We return the smallest one, which is 2.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [4,4,4,9,2,4]
24+
25+
**Output:** 4
26+
27+
**Explanation:** 4 is the even element appears the most.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [29,47,21,41,13,37,25,7]
32+
33+
**Output:** -1
34+
35+
**Explanation:** There is no even element.
36+
37+
**Constraints:**
38+
39+
* `1 <= nums.length <= 2000`
40+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2401_2500.s2405_optimal_partition_of_string;
2+
3+
// #Medium #String #Hash_Table #Greedy #2022_10_23_Time_7_ms_(99.40%)_Space_43.3_MB_(91.63%)
4+
5+
public class Solution {
6+
public int partitionString(String s) {
7+
int count = 1;
8+
boolean[] arr = new boolean[26];
9+
for (char c : s.toCharArray()) {
10+
if (arr[c - 'a']) {
11+
count++;
12+
arr = new boolean[26];
13+
}
14+
arr[c - 'a'] = true;
15+
}
16+
return count;
17+
}
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2405\. Optimal Partition of String
2+
3+
Medium
4+
5+
Given a string `s`, partition the string into one or more **substrings** such that the characters in each substring are **unique**. That is, no letter appears in a single substring more than **once**.
6+
7+
Return _the **minimum** number of substrings in such a partition._
8+
9+
Note that each character should belong to exactly one substring in a partition.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abacaba"
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
19+
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
20+
21+
It can be shown that 4 is the minimum number of substrings needed.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "ssssss"
26+
27+
**Output:** 6
28+
29+
**Explanation:**
30+
31+
The only valid partition is ("s","s","s","s","s","s").
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 10<sup>5</sup></code>
36+
* `s` consists of only English lowercase letters.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2401_2500.s2406_divide_intervals_into_minimum_number_of_groups;
2+
3+
// #Medium #Array #Sorting #Greedy #Two_Pointers #Prefix_Sum #Heap_Priority_Queue
4+
// #2022_10_23_Time_144_ms_(71.27%)_Space_151.1_MB_(13.30%)
5+
6+
public class Solution {
7+
public int minGroups(int[][] intervals) {
8+
int maxElement = 0;
9+
for (int[] i : intervals) {
10+
maxElement = Math.max(maxElement, i[0]);
11+
maxElement = Math.max(maxElement, i[1]);
12+
}
13+
long[] prefixSum = new long[maxElement + 2];
14+
for (int[] i : intervals) {
15+
prefixSum[i[0]] += 1;
16+
prefixSum[i[1] + 1] -= 1;
17+
}
18+
long ans = 0;
19+
for (int i = 1; i <= maxElement + 1; i++) {
20+
prefixSum[i] += prefixSum[i - 1];
21+
ans = Math.max(ans, prefixSum[i]);
22+
}
23+
return (int) ans;
24+
}
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2406\. Divide Intervals Into Minimum Number of Groups
2+
3+
Medium
4+
5+
You are given a 2D integer array `intervals` where <code>intervals[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> represents the **inclusive** interval <code>[left<sub>i</sub>, right<sub>i</sub>]</code>.
6+
7+
You have to divide the intervals into one or more **groups** such that each interval is in **exactly** one group, and no two intervals that are in the same group **intersect** each other.
8+
9+
Return _the **minimum** number of groups you need to make_.
10+
11+
Two intervals **intersect** if there is at least one common number between them. For example, the intervals `[1, 5]` and `[5, 8]` intersect.
12+
13+
**Example 1:**
14+
15+
**Input:** intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
16+
17+
**Output:** 3
18+
19+
**Explanation:** We can divide the intervals into the following groups:
20+
21+
- Group 1: [1, 5], [6, 8].
22+
23+
- Group 2: [2, 3], [5, 10].
24+
25+
- Group 3: [1, 10].
26+
27+
It can be proven that it is not possible to divide the intervals into fewer than 3 groups.
28+
29+
**Example 2:**
30+
31+
**Input:** intervals = [[1,3],[5,6],[8,10],[11,13]]
32+
33+
**Output:** 1
34+
35+
**Explanation:** None of the intervals overlap, so we can put all of them in one group.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= intervals.length <= 10<sup>5</sup></code>
40+
* `intervals[i].length == 2`
41+
* <code>1 <= left<sub>i</sub> <= right<sub>i</sub> <= 10<sup>6</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2401_2500.s2402_meeting_rooms_iii;
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 mostBooked() {
11+
assertThat(
12+
new Solution().mostBooked(2, new int[][] {{0, 10}, {1, 5}, {2, 7}, {3, 4}}),
13+
equalTo(0));
14+
}
15+
16+
@Test
17+
void mostBooked2() {
18+
assertThat(
19+
new Solution()
20+
.mostBooked(3, new int[][] {{1, 20}, {2, 10}, {3, 5}, {4, 9}, {6, 8}}),
21+
equalTo(1));
22+
}
23+
}

0 commit comments

Comments
 (0)