Skip to content

Added tasks 3206-3213 #1784

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 4 commits into from
Jul 9, 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
22 changes: 22 additions & 0 deletions src/main/java/g3201_3300/s3206_alternating_groups_i/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3201_3300.s3206_alternating_groups_i;

// #Easy #Array #Sliding_Window #2024_07_09_Time_1_ms_(97.24%)_Space_42.8_MB_(90.31%)

public class Solution {
public int numberOfAlternatingGroups(int[] colors) {
int n = colors.length;
int count = 0;
if (colors[n - 1] != colors[0] && colors[0] != colors[1]) {
count++;
}
if (colors[n - 1] != colors[0] && colors[n - 1] != colors[n - 2]) {
count++;
}
for (int i = 1; i < n - 1; i++) {
if (colors[i] != colors[i - 1] && colors[i] != colors[i + 1]) {
count++;
}
}
return count;
}
}
43 changes: 43 additions & 0 deletions src/main/java/g3201_3300/s3206_alternating_groups_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
3206\. Alternating Groups I

Easy

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]`:

* `colors[i] == 0` means that tile `i` is **red**.
* `colors[i] == 1` means that tile `i` is **blue**.

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.

Return the number of **alternating** groups.

**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other.

**Example 1:**

**Input:** colors = [1,1,1]

**Output:** 0

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-53-171.png)

**Example 2:**

**Input:** colors = [0,1,0,0,1]

**Output:** 3

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-47-491.png)

Alternating groups:

**![](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)**

**Constraints:**

* `3 <= colors.length <= 100`
* `0 <= colors[i] <= 1`
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g3201_3300.s3207_maximum_points_after_enemy_battles;

// #Medium #Array #Greedy #2024_07_09_Time_1_ms_(100.00%)_Space_55.5_MB_(99.34%)

public class Solution {
public long maximumPoints(int[] enemyEnergies, int currentEnergy) {
int n = enemyEnergies.length;
int min = enemyEnergies[0];
for (int i = 1; i < n; i++) {
min = Math.min(min, enemyEnergies[i]);
}
if (currentEnergy == 0 || currentEnergy < min) {
return 0;
}
long sum = currentEnergy;
for (int i = n - 1; i >= 0; i--) {
sum += enemyEnergies[i];
}
sum -= min;
return sum / min;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
3207\. Maximum Points After Enemy Battles

Medium

You are given an integer array `enemyEnergies` denoting the energy values of various enemies.

You are also given an integer `currentEnergy` denoting the amount of energy you have initially.

You start with 0 points, and all the enemies are unmarked initially.

You can perform **either** of the following operations **zero** or multiple times to gain points:

* Choose an **unmarked** enemy, `i`, such that `currentEnergy >= enemyEnergies[i]`. By choosing this option:
* You gain 1 point.
* Your energy is reduced by the enemy's energy, i.e. `currentEnergy = currentEnergy - enemyEnergies[i]`.
* If you have **at least** 1 point, you can choose an **unmarked** enemy, `i`. By choosing this option:
* Your energy increases by the enemy's energy, i.e. `currentEnergy = currentEnergy + enemyEnergies[i]`.
* The enemy `i` is **marked**.

Return an integer denoting the **maximum** points you can get in the end by optimally performing operations.

**Example 1:**

**Input:** enemyEnergies = [3,2,2], currentEnergy = 2

**Output:** 3

**Explanation:**

The following operations can be performed to get 3 points, which is the maximum:

* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 1`, and `currentEnergy = 0`.
* Second operation on enemy 0: `currentEnergy` increases by 3, and enemy 0 is marked. So, `points = 1`, `currentEnergy = 3`, and marked enemies = `[0]`.
* First operation on enemy 2: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 2`, `currentEnergy = 1`, and marked enemies = `[0]`.
* Second operation on enemy 2: `currentEnergy` increases by 2, and enemy 2 is marked. So, `points = 2`, `currentEnergy = 3`, and marked enemies = `[0, 2]`.
* First operation on enemy 1: `points` increases by 1, and `currentEnergy` decreases by 2. So, `points = 3`, `currentEnergy = 1`, and marked enemies = `[0, 2]`.

**Example 2:**

**Input:** enemyEnergies = [2], currentEnergy = 10

**Output:** 5

**Explanation:**

Performing the first operation 5 times on enemy 0 results in the maximum number of points.

**Constraints:**

* <code>1 <= enemyEnergies.length <= 10<sup>5</sup></code>
* <code>1 <= enemyEnergies[i] <= 10<sup>9</sup></code>
* <code>0 <= currentEnergy <= 10<sup>9</sup></code>
44 changes: 44 additions & 0 deletions src/main/java/g3201_3300/s3208_alternating_groups_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g3201_3300.s3208_alternating_groups_ii;

// #Medium #Array #Sliding_Window #2024_07_09_Time_2_ms_(99.02%)_Space_63.3_MB_(22.96%)

public class Solution {
public int numberOfAlternatingGroups(int[] colors, int k) {
int i = 0;
int len = 0;
int total = 0;
while (i < colors.length - 1) {
int j = i + 1;
if (colors[j] != colors[i]) {
len = 2;
j++;
while (j < colors.length && colors[j] != colors[j - 1]) {
j++;
len++;
}
if (j == colors.length) {
break;
}
total += Math.max(0, (len - k + 1));
}
i = j;
len = 0;
}
if (colors[0] != colors[colors.length - 1]) {
// if(len == colors.length) {
// return Math.max(0, colors.length);
// }
len = len == 0 ? 2 : len + 1;
int j = 1;
while (j < colors.length && colors[j] != colors[j - 1]) {
j++;
len++;
}
if (j >= k) {
len -= (j - k + 1);
}
}
total += Math.max(0, (len - k + 1));
return total;
}
}
58 changes: 58 additions & 0 deletions src/main/java/g3201_3300/s3208_alternating_groups_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
3208\. Alternating Groups II

Medium

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]`:

