Skip to content

Commit f77f339

Browse files
authored
Added tasks 2315-2326
1 parent a9988c4 commit f77f339

File tree

30 files changed

+1183
-0
lines changed

30 files changed

+1183
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g2301_2400.s2315_count_asterisks
2+
3+
// #Easy #String #2023_06_30_Time_137_ms_(94.44%)_Space_34.3_MB_(77.78%)
4+
5+
class Solution {
6+
fun countAsterisks(s: String): Int {
7+
var c = 0
8+
val n = s.length
9+
var i = 0
10+
while (i < n) {
11+
if (s[i] == '|') {
12+
i++
13+
while (s[i] != '|') {
14+
i++
15+
}
16+
}
17+
if (s[i] == '*') {
18+
c++
19+
}
20+
i++
21+
}
22+
return c
23+
}
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2315\. Count Asterisks
2+
3+
Easy
4+
5+
You are given a string `s`, where every **two** consecutive vertical bars `'|'` are grouped into a **pair**. In other words, the 1<sup>st</sup> and 2<sup>nd</sup> `'|'` make a pair, the 3<sup>rd</sup> and 4<sup>th</sup> `'|'` make a pair, and so forth.
6+
7+
Return _the number of_ `'*'` _in_ `s`_, **excluding** the_ `'*'` _between each pair of_ `'|'`.
8+
9+
**Note** that each `'|'` will belong to **exactly** one pair.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "l|\*e\*et|c\*\*o|\*de|"
14+
15+
**Output:** 2
16+
17+
**Explanation:** The considered characters are underlined: "<ins>l</ins>|\*e\*et|<ins>c\*\*o</ins>|\*de|".
18+
19+
The characters between the first and second '|' are excluded from the answer.
20+
21+
Also, the characters between the third and fourth '|' are excluded from the answer. There are 2 asterisks considered. Therefore, we return 2.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "iamprogrammer"
26+
27+
**Output:** 0
28+
29+
**Explanation:** In this example, there are no asterisks in s. Therefore, we return 0.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "yo|uar|e\*\*|b|e\*\*\*au|tifu|l"
34+
35+
**Output:** 5
36+
37+
**Explanation:** The considered characters are underlined: "<ins>yo</ins>|uar|<ins>e\*\*</ins>|b|<ins>e\*\*\*au</ins>|tifu|<ins>l</ins>".
38+
39+
There are 5 asterisks considered. Therefore, we return 5.
40+
41+
**Constraints:**
42+
43+
* `1 <= s.length <= 1000`
44+
* `s` consists of lowercase English letters, vertical bars `'|'`, and asterisks `'*'`.
45+
* `s` contains an **even** number of vertical bars `'|'`.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package g2301_2400.s2316_count_unreachable_pairs_of_nodes_in_an_undirected_graph
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #2023_06_30_Time_981_ms_(87.50%)_Space_118.4_MB_(50.00%)
5+
6+
class Solution {
7+
fun countPairs(n: Int, edges: Array<IntArray>): Long {
8+
val d = DSU(n)
9+
val map = HashMap<Int, Int>()
10+
for (e in edges) {
11+
d.union(e[0], e[1])
12+
}
13+
var ans: Long = 0
14+
for (i in 0 until n) {
15+
val p = d.findParent(i)
16+
val cnt = if (map.containsKey(p)) map[p]!! else 0
17+
ans += (i - cnt).toLong()
18+
map[p] = map.getOrDefault(p, 0) + 1
19+
}
20+
return ans
21+
}
22+
23+
private class DSU internal constructor(n: Int) {
24+
var rank: IntArray
25+
var parent: IntArray
26+
27+
init {
28+
rank = IntArray(n + 1)
29+
parent = IntArray(n + 1)
30+
for (i in 1..n) {
31+
parent[i] = i
32+
}
33+
}
34+
35+
fun findParent(node: Int): Int {
36+
if (parent[node] == node) {
37+
return node
38+
}
39+
parent[node] = findParent(parent[node])
40+
return findParent(parent[node])
41+
}
42+
43+
fun union(x: Int, y: Int): Boolean {
44+
val px = findParent(x)
45+
val py = findParent(y)
46+
if (px == py) {
47+
return false
48+
}
49+
if (rank[px] > rank[py]) {
50+
parent[py] = px
51+
} else {
52+
parent[px] = py
53+
rank[py]++
54+
}
55+
return true
56+
}
57+
}
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2316\. Count Unreachable Pairs of Nodes in an Undirected Graph
2+
3+
Medium
4+
5+
You are given an integer `n`. There is an **undirected** graph with `n` nodes, numbered from `0` to `n - 1`. You are given a 2D integer array `edges` where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an **undirected** edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.
6+
7+
Return _the **number of pairs** of different nodes that are **unreachable** from each other_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2022/05/05/tc-3.png)
12+
13+
**Input:** n = 3, edges = [[0,1],[0,2],[1,2]]
14+
15+
**Output:** 0
16+
17+
**Explanation:** There are no pairs of nodes that are unreachable from each other.
18+
19+
Therefore, we return 0.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2022/05/05/tc-2.png)
24+
25+
**Input:** n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
26+
27+
**Output:** 14
28+
29+
**Explanation:** There are 14 pairs of nodes that are unreachable from each other: [[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]].
30+
31+
Therefore, we return 14.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= n <= 10<sup>5</sup></code>
36+
* <code>0 <= edges.length <= 2 * 10<sup>5</sup></code>
37+
* `edges[i].length == 2`
38+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
39+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
40+
* There are no repeated edges.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package g2301_2400.s2317_maximum_xor_after_operations
2+
3+
// #Medium #Array #Math #Bit_Manipulation #2023_06_30_Time_373_ms_(100.00%)_Space_50_MB_(100.00%)
4+
5+
class Solution {
6+
fun maximumXOR(nums: IntArray): Int {
7+
var max = 0
8+
for (n in nums) {
9+
max = max or n
10+
}
11+
return max
12+
}
13+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2317\. Maximum XOR After Operations
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`. In one operation, select **any** non-negative integer `x` and an index `i`, then **update** `nums[i]` to be equal to `nums[i] AND (nums[i] XOR x)`.
6+
7+
Note that `AND` is the bitwise AND operation and `XOR` is the bitwise XOR operation.
8+
9+
Return _the **maximum** possible bitwise XOR of all elements of_ `nums` _after applying the operation **any number** of times_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,2,4,6]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
18+
19+
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
20+
21+
It can be shown that 7 is the maximum possible bitwise XOR.
22+
23+
Note that other operations may be used to achieve a bitwise XOR of 7.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,3,9,2]
28+
29+
**Output:** 11
30+
31+
**Explanation:** Apply the operation zero times.
32+
33+
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
34+
35+
It can be shown that 11 is the maximum possible bitwise XOR.
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>0 <= nums[i] <= 10<sup>8</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g2301_2400.s2318_number_of_distinct_roll_sequences
2+
3+
// #Hard #Dynamic_Programming #Memoization #2023_06_30_Time_441_ms_(100.00%)_Space_49.6_MB_(100.00%)
4+
5+
class Solution {
6+
private val memo = Array(10001) { Array(7) { IntArray(7) } }
7+
private val mod = 1000000007
8+
private val m = arrayOf(
9+
intArrayOf(1, 2, 3, 4, 5, 6),
10+
intArrayOf(2, 3, 4, 5, 6),
11+
intArrayOf(1, 3, 5),
12+
intArrayOf(1, 2, 4, 5),
13+
intArrayOf(1, 3, 5),
14+
intArrayOf(1, 2, 3, 4, 6),
15+
intArrayOf(1, 5)
16+
)
17+
18+
fun distinctSequences(n: Int): Int {
19+
return dp(n, 0, 0)
20+
}
21+
22+
private fun dp(n: Int, prev: Int, pprev: Int): Int {
23+
if (n == 0) {
24+
return 1
25+
}
26+
if (memo[n][prev][pprev] != 0) {
27+
return memo[n][prev][pprev]
28+
}
29+
var ans = 0
30+
for (x in m[prev]) {
31+
if (x != pprev) {
32+
ans = (ans + dp(n - 1, x, prev)) % mod
33+
}
34+
}
35+
memo[n][prev][pprev] = ans
36+
return ans
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2318\. Number of Distinct Roll Sequences
2+
3+
Hard
4+
5+
You are given an integer `n`. You roll a fair 6-sided dice `n` times. Determine the total number of **distinct** sequences of rolls possible such that the following conditions are satisfied:
6+
7+
1. The **greatest common divisor** of any **adjacent** values in the sequence is equal to `1`.
8+
2. There is **at least** a gap of `2` rolls between **equal** valued rolls. More formally, if the value of the <code>i<sup>th</sup></code> roll is **equal** to the value of the <code>j<sup>th</sup></code> roll, then `abs(i - j) > 2`.
9+
10+
Return _the **total number** of distinct sequences possible_. Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
11+
12+
Two sequences are considered distinct if at least one element is different.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 4
17+
18+
**Output:** 184
19+
20+
**Explanation:** Some of the possible sequences are (1, 2, 3, 4), (6, 1, 2, 3), (1, 2, 3, 1), etc.
21+
22+
Some invalid sequences are (1, 2, 1, 3), (1, 2, 3, 6).
23+
24+
(1, 2, 1, 3) is invalid since the first and third roll have an equal value and abs(1 - 3) = 2 (i and j are 1-indexed).
25+
26+
(1, 2, 3, 6) is invalid since the greatest common divisor of 3 and 6 = 3.
27+
28+
There are a total of 184 distinct sequences possible, so we return 184.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 2
33+
34+
**Output:** 22
35+
36+
**Explanation:** Some of the possible sequences are (1, 2), (2, 1), (3, 2).
37+
38+
Some invalid sequences are (3, 6), (2, 4) since the greatest common divisor is not equal to 1.
39+
40+
There are a total of 22 distinct sequences possible, so we return 22.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g2301_2400.s2319_check_if_matrix_is_x_matrix
2+
3+
// #Easy #Array #Matrix #2023_06_30_Time_247_ms_(100.00%)_Space_39_MB_(70.00%)
4+
5+
class Solution {
6+
fun checkXMatrix(grid: Array<IntArray>): Boolean {
7+
for (i in grid.indices) {
8+
for (j in grid[0].indices) {
9+
if (i == j || i + j == grid.size - 1) {
10+
if (grid[i][j] == 0) {
11+
return false
12+
}
13+
} else {
14+
if (grid[i][j] != 0) {
15+
return false
16+
}
17+
}
18+
}
19+
}
20+
return true
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2319\. Check if Matrix Is X-Matrix
2+
3+
Easy
4+
5+
A square matrix is said to be an **X-Matrix** if **both** of the following conditions hold:
6+
7+
1. All the elements in the diagonals of the matrix are **non-zero**.
8+
2. All other elements are 0.
9+
10+
Given a 2D integer array `grid` of size `n x n` representing a square matrix, return `true` _if_ `grid` _is an X-Matrix_. Otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2022/05/03/ex1.jpg)
15+
16+
**Input:** grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
17+
18+
**Output:** true
19+
20+
**Explanation:** Refer to the diagram above.
21+
22+
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
23+
24+
Thus, grid is an X-Matrix.
25+
26+
**Example 2:**
27+
28+
![](https://assets.leetcode.com/uploads/2022/05/03/ex2.jpg)
29+
30+
**Input:** grid = [[5,7,0],[0,3,1],[0,5,0]]
31+
32+
**Output:** false
33+
34+
**Explanation:** Refer to the diagram above.
35+
36+
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
37+
38+
Thus, grid is not an X-Matrix.
39+
40+
**Constraints:**
41+
42+
* `n == grid.length == grid[i].length`
43+
* `3 <= n <= 100`
44+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)