Skip to content

Added tasks 3168-3171 #1766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room;

// #Easy #String #Simulation #2024_06_06_Time_1_ms_(100.00%)_Space_41.9_MB_(67.53%)

public class Solution {
public int minimumChairs(String s) {
int count = 0;
int ans = Integer.MIN_VALUE;
for (char ch : s.toCharArray()) {
if (ch == 'E') {
count++;
ans = Math.max(ans, count);
} else {
count--;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
3168\. Minimum Number of Chairs in a Waiting Room

Easy

You are given a string `s`. Simulate events at each second `i`:

* If `s[i] == 'E'`, a person enters the waiting room and takes one of the chairs in it.
* If `s[i] == 'L'`, a person leaves the waiting room, freeing up a chair.

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**.

**Example 1:**

**Input:** s = "EEEEEEE"

**Output:** 7

**Explanation:**

After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.

**Example 2:**

**Input:** s = "ELELEEL"

**Output:** 2

**Explanation:**

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.

| Second | Event | People in the Waiting Room | Available Chairs |
|--------|-------|----------------------------|------------------|
| 0 | Enter | 1 | 1 |
| 1 | Leave | 0 | 2 |
| 2 | Enter | 1 | 1 |
| 3 | Leave | 0 | 2 |
| 4 | Enter | 1 | 1 |
| 5 | Enter | 2 | 0 |
| 6 | Leave | 1 | 1 |

**Example 3:**

**Input:** s = "ELEELEELLL"

**Output:** 3

**Explanation:**

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.

| Second | Event | People in the Waiting Room | Available Chairs |
|--------|-------|----------------------------|------------------|
| 0 | Enter | 1 | 2 |
| 1 | Leave | 0 | 3 |
| 2 | Enter | 1 | 2 |
| 3 | Enter | 2 | 1 |
| 4 | Leave | 1 | 2 |
| 5 | Enter | 2 | 1 |
| 6 | Enter | 3 | 0 |
| 7 | Leave | 2 | 1 |
| 8 | Leave | 1 | 2 |
| 9 | Leave | 0 | 3 |

**Constraints:**

* `1 <= s.length <= 50`
* `s` consists only of the letters `'E'` and `'L'`.
* `s` represents a valid sequence of entries and exits.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g3101_3200.s3169_count_days_without_meetings;

// #Medium #Array #Sorting #2024_06_06_Time_11_ms_(99.96%)_Space_113.7_MB_(5.10%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public int countDays(int days, int[][] meetings) {
List<int[]> availableDays = new ArrayList<>();
availableDays.add(new int[] {1, days});
// Iterate through each meeting
for (int[] meeting : meetings) {
int start = meeting[0];
int end = meeting[1];
List<int[]> newAvailableDays = new ArrayList<>();
// Iterate through available days and split the intervals
for (int[] interval : availableDays) {
if (start > interval[1] || end < interval[0]) {
// No overlap, keep the interval
newAvailableDays.add(interval);
} else {
// Overlap, split the interval
if (interval[0] < start) {
newAvailableDays.add(new int[] {interval[0], start - 1});
}
if (interval[1] > end) {
newAvailableDays.add(new int[] {end + 1, interval[1]});
}
}
}
availableDays = newAvailableDays;
}
// Count the remaining available days
int availableDaysCount = 0;
for (int[] interval : availableDays) {
availableDaysCount += interval[1] - interval[0] + 1;
}
return availableDaysCount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3169\. Count Days Without Meetings

Medium

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).

Return the count of days when the employee is available for work but no meetings are scheduled.

**Note:** The meetings may overlap.

**Example 1:**

**Input:** days = 10, meetings = [[5,7],[1,3],[9,10]]

**Output:** 2

**Explanation:**

There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.

**Example 2:**

**Input:** days = 5, meetings = [[2,4],[1,3]]

**Output:** 1

**Explanation:**

There is no meeting scheduled on the 5<sup>th</sup> day.

**Example 3:**

**Input:** days = 6, meetings = [[1,6]]

**Output:** 0

**Explanation:**

Meetings are scheduled for all working days.

**Constraints:**

* <code>1 <= days <= 10<sup>9</sup></code>
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
* `meetings[i].length == 2`
* `1 <= meetings[i][0] <= meetings[i][1] <= days`
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g3101_3200.s3170_lexicographically_minimum_string_after_removing_stars;

// #Medium #String #Hash_Table #Greedy #Stack #Heap_Priority_Queue
// #2024_06_06_Time_29_ms_(99.93%)_Space_45.6_MB_(92.80%)

import java.util.Arrays;

public class Solution {
public String clearStars(String s) {
char[] arr = s.toCharArray();
int[] idxChain = new int[arr.length];
int[] lastIdx = new int[26];
Arrays.fill(idxChain, -1);
Arrays.fill(lastIdx, -1);
for (int i = 0; i < arr.length; i++) {
if (arr[i] == '*') {
for (int j = 0; j < 26; j++) {
if (lastIdx[j] != -1) {
arr[lastIdx[j]] = '#';
lastIdx[j] = idxChain[lastIdx[j]];
break;
}
}
arr[i] = '#';
} else {
idxChain[i] = lastIdx[arr[i] - 'a'];
lastIdx[arr[i] - 'a'] = i;
}
}
StringBuilder sb = new StringBuilder();
for (char c : arr) {
if (c != '#') {
sb.append(c);
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3170\. Lexicographically Minimum String After Removing Stars

Medium

You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.

While there is a `'*'`, do the following operation:

* Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its _left_. If there are several smallest characters, you can delete any of them.

Return the lexicographically smallest resulting string after removing all `'*'` characters.

**Example 1:**

**Input:** s = "aaba\*"

**Output:** "aab"

**Explanation:**

We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest.

**Example 2:**

**Input:** s = "abc"

**Output:** "abc"

**Explanation:**

There is no `'*'` in the string.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists only of lowercase English letters and `'*'`.
* The input is generated such that it is possible to delete all `'*'` characters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3171_find_subarray_with_bitwise_and_closest_to_k;

// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree
// #2024_06_06_Time_10_ms_(98.04%)_Space_56.3_MB_(79.06%)

public class Solution {
public int minimumDifference(int[] nums, int k) {
int res = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++) {
res = Math.min(res, Math.abs(nums[i] - k));
for (int j = i - 1; j >= 0 && (nums[j] & nums[i]) != nums[j]; j--) {
nums[j] &= nums[i];
res = Math.min(res, Math.abs(nums[j] - k));
}
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3171\. Find Subarray With Bitwise AND Closest to K

Hard

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.

Return the **minimum** possible value of the absolute difference.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,4,5], k = 3

**Output:** 1

**Explanation:**

The subarray `nums[2..3]` has `AND` value 4, which gives the minimum absolute difference `|3 - 4| = 1`.

**Example 2:**

**Input:** nums = [1,2,1,2], k = 2

**Output:** 0

**Explanation:**

The subarray `nums[1..1]` has `AND` value 2, which gives the minimum absolute difference `|2 - 2| = 0`.

**Example 3:**

**Input:** nums = [1], k = 10

**Output:** 9

**Explanation:**

There is a single subarray with `AND` value 1, which gives the minimum absolute difference `|10 - 1| = 9`.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void minimumChairs() {
assertThat(new Solution().minimumChairs("EEEEEEE"), equalTo(7));
}

@Test
void minimumChairs2() {
assertThat(new Solution().minimumChairs("ELELEEL"), equalTo(2));
}

@Test
void minimumChairs3() {
assertThat(new Solution().minimumChairs("ELEELEELLL"), equalTo(3));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g3101_3200.s3169_count_days_without_meetings;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countDays() {
assertThat(new Solution().countDays(10, new int[][] {{5, 7}, {1, 3}, {9, 10}}), equalTo(2));
}

@Test
void countDays2() {
assertThat(new Solution().countDays(5, new int[][] {{2, 4}, {1, 3}}), equalTo(1));
}

@Test
void countDays3() {
assertThat(new Solution().countDays(6, new int[][] {{1, 6}}), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g3101_3200.s3170_lexicographically_minimum_string_after_removing_stars;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void clearStars() {
assertThat(new Solution().clearStars("aaba*"), equalTo("aab"));
}

@Test
void clearStars2() {
assertThat(new Solution().clearStars("abc"), equalTo("abc"));
}
}
Loading
Loading