Skip to content

Commit 9182a52

Browse files
authored
Added tasks 3206-3213
1 parent a0196ad commit 9182a52

File tree

24 files changed

+783
-0
lines changed

24 files changed

+783
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3201_3300.s3206_alternating_groups_i;
2+
3+
// #Easy #Array #Sliding_Window #2024_07_09_Time_1_ms_(97.24%)_Space_42.8_MB_(90.31%)
4+
5+
public class Solution {
6+
public int numberOfAlternatingGroups(int[] colors) {
7+
int n = colors.length;
8+
int count = 0;
9+
if (colors[n - 1] != colors[0] && colors[0] != colors[1]) {
10+
count++;
11+
}
12+
if (colors[n - 1] != colors[0] && colors[n - 1] != colors[n - 2]) {
13+
count++;
14+
}
15+
for (int i = 1; i < n - 1; i++) {
16+
if (colors[i] != colors[i - 1] && colors[i] != colors[i + 1]) {
17+
count++;
18+
}
19+
}
20+
return count;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3206\. Alternating Groups I
2+
3+
Easy
4+
5+
There is a circle of red and blue tiles. You are given an array of integers `colors`. The color of tile `i` is represented by `colors[i]`:
6+
7+
* `colors[i] == 0` means that tile `i` is **red**.
8+
* `colors[i] == 1` means that tile `i` is **blue**.
9+
10+
Every 3 contiguous tiles in the circle with **alternating** colors (the middle tile has a different color from its **left** and **right** tiles) is called an **alternating** group.
11+
12+
Return the number of **alternating** groups.
13+
14+
**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other.
15+
16+
**Example 1:**
17+
18+
**Input:** colors = [1,1,1]
19+
20+
**Output:** 0
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-53-171.png)
25+
26+
**Example 2:**
27+
28+
**Input:** colors = [0,1,0,0,1]
29+
30+
**Output:** 3
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-47-491.png)
35+
36+
Alternating groups:
37+
38+
**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-50-441.png)**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-48-211.png)**![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-49-351.png)**
39+
40+
**Constraints:**
41+
42+
* `3 <= colors.length <= 100`
43+
* `0 <= colors[i] <= 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3201_3300.s3207_maximum_points_after_enemy_battles;
2+
3+
// #Medium #Array #Greedy #2024_07_09_Time_1_ms_(100.00%)_Space_55.5_MB_(99.34%)
4+
5+
public class Solution {
6+
public long maximumPoints(int[] enemyEnergies, int currentEnergy) {
7+
int n = enemyEnergies.length;
8+
int min = enemyEnergies[0];
9+
for (int i = 1; i < n; i++) {
10+
min = Math.min(min, enemyEnergies[i]);
11+
}
12+
if (currentEnergy == 0 || currentEnergy < min) {
13+
return 0;
14+
}
15+
long sum = currentEnergy;
16+
for (int i = n - 1; i >= 0; i--) {
17+
sum += enemyEnergies[i];
18+
}
19+
sum -= min;
20+
return sum / min;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3207\. Maximum Points After Enemy Battles
2+
3+
Medium
4+
5+
You are given an integer array `enemyEnergies` denoting the energy values of various enemies.
6+
7+
You are also given an integer `currentEnergy` denoting the amount of energy you have initially.
8+
9+
You start with 0 points, and all the enemies are unmarked initially.
10+
11+
You can perform **either** of the following operations **zero** or multiple times to gain points:
12+
13+
* Choose an **unmarked** enemy, `i`, such that `currentEnergy >= enemyEnergies[i]`. By choosing this option:
14+
* You gain 1 point.
15+
* Your energy is reduced by the enemy's energy, i.e. `currentEnergy = currentEnergy - enemyEnergies[i]`.
16+
* If you have **at least** 1 point, you can choose an **unmarked** enemy, `i`. By choosing this option:
17+
* Your energy increases by the enemy's energy, i.e. `currentEnergy = currentEnergy + enemyEnergies[i]`.
18+
* The enemy `i` is **marked**.
19+
20+
Return an integer denoting the **maximum** points you can get in the end by optimally performing operations.
21+
22+
**Example 1:**
23+
24+
**Input:** enemyEnergies = [3,2,2], currentEnergy = 2
25+
26+
**Output:** 3
27+
28+
**Explanation:**
29+
30+
The following operations can be performed to get 3 points, which is the maximum:
31+
32+
* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 1`, and `currentEnergy = 0`.
33+
* Second operation on enemy 0: `currentEnergy` increases by 3, and enemy 0 is marked. So, `points = 1`, `currentEnergy = 3`, and marked enemies = `[0]`.
34+
* First operation on enemy 2: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 2`, `currentEnergy = 1`, and marked enemies = `[0]`.
35+
* Second operation on enemy 2: `currentEnergy` increases by 2, and enemy 2 is marked. So, `points = 2`, `currentEnergy = 3`, and marked enemies = `[0, 2]`.
36+
* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 3`, `currentEnergy = 1`, and marked enemies = `[0, 2]`.
37+
38+
**Example 2:**
39+
40+
**Input:** enemyEnergies = [2], currentEnergy = 10
41+
42+
**Output:** 5
43+
44+
**Explanation:**
45+
46+
Performing the first operation 5 times on enemy 0 results in the maximum number of points.
47+
48+
**Constraints:**
49+
50+
* <code>1 <= enemyEnergies.length <= 10<sup>5</sup></code>
51+
* <code>1 <= enemyEnergies[i] <= 10<sup>9</sup></code>
52+
* <code>0 <= currentEnergy <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3201_3300.s3208_alternating_groups_ii;
2+
3+
// #Medium #Array #Sliding_Window #2024_07_09_Time_2_ms_(99.02%)_Space_63.3_MB_(22.96%)
4+
5+
public class Solution {
6+
public int numberOfAlternatingGroups(int[] colors, int k) {
7+
int i = 0;
8+
int len = 0;
9+
int total = 0;
10+
while (i < colors.length - 1) {
11+
int j = i + 1;
12+
if (colors[j] != colors[i]) {
13+
len = 2;
14+
j++;
15+
while (j < colors.length && colors[j] != colors[j - 1]) {
16+
j++;
17+
len++;
18+
}
19+
if (j == colors.length) {
20+
break;
21+
}
22+
total += Math.max(0, (len - k + 1));
23+
}
24+
i = j;
25+
len = 0;
26+
}
27+
if (colors[0] != colors[colors.length - 1]) {
28+
// if(len == colors.length) {
29+
// return Math.max(0, colors.length);
30+
// }
31+
len = len == 0 ? 2 : len + 1;
32+
int j = 1;
33+
while (j < colors.length && colors[j] != colors[j - 1]) {
34+
j++;
35+
len++;
36+
}
37+
if (j >= k) {
38+
len -= (j - k + 1);
39+
}
40+
}
41+
total += Math.max(0, (len - k + 1));
42+
return total;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
3208\. Alternating Groups II
2+
3+
Medium
4+
5+
There is a circle of red and blue tiles. You are given an array of integers `colors` and an integer `k`. The color of tile `i` is represented by `colors[i]`:
6+
7+
* `colors[i] == 0` means that tile `i` is **red**.
8+
* `colors[i] == 1` means that tile `i` is **blue**.
9+
10+
An **alternating** group is every `k` contiguous tiles in the circle with **alternating** colors (each tile in the group except the first and last one has a different color from its **left** and **right** tiles).
11+
12+
Return the number of **alternating** groups.
13+
14+
**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other.
15+
16+
**Example 1:**
17+
18+
**Input:** colors = [0,1,0,1,0], k = 3
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png)**
25+
26+
Alternating groups:
27+
28+
![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png)![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png)![](https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png)
29+
30+
**Example 2:**
31+
32+
**Input:** colors = [0,1,0,0,1,0,1], k = 6
33+
34+
**Output:** 2
35+
36+
**Explanation:**
37+
38+
**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png)**
39+
40+
Alternating groups:
41+
42+
![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png)![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png)
43+
44+
**Example 3:**
45+
46+
**Input:** colors = [1,1,0,1], k = 4
47+
48+
**Output:** 0
49+
50+
**Explanation:**
51+
52+
![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png)
53+
54+
**Constraints:**
55+
56+
* <code>3 <= colors.length <= 10<sup>5</sup></code>
57+
* `0 <= colors[i] <= 1`
58+
* `3 <= k <= colors.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g3201_3300.s3209_number_of_subarrays_with_and_value_of_k;
2+
3+
// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree
4+
// #2024_07_09_Time_7_ms_(100.00%)_Space_62.9_MB_(11.74%)
5+
6+
public class Solution {
7+
public long countSubarrays(int[] nums, int k) {
8+
long ans = 0;
9+
int left = 0;
10+
int right = 0;
11+
for (int i = 0; i < nums.length; i++) {
12+
int x = nums[i];
13+
for (int j = i - 1; j >= 0 && (nums[j] & x) != nums[j]; j--) {
14+
nums[j] &= x;
15+
}
16+
while (left <= i && nums[left] < k) {
17+
left++;
18+
}
19+
while (right <= i && nums[right] <= k) {
20+
right++;
21+
}
22+
ans += right - left;
23+
}
24+
return ans;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3209\. Number of Subarrays With AND Value of K
2+
3+
Hard
4+
5+
Given an array of integers `nums` and an integer `k`, return the number of subarrays of `nums` where the bitwise `AND` of the elements of the subarray equals `k`.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,1,1], k = 1
10+
11+
**Output:** 6
12+
13+
**Explanation:**
14+
15+
All subarrays contain only 1's.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [1,1,2], k = 1
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
Subarrays having an `AND` value of 1 are: <code>[<ins>**1**</ins>,1,2]</code>, <code>[1,<ins>**1**</ins>,2]</code>, <code>[<ins>**1,1**</ins>,2]</code>.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [1,2,3], k = 2
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
Subarrays having an `AND` value of 2 are: <code>[1,**<ins>2</ins>**,3]</code>, <code>[1,<ins>**2,3**</ins>]</code>.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>0 <= nums[i], k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g3201_3300.s3210_find_the_encrypted_string;
2+
3+
// #Easy #String #2024_07_09_Time_1_ms_(100.00%)_Space_42.8_MB_(34.96%)
4+
5+
public class Solution {
6+
public String getEncryptedString(String s, int k) {
7+
int n = s.length();
8+
k = k % n;
9+
StringBuilder str = new StringBuilder(s.substring(k, n));
10+
str.append(s.substring(0, k));
11+
return str.toString();
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3210\. Find the Encrypted String
2+
3+
Easy
4+
5+
You are given a string `s` and an integer `k`. Encrypt the string using the following algorithm:
6+
7+
* For each character `c` in `s`, replace `c` with the <code>k<sup>th</sup></code> character after `c` in the string (in a cyclic manner).
8+
9+
Return the _encrypted string_.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "dart", k = 3
14+
15+
**Output:** "tdar"
16+
17+
**Explanation:**
18+
19+
* For `i = 0`, the 3<sup>rd</sup> character after `'d'` is `'t'`.
20+
* For `i = 1`, the 3<sup>rd</sup> character after `'a'` is `'d'`.
21+
* For `i = 2`, the 3<sup>rd</sup> character after `'r'` is `'a'`.
22+
* For `i = 3`, the 3<sup>rd</sup> character after `'t'` is `'r'`.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "aaa", k = 1
27+
28+
**Output:** "aaa"
29+
30+
**Explanation:**
31+
32+
As all the characters are the same, the encrypted string will also be the same.
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 100`
37+
* <code>1 <= k <= 10<sup>4</sup></code>
38+
* `s` consists only of lowercase English letters.

0 commit comments

Comments
 (0)