Skip to content

Commit 5943a79

Browse files
authored
Added tasks 3248-3251
1 parent c270496 commit 5943a79

File tree

14 files changed

+505
-0
lines changed

14 files changed

+505
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3201_3300.s3248_snake_in_matrix
2+
3+
// #Easy #Array #String #Simulation #2024_08_13_Time_174_ms_(90.91%)_Space_37.5_MB_(34.09%)
4+
5+
class Solution {
6+
fun finalPositionOfSnake(n: Int, commands: List<String>): Int {
7+
var x = 0
8+
var y = 0
9+
for (command in commands) {
10+
when (command) {
11+
"UP" -> if (x > 0) {
12+
x--
13+
}
14+
"DOWN" -> if (x < n - 1) {
15+
x++
16+
}
17+
"LEFT" -> if (y > 0) {
18+
y--
19+
}
20+
"RIGHT" -> if (y < n - 1) {
21+
y++
22+
}
23+
}
24+
}
25+
return (x * n) + y
26+
}
27+
}
Loading
Loading
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3248\. Snake in Matrix
2+
3+
Easy
4+
5+
There is a snake in an `n x n` matrix `grid` and can move in **four possible directions**. Each cell in the `grid` is identified by the position: `grid[i][j] = (i * n) + j`.
6+
7+
The snake starts at cell 0 and follows a sequence of commands.
8+
9+
You are given an integer `n` representing the size of the `grid` and an array of strings `commands` where each `command[i]` is either `"UP"`, `"RIGHT"`, `"DOWN"`, and `"LEFT"`. It's guaranteed that the snake will remain within the `grid` boundaries throughout its movement.
10+
11+
Return the position of the final cell where the snake ends up after executing `commands`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 2, commands = ["RIGHT","DOWN"]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
![image](image01.png)
22+
23+
**Example 2:**
24+
25+
**Input:** n = 3, commands = ["DOWN","RIGHT","UP"]
26+
27+
**Output:** 1
28+
29+
**Explanation:**
30+
31+
![image](image02.png)
32+
33+
**Constraints:**
34+
35+
* `2 <= n <= 10`
36+
* `1 <= commands.length <= 100`
37+
* `commands` consists only of `"UP"`, `"RIGHT"`, `"DOWN"`, and `"LEFT"`.
38+
* The input is generated such the snake will not move outside of the boundaries.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g3201_3300.s3249_count_the_number_of_good_nodes
2+
3+
// #Medium #Depth_First_Search #Tree #2024_08_13_Time_1190_ms_(100.00%)_Space_127.6_MB_(77.27%)
4+
5+
class Solution {
6+
private var count = 0
7+
8+
fun countGoodNodes(edges: Array<IntArray>): Int {
9+
val n = edges.size + 1
10+
val nodes = arrayOfNulls<TNode>(n)
11+
nodes[0] = TNode()
12+
for (edge in edges) {
13+
val a = edge[0]
14+
val b = edge[1]
15+
if (nodes[b] != null && nodes[a] == null) {
16+
nodes[a] = TNode()
17+
nodes[b]!!.children.add(nodes[a])
18+
} else {
19+
if (nodes[a] == null) {
20+
nodes[a] = TNode()
21+
}
22+
if (nodes[b] == null) {
23+
nodes[b] = TNode()
24+
}
25+
nodes[a]!!.children.add(nodes[b])
26+
}
27+
}
28+
sizeOfTree(nodes[0])
29+
return count
30+
}
31+
32+
private fun sizeOfTree(node: TNode?): Int {
33+
if (node!!.size > 0) {
34+
return node.size
35+
}
36+
val children: List<TNode?> = node.children
37+
if (children.isEmpty()) {
38+
count++
39+
node.size = 1
40+
return 1
41+
}
42+
val size = sizeOfTree(children[0])
43+
var sum = size
44+
var goodNode = true
45+
for (i in 1 until children.size) {
46+
val child = children[i]
47+
if (size != sizeOfTree(child)) {
48+
goodNode = false
49+
}
50+
sum += sizeOfTree(child)
51+
}
52+
if (goodNode) {
53+
count++
54+
}
55+
sum++
56+
node.size = sum
57+
return sum
58+
}
59+
60+
private class TNode {
61+
var size: Int = -1
62+
var children: MutableList<TNode?> = ArrayList()
63+
}
64+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3249\. Count the Number of Good Nodes
2+
3+
Medium
4+
5+
There is an **undirected** tree with `n` nodes labeled from `0` to `n - 1`, and rooted at node `0`. You are given a 2D integer array `edges` of length `n - 1`, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.
6+
7+
A node is **good** if all the subtrees rooted at its children have the same size.
8+
9+
Return the number of **good** nodes in the given tree.
10+
11+
A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.
12+
13+
**Example 1:**
14+
15+
**Input:** edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
16+
17+
**Output:** 7
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2024/05/26/tree1.png)
22+
23+
All of the nodes of the given tree are good.
24+
25+
**Example 2:**
26+
27+
**Input:** edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]
28+
29+
**Output:** 6
30+
31+
**Explanation:**
32+
33+
![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-193552.png)
34+
35+
There are 6 good nodes in the given tree. They are colored in the image above.
36+
37+
**Example 3:**
38+
39+
**Input:** edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]]
40+
41+
**Output:** 12
42+
43+
**Explanation:**
44+
45+
![](https://assets.leetcode.com/uploads/2024/08/08/rob.jpg)
46+
47+
All nodes except node 9 are good.
48+
49+
**Constraints:**
50+
51+
* <code>2 <= n <= 10<sup>5</sup></code>
52+
* `edges.length == n - 1`
53+
* `edges[i].length == 2`
54+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
55+
* The input is generated such that `edges` represents a valid tree.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g3201_3300.s3250_find_the_count_of_monotonic_pairs_i
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Prefix_Sum #Combinatorics
4+
// #2024_08_13_Time_241_ms_(100.00%)_Space_39.2_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun countOfPairs(nums: IntArray): Int {
11+
val maxShift = IntArray(nums.size)
12+
maxShift[0] = nums[0]
13+
var currShift = 0
14+
for (i in 1 until nums.size) {
15+
currShift = max(currShift, (nums[i] - maxShift[i - 1]))
16+
maxShift[i] = min(maxShift[i - 1], (nums[i] - currShift))
17+
if (maxShift[i] < 0) {
18+
return 0
19+
}
20+
}
21+
val cases = getAllCases(nums, maxShift)
22+
return cases[nums.size - 1]!![maxShift[nums.size - 1]]
23+
}
24+
25+
private fun getAllCases(nums: IntArray, maxShift: IntArray): Array<IntArray?> {
26+
var currCases: IntArray
27+
val cases = arrayOfNulls<IntArray>(nums.size)
28+
cases[0] = IntArray(maxShift[0] + 1)
29+
for (i in cases[0]!!.indices) {
30+
cases[0]!![i] = i + 1
31+
}
32+
for (i in 1 until nums.size) {
33+
currCases = IntArray(maxShift[i] + 1)
34+
currCases[0] = 1
35+
for (j in 1 until currCases.size) {
36+
val prevCases =
37+
if (j < cases[i - 1]!!.size
38+
) cases[i - 1]!![j]
39+
else cases[i - 1]!![cases[i - 1]!!.size - 1]
40+
currCases[j] = (currCases[j - 1] + prevCases) % (1000000000 + 7)
41+
}
42+
cases[i] = currCases
43+
}
44+
return cases
45+
}
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3250\. Find the Count of Monotonic Pairs I
2+
3+
Hard
4+
5+
You are given an array of **positive** integers `nums` of length `n`.
6+
7+
We call a pair of **non-negative** integer arrays `(arr1, arr2)` **monotonic** if:
8+
9+
* The lengths of both arrays are `n`.
10+
* `arr1` is monotonically **non-decreasing**, in other words, `arr1[0] <= arr1[1] <= ... <= arr1[n - 1]`.
11+
* `arr2` is monotonically **non-increasing**, in other words, `arr2[0] >= arr2[1] >= ... >= arr2[n - 1]`.
12+
* `arr1[i] + arr2[i] == nums[i]` for all `0 <= i <= n - 1`.
13+
14+
Return the count of **monotonic** pairs.
15+
16+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [2,3,2]
21+
22+
**Output:** 4
23+
24+
**Explanation:**
25+
26+
The good pairs are:
27+
28+
1. `([0, 1, 1], [2, 2, 1])`
29+
2. `([0, 1, 2], [2, 2, 0])`
30+
3. `([0, 2, 2], [2, 1, 0])`
31+
4. `([1, 2, 2], [1, 1, 0])`
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [5,5,5,5]
36+
37+
**Output:** 126
38+
39+
**Constraints:**
40+
41+
* `1 <= n == nums.length <= 2000`
42+
* `1 <= nums[i] <= 50`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g3201_3300.s3251_find_the_count_of_monotonic_pairs_ii
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Prefix_Sum #Combinatorics
4+
// #2024_08_13_Time_291_ms_(100.00%)_Space_47_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun countOfPairs(nums: IntArray): Int {
10+
var prefixZeros = 0
11+
val n = nums.size
12+
// Calculate prefix zeros
13+
for (i in 1 until n) {
14+
prefixZeros += max((nums[i] - nums[i - 1]), 0)
15+
}
16+
val row = n + 1
17+
val col = nums[n - 1] + 1 - prefixZeros
18+
if (col <= 0) {
19+
return 0
20+
}
21+
// Initialize dp array
22+
val dp = IntArray(col)
23+
dp.fill(1)
24+
// Fill dp array
25+
for (r in 1 until row) {
26+
for (c in 1 until col) {
27+
dp[c] = (dp[c] + dp[c - 1]) % MOD
28+
}
29+
}
30+
return dp[col - 1]
31+
}
32+
33+
companion object {
34+
private const val MOD = 1000000007
35+
}
36+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3251\. Find the Count of Monotonic Pairs II
2+
3+
Hard
4+
5+
You are given an array of **positive** integers `nums` of length `n`.
6+
7+
We call a pair of **non-negative** integer arrays `(arr1, arr2)` **monotonic** if:
8+
9+
* The lengths of both arrays are `n`.
10+
* `arr1` is monotonically **non-decreasing**, in other words, `arr1[0] <= arr1[1] <= ... <= arr1[n - 1]`.
11+
* `arr2` is monotonically **non-increasing**, in other words, `arr2[0] >= arr2[1] >= ... >= arr2[n - 1]`.
12+
* `arr1[i] + arr2[i] == nums[i]` for all `0 <= i <= n - 1`.
13+
14+
Return the count of **monotonic** pairs.
15+
16+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [2,3,2]
21+
22+
**Output:** 4
23+
24+
**Explanation:**
25+
26+
The good pairs are:
27+
28+
1. `([0, 1, 1], [2, 2, 1])`
29+
2. `([0, 1, 2], [2, 2, 0])`
30+
3. `([0, 2, 2], [2, 1, 0])`
31+
4. `([1, 2, 2], [1, 1, 0])`
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [5,5,5,5]
36+
37+
**Output:** 126
38+
39+
**Constraints:**
40+
41+
* `1 <= n == nums.length <= 2000`
42+
* `1 <= nums[i] <= 1000`

0 commit comments

Comments
 (0)