Skip to content

Commit ae135b3

Browse files
authored
Added tasks 3206-3213
1 parent 77df0fb commit ae135b3

File tree

24 files changed

+797
-0
lines changed

24 files changed

+797
-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_11_Time_167_ms_(88.14%)_Space_38.3_MB_(23.73%)
4+
5+
class Solution {
6+
fun numberOfAlternatingGroups(colors: IntArray): Int {
7+
val n = colors.size
8+
var 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 (i in 1 until n - 1) {
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,24 @@
1+
package g3201_3300.s3207_maximum_points_after_enemy_battles
2+
3+
// #Medium #Array #Greedy #2024_07_11_Time_470_ms_(100.00%)_Space_62_MB_(95.56%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun maximumPoints(enemyEnergies: IntArray, currentEnergy: Int): Long {
9+
val n = enemyEnergies.size
10+
var min = enemyEnergies[0]
11+
for (i in 1 until n) {
12+
min = min(min.toDouble(), enemyEnergies[i].toDouble()).toInt()
13+
}
14+
if (currentEnergy == 0 || currentEnergy < min) {
15+
return 0
16+
}
17+
var sum = currentEnergy.toLong()
18+
for (i in n - 1 downTo 0) {
19+
sum += enemyEnergies[i].toLong()
20+
}
21+
sum -= min.toLong()
22+
return sum / min
23+
}
24+
}
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,43 @@
1+
package g3201_3300.s3208_alternating_groups_ii
2+
3+
// #Medium #Array #Sliding_Window #2024_07_11_Time_449_ms_(97.62%)_Space_59.6_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun numberOfAlternatingGroups(colors: IntArray, k: Int): Int {
9+
var i = 0
10+
var len = 0
11+
var total = 0
12+
while (i < colors.size - 1) {
13+
var j = i + 1
14+
if (colors[j] != colors[i]) {
15+
len = 2
16+
j++
17+
while (j < colors.size && colors[j] != colors[j - 1]) {
18+
j++
19+
len++
20+
}
21+
if (j == colors.size) {
22+
break
23+
}
24+
total += max(0, (len - k + 1))
25+
}
26+
i = j
27+
len = 0
28+
}
29+
if (colors[0] != colors[colors.size - 1]) {
30+
len = if (len == 0) 2 else len + 1
31+
var j = 1
32+
while (j < colors.size && colors[j] != colors[j - 1]) {
33+
j++
34+
len++
35+
}
36+
if (j >= k) {
37+
len -= (j - k + 1)
38+
}
39+
}
40+
total += max(0, (len - k + 1))
41+
return total
42+
}
43+
}
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,28 @@
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_11_Time_530_ms_(100.00%)_Space_58.2_MB_(76.19%)
5+
6+
class Solution {
7+
fun countSubarrays(nums: IntArray, k: Int): Long {
8+
var ans: Long = 0
9+
var left = 0
10+
var right = 0
11+
for (i in nums.indices) {
12+
val x = nums[i]
13+
var j = i - 1
14+
while (j >= 0 && (nums[j] and x) != nums[j]) {
15+
nums[j] = nums[j] and x
16+
j--
17+
}
18+
while (left <= i && nums[left] < k) {
19+
left++
20+
}
21+
while (right <= i && nums[right] <= k) {
22+
right++
23+
}
24+
ans += (right - left).toLong()
25+
}
26+
return ans
27+
}
28+
}
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,15 @@
1+
package g3201_3300.s3210_find_the_encrypted_string
2+
3+
// #Easy #String #2024_07_11_Time_170_ms_(62.69%)_Space_35.5_MB_(67.16%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun getEncryptedString(s: String, k: Int): String {
8+
var k = k
9+
val n = s.length
10+
k %= n
11+
val str = StringBuilder(s.substring(k, n))
12+
str.append(s.substring(0, k))
13+
return str.toString()
14+
}
15+
}
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)