* `colors[i] == 0` means that tile `i` is **red**.
* `colors[i] == 1` means that tile `i` is **blue**.

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

Return the number of **alternating** groups.

**Note** that since `colors` represents a **circle**, the **first** and the **last** tiles are considered to be next to each other.

**Example 1:**

**Input:** colors = [0,1,0,1,0], k = 3

**Output:** 3

**Explanation:**

**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png)**

Alternating groups:

![](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)

**Example 2:**

**Input:** colors = [0,1,0,0,1,0,1], k = 6

**Output:** 2

**Explanation:**

**![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png)**

Alternating groups:

![](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)

**Example 3:**

**Input:** colors = [1,1,0,1], k = 4

**Output:** 0

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png)

**Constraints:**

* <code>3 <= colors.length <= 10<sup>5</sup></code>
* `0 <= colors[i] <= 1`
* `3 <= k <= colors.length`
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g3201_3300.s3209_number_of_subarrays_with_and_value_of_k;

// #Hard #Array #Binary_Search #Bit_Manipulation #Segment_Tree
// #2024_07_09_Time_7_ms_(100.00%)_Space_62.9_MB_(11.74%)

public class Solution {
public long countSubarrays(int[] nums, int k) {
long ans = 0;
int left = 0;
int right = 0;
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
for (int j = i - 1; j >= 0 && (nums[j] & x) != nums[j]; j--) {
nums[j] &= x;
}
while (left <= i && nums[left] < k) {
left++;
}
while (right <= i && nums[right] <= k) {
right++;
}
ans += right - left;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
3209\. Number of Subarrays With AND Value of K

Hard

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

**Example 1:**

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

**Output:** 6

**Explanation:**

All subarrays contain only 1's.

**Example 2:**

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

**Output:** 3

**Explanation:**

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

**Example 3:**

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

**Output:** 2

**Explanation:**

Subarrays having an `AND` value of 2 are: <code>[1,**<ins>2</ins>**,3]</code>, <code>[1,<ins>**2,3**</ins>]</code>.

**Constraints:**

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

// #Easy #String #2024_07_09_Time_1_ms_(100.00%)_Space_42.8_MB_(34.96%)

public class Solution {
public String getEncryptedString(String s, int k) {
int n = s.length();
k = k % n;
StringBuilder str = new StringBuilder(s.substring(k, n));
str.append(s.substring(0, k));
return str.toString();
}
}
38 changes: 38 additions & 0 deletions src/main/java/g3201_3300/s3210_find_the_encrypted_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3210\. Find the Encrypted String

Easy

You are given a string `s` and an integer `k`. Encrypt the string using the following algorithm:

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

Return the _encrypted string_.

**Example 1:**

**Input:** s = "dart", k = 3

**Output:** "tdar"

**Explanation:**

* For `i = 0`, the 3<sup>rd</sup> character after `'d'` is `'t'`.
* For `i = 1`, the 3<sup>rd</sup> character after `'a'` is `'d'`.
* For `i = 2`, the 3<sup>rd</sup> character after `'r'` is `'a'`.
* For `i = 3`, the 3<sup>rd</sup> character after `'t'` is `'r'`.

**Example 2:**

**Input:** s = "aaa", k = 1

**Output:** "aaa"

**Explanation:**

As all the characters are the same, the encrypted string will also be the same.

**Constraints:**

* `1 <= s.length <= 100`
* <code>1 <= k <= 10<sup>4</sup></code>
* `s` consists only of lowercase English letters.
Loading
Loading