Skip to content

Commit 9b2dd27

Browse files
authored
Added tasks 3168-3171
1 parent 71cb454 commit 9b2dd27

File tree

12 files changed

+407
-0
lines changed

12 files changed

+407
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room
2+
3+
// #Easy #String #Simulation #2024_06_08_Time_148_ms_(86.52%)_Space_35_MB_(8.99%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun minimumChairs(s: String): Int {
9+
var count = 0
10+
var ans = Int.MIN_VALUE
11+
for (ch in s.toCharArray()) {
12+
if (ch == 'E') {
13+
count++
14+
ans = max(ans, count)
15+
} else {
16+
count--
17+
}
18+
}
19+
return ans
20+
}
21+
}
Lines changed: 69 additions & 0 deletions
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.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3101_3200.s3169_count_days_without_meetings
2+
3+
// #Medium #Array #Sorting #2024_06_08_Time_733_ms_(97.59%)_Space_101.5_MB_(93.98%)
4+
5+
class Solution {
6+
fun countDays(days: Int, meetings: Array<IntArray>): Int {
7+
var availableDays: MutableList<IntArray> = ArrayList()
8+
availableDays.add(intArrayOf(1, days))
9+
// Iterate through each meeting
10+
for (meeting in meetings) {
11+
val start = meeting[0]
12+
val end = meeting[1]
13+
val newAvailableDays: MutableList<IntArray> = ArrayList()
14+
// Iterate through available days and split the intervals
15+
for (interval in availableDays) {
16+
if (start > interval[1] || end < interval[0]) {
17+
// No overlap, keep the interval
18+
newAvailableDays.add(interval)
19+
} else {
20+
// Overlap, split the interval
21+
if (interval[0] < start) {
22+
newAvailableDays.add(intArrayOf(interval[0], start - 1))
23+
}
24+
if (interval[1] > end) {
25+
newAvailableDays.add(intArrayOf(end + 1, interval[1]))
26+
}
27+
}
28+
}
29+
availableDays = newAvailableDays
30+
}
31+
// Count the remaining available days
32+
var availableDaysCount = 0
33+
for (interval in availableDays) {
34+
availableDaysCount += interval[1] - interval[0] + 1
35+
}
36+
return availableDaysCount
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
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`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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_08_Time_316_ms_(100.00%)_Space_40.9_MB_(89.58%)
5+
6+
class Solution {
7+
fun clearStars(s: String): String {
8+
val arr = s.toCharArray()
9+
val idxChain = IntArray(arr.size)
10+
val lastIdx = IntArray(26)
11+
idxChain.fill(-1)
12+
lastIdx.fill(-1)
13+
for (i in arr.indices) {
14+
if (arr[i] == '*') {
15+
for (j in 0..25) {
16+
if (lastIdx[j] != -1) {
17+
arr[lastIdx[j]] = '#'
18+
lastIdx[j] = idxChain[lastIdx[j]]
19+
break
20+
}
21+
}
22+
arr[i] = '#'
23+
} else {
24+
idxChain[i] = lastIdx[arr[i].code - 'a'.code]
25+
lastIdx[arr[i].code - 'a'.code] = i
26+
}
27+
}
28+
val sb = StringBuilder()
29+
for (c in arr) {
30+
if (c != '#') {
31+
sb.append(c)
32+
}
33+
}
34+
return sb.toString()
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_08_Time_520_ms_(100.00%)_Space_63.6_MB_(96.15%)
5+
6+
import kotlin.math.abs
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun minimumDifference(nums: IntArray, k: Int): Int {
11+
var res = Int.MAX_VALUE
12+
for (i in nums.indices) {
13+
res = min(res, abs((nums[i] - k)))
14+
var j = i - 1
15+
while (j >= 0 && (nums[j] and nums[i]) != nums[j]) {
16+
nums[j] = nums[j] and nums[i]
17+
res = min(res, abs((nums[j] - k)))
18+
j--
19+
}
20+
}
21+
return res
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
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>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3101_3200.s3168_minimum_number_of_chairs_in_a_waiting_room
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun minimumChairs() {
10+
assertThat(Solution().minimumChairs("EEEEEEE"), equalTo(7))
11+
}
12+
13+
@Test
14+
fun minimumChairs2() {
15+
assertThat(Solution().minimumChairs("ELELEEL"), equalTo(2))
16+
}
17+
18+
@Test
19+
fun minimumChairs3() {
20+
assertThat(Solution().minimumChairs("ELEELEELLL"), equalTo(3))
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3101_3200.s3169_count_days_without_meetings
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun countDays() {
10+
assertThat(
11+
Solution().countDays(
12+
10,
13+
arrayOf(intArrayOf(5, 7), intArrayOf(1, 3), intArrayOf(9, 10))
14+
),
15+
equalTo(2)
16+
)
17+
}
18+
19+
@Test
20+
fun countDays2() {
21+
assertThat(
22+
Solution().countDays(5, arrayOf(intArrayOf(2, 4), intArrayOf(1, 3))),
23+
equalTo(1)
24+
)
25+
}
26+
27+
@Test
28+
fun countDays3() {
29+
assertThat(Solution().countDays(6, arrayOf(intArrayOf(1, 6))), equalTo(0))
30+
}
31+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3170_lexicographically_minimum_string_after_removing_stars
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun clearStars() {
10+
assertThat(Solution().clearStars("aaba*"), equalTo("aab"))
11+
}
12+
13+
@Test
14+
fun clearStars2() {
15+
assertThat(Solution().clearStars("abc"), equalTo("abc"))
16+
}
17+
}

0 commit comments

Comments
 (0)