Skip to content

Commit b501601

Browse files
authored
Added tasks 3168-3171
1 parent 35b6b84 commit b501601

File tree

12 files changed

+400
-0
lines changed

12 files changed

+400
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room;
2+
3+
// #Easy #String #Simulation #2024_06_06_Time_1_ms_(100.00%)_Space_41.9_MB_(67.53%)
4+
5+
public class Solution {
6+
public int minimumChairs(String s) {
7+
int count = 0;
8+
int ans = Integer.MIN_VALUE;
9+
for (char ch : s.toCharArray()) {
10+
if (ch == 'E') {
11+
count++;
12+
ans = Math.max(ans, count);
13+
} else {
14+
count--;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
3168\. Minimum Number of Chairs in a Waiting Room
2+
3+
Easy
4+
5+
You are given a string `s`. Simulate events at each second `i`:
6+
7+
* If `s[i] == 'E'`, a person enters the waiting room and takes one of the chairs in it.
8+
* If `s[i] == 'L'`, a person leaves the waiting room, freeing up a chair.
9+
10+
Return the **minimum** number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially **empty**.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "EEEEEEE"
15+
16+
**Output:** 7
17+
18+
**Explanation:**
19+
20+
After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "ELELEEL"
25+
26+
**Output:** 2
27+
28+
**Explanation:**
29+
30+
Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.
31+
32+
| Second | Event | People in the Waiting Room | Available Chairs |
33+
|--------|-------|----------------------------|------------------|
34+
| 0 | Enter | 1 | 1 |
35+
| 1 | Leave | 0 | 2 |
36+
| 2 | Enter | 1 | 1 |
37+
| 3 | Leave | 0 | 2 |
38+
| 4 | Enter | 1 | 1 |
39+
| 5 | Enter | 2 | 0 |
40+
| 6 | Leave | 1 | 1 |
41+
42+
**Example 3:**
43+
44+
**Input:** s = "ELEELEELLL"
45+
46+
**Output:** 3
47+
48+
**Explanation:**
49+
50+
Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.
51+
52+
| Second | Event | People in the Waiting Room | Available Chairs |
53+
|--------|-------|----------------------------|------------------|
54+
| 0 | Enter | 1 | 2 |
55+
| 1 | Leave | 0 | 3 |
56+
| 2 | Enter | 1 | 2 |
57+
| 3 | Enter | 2 | 1 |
58+
| 4 | Leave | 1 | 2 |
59+
| 5 | Enter | 2 | 1 |
60+
| 6 | Enter | 3 | 0 |
61+
| 7 | Leave | 2 | 1 |
62+
| 8 | Leave | 1 | 2 |
63+
| 9 | Leave | 0 | 3 |
64+
65+
**Constraints:**
66+
67+
* `1 <= s.length <= 50`
68+
* `s` consists only of the letters `'E'` and `'L'`.
69+
* `s` represents a valid sequence of entries and exits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g3101_3200.s3169_count_days_without_meetings;
2+
3+
// #Medium #Array #Sorting #2024_06_06_Time_11_ms_(99.96%)_Space_113.7_MB_(5.10%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int countDays(int days, int[][] meetings) {
10+
List<int[]> availableDays = new ArrayList<>();
11+
availableDays.add(new int[] {1, days});
12+
// Iterate through each meeting
13+
for (int[] meeting : meetings) {
14+
int start = meeting[0];
15+
int end = meeting[1];
16+
List<int[]> newAvailableDays = new ArrayList<>();
17+
// Iterate through available days and split the intervals
18+
for (int[] interval : availableDays) {
19+
if (start > interval[1] || end < interval[0]) {
20+
// No overlap, keep the interval
21+
newAvailableDays.add(interval);
22+
} else {
23+
// Overlap, split the interval
24+
if (interval[0] < start) {
25+
newAvailableDays.add(new int[] {interval[0], start - 1});
26+
}
27+
if (interval[1] > end) {
28+
newAvailableDays.add(new int[] {end + 1, interval[1]});
29+
}
30+
}
31+
}
32+
availableDays = newAvailableDays;
33+
}
34+
// Count the remaining available days
35+
int availableDaysCount = 0;
36+
for (int[] interval : availableDays) {
37+
availableDaysCount += interval[1] - interval[0] + 1;
38+
}
39+
return availableDaysCount;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3169\. Count Days Without Meetings
2+
3+
Medium
4+
5+
You are given a positive integer `days` representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array `meetings` of size `n` where, `meetings[i] = [start_i, end_i]` represents the starting and ending days of meeting `i` (inclusive).
6+
7+
Return the count of days when the employee is available for work but no meetings are scheduled.
8+
9+
**Note:** The meetings may overlap.
10+
11+
**Example 1:**
12+
13+
**Input:** days = 10, meetings = [[5,7],[1,3],[9,10]]
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.
20+
21+
**Example 2:**
22+
23+
**Input:** days = 5, meetings = [[2,4],[1,3]]
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
There is no meeting scheduled on the 5<sup>th</sup> day.
30+
31+
**Example 3:**
32+
33+
**Input:** days = 6, meetings = [[1,6]]
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
Meetings are scheduled for all working days.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= days <= 10<sup>9</sup></code>
44+
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
45+
* `meetings[i].length == 2`
46+
* `1 <= meetings[i][0] <= meetings[i][1] <= days`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3101_3200.s3170_lexicographically_minimum_string_after_removing_stars;
2+
3+
// #Medium #String #Hash_Table #Greedy #Stack #Heap_Priority_Queue
4+
// #2024_06_06_Time_29_ms_(99.93%)_Space_45.6_MB_(92.80%)
5+
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public String clearStars(String s) {
10+
char[] arr = s.toCharArray();
11+
int[] idxChain = new int[arr.length];
12+
int[] lastIdx = new int[26];
13+
Arrays.fill(idxChain, -1);
14+
Arrays.fill(lastIdx, -1);
15+
for (int i = 0; i < arr.length; i++) {
16+
if (arr[i] == '*') {
17+
for (int j = 0; j < 26; j++) {
18+
if (lastIdx[j] != -1) {
19+
arr[lastIdx[j]] = '#';
20+
lastIdx[j] = idxChain[lastIdx[j]];
21+
break;
22+
}
23+
}
24+
arr[i] = '#';
25+
} else {
26+
idxChain[i] = lastIdx[arr[i] - 'a'];
27+
lastIdx[arr[i] - 'a'] = i;
28+
}
29+
}
30+
StringBuilder sb = new StringBuilder();
31+
for (char c : arr) {
32+
if (c != '#') {
33+
sb.append(c);
34+
}
35+
}
36+
return sb.toString();
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3170\. Lexicographically Minimum String After Removing Stars
2+
3+
Medium
4+
5+
You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.
6+
7+
While there is a `'*'`, do the following operation:
8+
9+
* Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its _left_. If there are several smallest characters, you can delete any of them.
10+
11+
Return the lexicographically smallest resulting string after removing all `'*'` characters.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "aaba\*"
16+
17+
**Output:** "aab"
18+
19+
**Explanation:**
20+
21+
We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "abc"
26+
27+
**Output:** "abc"
28+
29+
**Explanation:**
30+
31+
There is no `'*'` in the string.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 10<sup>5</sup></code>
36+
* `s` consists only of lowercase English letters and `'*'`.
37+
* The input is generated such that it is possible to delete all `'*'` characters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3171_find_subarray_with_bitwise_and_closest_to_k;
2+
3+
// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree
4+
// #2024_06_06_Time_10_ms_(98.04%)_Space_56.3_MB_(79.06%)
5+
6+
public class Solution {
7+
public int minimumDifference(int[] nums, int k) {
8+
int res = Integer.MAX_VALUE;
9+
for (int i = 0; i < nums.length; i++) {
10+
res = Math.min(res, Math.abs(nums[i] - k));
11+
for (int j = i - 1; j >= 0 && (nums[j] & nums[i]) != nums[j]; j--) {
12+
nums[j] &= nums[i];
13+
res = Math.min(res, Math.abs(nums[j] - k));
14+
}
15+
}
16+
return res;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3171\. Find Subarray With Bitwise AND Closest to K
2+
3+
Hard
4+
5+
You are given an array `nums` and an integer `k`. You need to find a subarray of `nums` such that the **absolute difference** between `k` and the bitwise `AND` of the subarray elements is as **small** as possible. In other words, select a subarray `nums[l..r]` such that `|k - (nums[l] AND nums[l + 1] ... AND nums[r])|` is minimum.
6+
7+
Return the **minimum** possible value of the absolute difference.
8+
9+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,4,5], k = 3
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The subarray `nums[2..3]` has `AND` value 4, which gives the minimum absolute difference `|3 - 4| = 1`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,1,2], k = 2
24+
25+
**Output:** 0
26+
27+
**Explanation:**
28+
29+
The subarray `nums[1..1]` has `AND` value 2, which gives the minimum absolute difference `|2 - 2| = 0`.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1], k = 10
34+
35+
**Output:** 9
36+
37+
**Explanation:**
38+
39+
There is a single subarray with `AND` value 1, which gives the minimum absolute difference `|10 - 1| = 9`.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
44+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
45+
* <code>1 <= k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room;
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 minimumChairs() {
11+
assertThat(new Solution().minimumChairs("EEEEEEE"), equalTo(7));
12+
}
13+
14+
@Test
15+
void minimumChairs2() {
16+
assertThat(new Solution().minimumChairs("ELELEEL"), equalTo(2));
17+
}
18+
19+
@Test
20+
void minimumChairs3() {
21+
assertThat(new Solution().minimumChairs("ELEELEELLL"), equalTo(3));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3101_3200.s3169_count_days_without_meetings;
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 countDays() {
11+
assertThat(new Solution().countDays(10, new int[][] {{5, 7}, {1, 3}, {9, 10}}), equalTo(2));
12+
}
13+
14+
@Test
15+
void countDays2() {
16+
assertThat(new Solution().countDays(5, new int[][] {{2, 4}, {1, 3}}), equalTo(1));
17+
}
18+
19+
@Test
20+
void countDays3() {
21+
assertThat(new Solution().countDays(6, new int[][] {{1, 6}}), equalTo(0));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3170_lexicographically_minimum_string_after_removing_stars;
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 clearStars() {
11+
assertThat(new Solution().clearStars("aaba*"), equalTo("aab"));
12+
}
13+
14+
@Test
15+
void clearStars2() {
16+
assertThat(new Solution().clearStars("abc"), equalTo("abc"));
17+
}
18+
}

0 commit comments

Comments
 (0)