Skip to content

Commit fa5e761

Browse files
authored
Added tasks 2763-2768
1 parent 644288e commit fa5e761

File tree

15 files changed

+478
-0
lines changed

15 files changed

+478
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2701_2800.s2763_sum_of_imbalance_numbers_of_all_subarrays
2+
3+
// #Hard #Array #Hash_Table #Ordered_Set #2023_08_11_Time_417_ms_(95.24%)_Space_40.2_MB_(95.24%)
4+
5+
class Solution {
6+
fun sumImbalanceNumbers(nums: IntArray): Int {
7+
val n = nums.size
8+
var ans = 0
9+
for (i in 0 until n) {
10+
val s: MutableSet<Int> = HashSet()
11+
var curr = 0
12+
for (j in i until n) {
13+
val x = nums[j]
14+
if (s.contains(x)) {
15+
// do nothing
16+
} else if (s.contains(x - 1) && s.contains(x + 1)) {
17+
curr--
18+
} else if (!s.contains(x - 1) && !s.contains(x + 1) && s.isNotEmpty()) {
19+
curr++
20+
}
21+
s.add(x)
22+
ans += curr
23+
}
24+
}
25+
return ans
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2763\. Sum of Imbalance Numbers of All Subarrays
2+
3+
Hard
4+
5+
The **imbalance number** of a **0-indexed** integer array `arr` of length `n` is defined as the number of indices in `sarr = sorted(arr)` such that:
6+
7+
* `0 <= i < n - 1`, and
8+
* `sarr[i+1] - sarr[i] > 1`
9+
10+
Here, `sorted(arr)` is the function that returns the sorted version of `arr`.
11+
12+
Given a **0-indexed** integer array `nums`, return _the **sum of imbalance numbers** of all its **subarrays**_.
13+
14+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,3,1,4]
19+
20+
**Output:** 3
21+
22+
**Explanation:** There are 3 subarrays with non-zero imbalance numbers:
23+
- Subarray [3, 1] with an imbalance number of 1.
24+
- Subarray [3, 1, 4] with an imbalance number of 1.
25+
- Subarray [1, 4] with an imbalance number of 1.
26+
27+
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [1,3,3,3,5]
32+
33+
**Output:** 8
34+
35+
**Explanation:** There are 7 subarrays with non-zero imbalance numbers:
36+
- Subarray [1, 3] with an imbalance number of 1.
37+
- Subarray [1, 3, 3] with an imbalance number of 1.
38+
- Subarray [1, 3, 3, 3] with an imbalance number of 1.
39+
- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2.
40+
- Subarray [3, 3, 3, 5] with an imbalance number of 1.
41+
- Subarray [3, 3, 5] with an imbalance number of 1.
42+
- Subarray [3, 5] with an imbalance number of 1.
43+
44+
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8.
45+
46+
**Constraints:**
47+
48+
* `1 <= nums.length <= 1000`
49+
* `1 <= nums[i] <= nums.length`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2701_2800.s2765_longest_alternating_subarray
2+
3+
// #Easy #Array #Enumeration #2023_08_11_Time_191_ms_(97.92%)_Space_42.2_MB_(41.67%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun alternatingSubarray(nums: IntArray): Int {
9+
var result = -1
10+
var prious = 0
11+
var sum = 1
12+
for (i in 1..nums.lastIndex) {
13+
val s = nums[i] - nums[i - 1]
14+
if (abs(s) != 1) {
15+
sum = 1
16+
continue
17+
}
18+
if (s == prious) {
19+
sum = 2
20+
}
21+
if (s != prious) {
22+
if (s != if (sum % 2 == 0) -1 else 1) continue
23+
sum++
24+
prious = s
25+
}
26+
result = maxOf(result, sum)
27+
}
28+
return result
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2765\. Longest Alternating Subarray
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. A subarray `s` of length `m` is called **alternating** if:
6+
7+
* `m` is greater than `1`.
8+
* <code>s<sub>1</sub> = s<sub>0</sub> + 1</code>.
9+
* The **0-indexed** subarray `s` looks like <code>[s<sub>0</sub>, s<sub>1</sub>, s<sub>0</sub>, s<sub>1</sub>,...,s<sub>(m-1) % 2</sub>]</code>. In other words, <code>s<sub>1</sub> - s<sub>0</sub> = 1</code>, <code>s<sub>2</sub> - s<sub>1</sub> = -1</code>, <code>s<sub>3</sub> - s<sub>2</sub> = 1</code>, <code>s<sub>4</sub> - s<sub>3</sub> = -1</code>, and so on up to <code>s[m - 1] - s[m - 2] = (-1)<sup>m</sup></code>.
10+
11+
Return _the maximum length of all **alternating** subarrays present in_ `nums` _or_ `-1` _if no such subarray exists__._
12+
13+
A subarray is a contiguous **non-empty** sequence of elements within an array.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [2,3,4,3,4]
18+
19+
**Output:** 4
20+
21+
**Explanation:** The alternating subarrays are [3,4], [3,4,3], and [3,4,3,4]. The longest of these is [3,4,3,4], which is of length 4.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [4,5,6]
26+
27+
**Output:** 2
28+
29+
**Explanation:** [4,5] and [5,6] are the only two alternating subarrays. They are both of length 2.
30+
31+
**Constraints:**
32+
33+
* `2 <= nums.length <= 100`
34+
* <code>1 <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2701_2800.s2766_relocate_marbles
2+
3+
// #Medium #Array #Hash_Table #Sorting #Simulation
4+
// #2023_08_11_Time_1038_ms_(100.00%)_Space_79.8_MB_(51.61%)
5+
6+
class Solution {
7+
fun relocateMarbles(nums: IntArray, moveFrom: IntArray, moveTo: IntArray): List<Int> {
8+
val s = HashSet<Int>()
9+
nums.forEach { s.add(it) }
10+
for (i in moveTo.indices) {
11+
if (s.contains(moveFrom[i])) {
12+
s.remove(moveFrom[i])
13+
s.add(moveTo[i])
14+
}
15+
}
16+
return s.toList().sorted()
17+
}
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2766\. Relocate Marbles
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` representing the initial positions of some marbles. You are also given two **0-indexed** integer arrays `moveFrom` and `moveTo` of **equal** length.
6+
7+
Throughout `moveFrom.length` steps, you will change the positions of the marbles. On the <code>i<sup>th</sup></code> step, you will move **all** marbles at position `moveFrom[i]` to position `moveTo[i]`.
8+
9+
After completing all the steps, return _the sorted list of **occupied** positions_.
10+
11+
**Notes:**
12+
13+
* We call a position **occupied** if there is at least one marble in that position.
14+
* There may be multiple marbles in a single position.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]
19+
20+
**Output:** [5,6,8,9]
21+
22+
**Explanation:** Initially, the marbles are at positions 1,6,7,8.
23+
24+
At the i = 0th step, we move the marbles at position 1 to position 2. Then, positions 2,6,7,8 are occupied.
25+
26+
At the i = 1st step, we move the marbles at position 7 to position 9. Then, positions 2,6,8,9 are occupied.
27+
28+
At the i = 2nd step, we move the marbles at position 2 to position 5. Then, positions 5,6,8,9 are occupied.
29+
30+
At the end, the final positions containing at least one marbles are [5,6,8,9].
31+
32+
**Example 2:**
33+
34+
**Input:** nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
35+
36+
**Output:** [2]
37+
38+
**Explanation:** Initially, the marbles are at positions [1,1,3,3].
39+
40+
At the i = 0th step, we move all the marbles at position 1 to position 2. Then, the marbles are at positions [2,2,3,3].
41+
42+
At the i = 1st step, we move all the marbles at position 3 to position 2. Then, the marbles are at positions [2,2,2,2].
43+
44+
Since 2 is the only occupied position, we return [2].
45+
46+
**Constraints:**
47+
48+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
49+
* <code>1 <= moveFrom.length <= 10<sup>5</sup></code>
50+
* `moveFrom.length == moveTo.length`
51+
* <code>1 <= nums[i], moveFrom[i], moveTo[i] <= 10<sup>9</sup></code>
52+
* The test cases are generated such that there is at least a marble in `moveFrom[i]` at the moment we want to apply the <code>i<sup>th</sup></code> move.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g2701_2800.s2767_partition_string_into_minimum_beautiful_substrings
2+
3+
// #Medium #String #Hash_Table #Dynamic_Programming #Backtracking
4+
// #2023_08_11_Time_162_ms_(96.00%)_Space_36.2_MB_(80.00%)
5+
6+
class Solution {
7+
fun minimumBeautifulSubstrings(s: String): Int {
8+
val set: MutableSet<String> = HashSet()
9+
set.add("1")
10+
set.add("101")
11+
set.add("11001")
12+
set.add("1111101")
13+
set.add("1001110001")
14+
set.add("110000110101")
15+
set.add("11110100001001")
16+
val result = minimumBeautifulSubstringsHelper(s, 0, set, 0)
17+
return if (result == Int.MAX_VALUE) {
18+
-1
19+
} else result
20+
}
21+
22+
private fun minimumBeautifulSubstringsHelper(s: String, index: Int, set: Set<String>, count: Int): Int {
23+
if (index >= s.length) {
24+
return count
25+
}
26+
var minResult = Int.MAX_VALUE
27+
for (i in index..s.length) {
28+
if (set.contains(s.substring(index, i))) {
29+
val result = minimumBeautifulSubstringsHelper(s, i, set, count + 1)
30+
minResult = minResult.coerceAtMost(result)
31+
}
32+
}
33+
return minResult
34+
}
35+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2767\. Partition String Into Minimum Beautiful Substrings
2+
3+
Medium
4+
5+
Given a binary string `s`, partition the string into one or more **substrings** such that each substring is **beautiful**.
6+
7+
A string is **beautiful** if:
8+
9+
* It doesn't contain leading zeros.
10+
* It's the **binary** representation of a number that is a power of `5`.
11+
12+
Return _the **minimum** number of substrings in such partition._ If it is impossible to partition the string `s` into beautiful substrings, return `-1`.
13+
14+
A **substring** is a contiguous sequence of characters in a string.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "1011"
19+
20+
**Output:** 2
21+
22+
**Explanation:** We can paritition the given string into ["101", "1"].
23+
- The string "101" does not contain leading zeros and is the binary representation of integer 5<sup>1</sup> = 5.
24+
- The string "1" does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
25+
26+
It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into.
27+
28+
**Example 2:**
29+
30+
**Input:** s = "111"
31+
32+
**Output:** 3
33+
34+
**Explanation:** We can paritition the given string into ["1", "1", "1"].
35+
- The string "1" does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
36+
37+
It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into.
38+
39+
**Example 3:**
40+
41+
**Input:** s = "0"
42+
43+
**Output:** -1
44+
45+
**Explanation:** We can not partition the given string into beautiful substrings.
46+
47+
**Constraints:**
48+
49+
* `1 <= s.length <= 15`
50+
* `s[i]` is either `'0'` or `'1'`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2701_2800.s2768_number_of_black_blocks
2+
3+
// #Medium #Array #Hash_Table #Enumeration #2023_08_11_Time_719_ms_(100.00%)_Space_55.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun countBlackBlocks(m: Int, n: Int, coordinates: Array<IntArray>): LongArray {
7+
val ans = LongArray(5)
8+
val count: MutableMap<Int, Int> = HashMap()
9+
for (coordinate in coordinates) {
10+
val x = coordinate[0]
11+
val y = coordinate[1]
12+
for (i in x until x + 2) {
13+
for (j in y until y + 2) {
14+
if (i - 1 >= 0 && i < m && j - 1 >= 0 && j < n) {
15+
count.merge(
16+
i * n + j, 1
17+
) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) }
18+
}
19+
}
20+
}
21+
}
22+
for (freq in count.values) {
23+
++ans[freq]
24+
}
25+
ans[0] = (m - 1L) * (n - 1) - ans.sum()
26+
return ans
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2768\. Number of Black Blocks
2+
3+
Medium
4+
5+
You are given two integers `m` and `n` representing the dimensions of a **0-indexed** `m x n` grid.
6+
7+
You are also given a **0-indexed** 2D integer matrix `coordinates`, where `coordinates[i] = [x, y]` indicates that the cell with coordinates `[x, y]` is colored **black**. All cells in the grid that do not appear in `coordinates` are **white**.
8+
9+
A block is defined as a `2 x 2` submatrix of the grid. More formally, a block with cell `[x, y]` as its top-left corner where `0 <= x < m - 1` and `0 <= y < n - 1` contains the coordinates `[x, y]`, `[x + 1, y]`, `[x, y + 1]`, and `[x + 1, y + 1]`.
10+
11+
Return _a **0-indexed** integer array_ `arr` _of size_ `5` _such that_ `arr[i]` _is the number of blocks that contains exactly_ `i` _**black** cells_.
12+
13+
**Example 1:**
14+
15+
**Input:** m = 3, n = 3, coordinates = [[0,0]]
16+
17+
**Output:** [3,1,0,0,0]
18+
19+
**Explanation:** The grid looks like this: ![](https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-44656-am.png)
20+
21+
There is only 1 block with one black cell, and it is the block starting with cell [0,0].
22+
23+
The other 3 blocks start with cells [0,1], [1,0] and [1,1]. They all have zero black cells.
24+
25+
Thus, we return [3,1,0,0,0].
26+
27+
**Example 2:**
28+
29+
**Input:** m = 3, n = 3, coordinates = [[0,0],[1,1],[0,2]]
30+
31+
**Output:** [0,2,2,0,0]
32+
33+
**Explanation:** The grid looks like this: ![](https://assets.leetcode.com/uploads/2023/06/18/screen-shot-2023-06-18-at-45018-am.png)
34+
35+
There are 2 blocks with two black cells (the ones starting with cell coordinates [0,0] and [0,1]).
36+
37+
The other 2 blocks have starting cell coordinates of [1,0] and [1,1]. They both have 1 black cell.
38+
39+
Therefore, we return [0,2,2,0,0].
40+
41+
**Constraints:**
42+
43+
* <code>2 <= m <= 10<sup>5</sup></code>
44+
* <code>2 <= n <= 10<sup>5</sup></code>
45+
* <code>0 <= coordinates.length <= 10<sup>4</sup></code>
46+
* `coordinates[i].length == 2`
47+
* `0 <= coordinates[i][0] < m`
48+
* `0 <= coordinates[i][1] < n`
49+
* It is guaranteed that `coordinates` contains pairwise distinct coordinates.

0 commit comments

Comments
 (0)