Skip to content

Commit 4b1637f

Browse files
authored
Added tasks 1052, 1053, 1054.
1 parent a07bab4 commit 4b1637f

File tree

9 files changed

+261
-0
lines changed

9 files changed

+261
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g1001_1100.s1052_grumpy_bookstore_owner;
2+
3+
// #Medium #Array #Sliding_Window #2022_02_28_Time_4_ms_(70.26%)_Space_54.4_MB_(27.35%)
4+
5+
public class Solution {
6+
public int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
7+
// storing numbers of customers who faced grumpy owner till ith minute.
8+
int[] grumpySum = new int[grumpy.length];
9+
int ans = 0;
10+
if (grumpy[0] == 1) {
11+
grumpySum[0] = customers[0];
12+
} else {
13+
ans += customers[0];
14+
}
15+
for (int i = 1; i < grumpy.length; i++) {
16+
if (grumpy[i] == 1) {
17+
grumpySum[i] = grumpySum[i - 1] + customers[i];
18+
} else {
19+
grumpySum[i] = grumpySum[i - 1];
20+
ans += customers[i];
21+
}
22+
}
23+
// calculating max number of customers who faced grumpy owner in a window of size 'minutes'.
24+
int max = 0;
25+
for (int i = 0; i <= customers.length - minutes; i++) {
26+
if (i == 0) {
27+
max = Math.max(max, grumpySum[i + minutes - 1]);
28+
} else {
29+
max = Math.max(max, grumpySum[i + minutes - 1] - grumpySum[i - 1]);
30+
}
31+
}
32+
// making the owner non-grumpy in that max window and adding the number of customers who do
33+
// not face the grumpy customers.
34+
ans += max;
35+
return ans;
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1052\. Grumpy Bookstore Owner
2+
3+
Medium
4+
5+
There is a bookstore owner that has a store open for `n` minutes. Every minute, some number of customers enter the store. You are given an integer array `customers` of length `n` where `customers[i]` is the number of the customer that enters the store at the start of the <code>i<sup>th</sup></code> minute and all those customers leave after the end of that minute.
6+
7+
On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where `grumpy[i]` is `1` if the bookstore owner is grumpy during the <code>i<sup>th</sup></code> minute, and is `0` otherwise.
8+
9+
When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
10+
11+
The bookstore owner knows a secret technique to keep themselves not grumpy for `minutes` consecutive minutes, but can only use it once.
12+
13+
Return _the maximum number of customers that can be satisfied throughout the day_.
14+
15+
**Example 1:**
16+
17+
**Input:** customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
18+
19+
**Output:** 16
20+
21+
**Explanation:** The bookstore owner keeps themselves not grumpy for the last 3 minutes.
22+
23+
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
24+
25+
**Example 2:**
26+
27+
**Input:** customers = [1], grumpy = [0], minutes = 1
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `n == customers.length == grumpy.length`
34+
* <code>1 <= minutes <= n <= 2 * 10<sup>4</sup></code>
35+
* `0 <= customers[i] <= 1000`
36+
* `grumpy[i]` is either `0` or `1`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1001_1100.s1053_previous_permutation_with_one_swap;
2+
3+
// #Medium #Array #Greedy #2022_02_28_Time_0_ms_(100.00%)_Space_56.1_MB_(27.21%)
4+
5+
public class Solution {
6+
public int[] prevPermOpt1(int[] arr) {
7+
for (int i = arr.length - 1; i >= 0; i--) {
8+
int diff = Integer.MAX_VALUE;
9+
int index = i;
10+
for (int j = i + 1; j < arr.length; j++) {
11+
if (arr[i] - arr[j] > 0 && diff > arr[i] - arr[j]) {
12+
diff = arr[i] - arr[j];
13+
index = j;
14+
}
15+
}
16+
if (diff != Integer.MAX_VALUE) {
17+
int temp = arr[i];
18+
arr[i] = arr[index];
19+
arr[index] = temp;
20+
break;
21+
}
22+
}
23+
return arr;
24+
}
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1053\. Previous Permutation With One Swap
2+
3+
Medium
4+
5+
Given an array of positive integers `arr` (not necessarily distinct), return _the lexicographically largest permutation that is smaller than_ `arr`, that can be **made with exactly one swap** (A _swap_ exchanges the positions of two numbers `arr[i]` and `arr[j]`). If it cannot be done, then return the same array.
6+
7+
**Example 1:**
8+
9+
**Input:** arr = [3,2,1]
10+
11+
**Output:** [3,1,2]
12+
13+
**Explanation:** Swapping 2 and 1.
14+
15+
**Example 2:**
16+
17+
**Input:** arr = [1,1,5]
18+
19+
**Output:** [1,1,5]
20+
21+
**Explanation:** This is already the smallest permutation.
22+
23+
**Example 3:**
24+
25+
**Input:** arr = [1,9,4,6,7]
26+
27+
**Output:** [1,7,4,6,9]
28+
29+
**Explanation:** Swapping 9 and 7.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= arr.length <= 10<sup>4</sup></code>
34+
* <code>1 <= arr[i] <= 10<sup>4</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1001_1100.s1054_distant_barcodes;
2+
3+
// #Medium #Array #Hash_Table #Sorting #Greedy #Heap_Priority_Queue #Counting
4+
// #2022_02_28_Time_45_ms_(64.21%)_Space_69.1_MB_(71.24%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class Solution {
12+
public int[] rearrangeBarcodes(int[] barcodes) {
13+
Map<Integer, Integer> cnt = new HashMap<>();
14+
for (int i : barcodes) {
15+
cnt.put(i, cnt.getOrDefault(i, 0) + 1);
16+
}
17+
List<Map.Entry<Integer, Integer>> list = new ArrayList<>(cnt.entrySet());
18+
list.sort(Map.Entry.<Integer, Integer>comparingByValue().reversed());
19+
int l = barcodes.length;
20+
int i = 0;
21+
int[] res = new int[l];
22+
for (Map.Entry<Integer, Integer> e : list) {
23+
int time = e.getValue();
24+
while (time-- > 0) {
25+
res[i] = e.getKey();
26+
i += 2;
27+
if (i >= barcodes.length) {
28+
i = 1;
29+
}
30+
}
31+
}
32+
return res;
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
1054\. Distant Barcodes
2+
3+
Medium
4+
5+
In a warehouse, there is a row of barcodes, where the <code>i<sup>th</sup></code> barcode is `barcodes[i]`.
6+
7+
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
8+
9+
**Example 1:**
10+
11+
**Input:** barcodes = [1,1,1,2,2,2]
12+
13+
**Output:** [2,1,2,1,2,1]
14+
15+
**Example 2:**
16+
17+
**Input:** barcodes = [1,1,1,1,2,2,3,3]
18+
19+
**Output:** [1,3,1,3,1,2,1,2]
20+
21+
**Constraints:**
22+
23+
* `1 <= barcodes.length <= 10000`
24+
* `1 <= barcodes[i] <= 10000`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g1001_1100.s1052_grumpy_bookstore_owner;
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 maxSatisfied() {
11+
assertThat(
12+
new Solution()
13+
.maxSatisfied(
14+
new int[] {1, 0, 1, 2, 1, 1, 7, 5},
15+
new int[] {0, 1, 0, 1, 0, 1, 0, 1},
16+
3),
17+
equalTo(16));
18+
}
19+
20+
@Test
21+
void maxSatisfied2() {
22+
assertThat(new Solution().maxSatisfied(new int[] {1}, new int[] {0}, 3), equalTo(1));
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1001_1100.s1053_previous_permutation_with_one_swap;
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 prevPermOpt1() {
11+
assertThat(new Solution().prevPermOpt1(new int[] {3, 2, 1}), equalTo(new int[] {3, 1, 2}));
12+
}
13+
14+
@Test
15+
void prevPermOpt2() {
16+
assertThat(new Solution().prevPermOpt1(new int[] {1, 1, 5}), equalTo(new int[] {1, 1, 5}));
17+
}
18+
19+
@Test
20+
void prevPermOpt3() {
21+
assertThat(
22+
new Solution().prevPermOpt1(new int[] {1, 9, 4, 6, 7}),
23+
equalTo(new int[] {1, 7, 4, 6, 9}));
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1054_distant_barcodes;
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 rearrangeBarcodes() {
11+
assertThat(
12+
new Solution().rearrangeBarcodes(new int[] {1, 1, 1, 2, 2, 2}),
13+
equalTo(new int[] {1, 2, 1, 2, 1, 2}));
14+
}
15+
16+
@Test
17+
void rearrangeBarcodes2() {
18+
assertThat(
19+
new Solution().rearrangeBarcodes(new int[] {1, 1, 1, 1, 2, 2, 3, 3}),
20+
equalTo(new int[] {1, 2, 1, 2, 1, 3, 1, 3}));
21+
}
22+
}

0 commit comments

Comments
 (0)