Skip to content

Added tasks 3248-3251 #677

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 3 commits into from
Aug 13, 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
27 changes: 27 additions & 0 deletions src/main/kotlin/g3201_3300/s3248_snake_in_matrix/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g3201_3300.s3248_snake_in_matrix

// #Easy #Array #String #Simulation #2024_08_13_Time_174_ms_(90.91%)_Space_37.5_MB_(34.09%)

class Solution {
fun finalPositionOfSnake(n: Int, commands: List<String>): Int {
var x = 0
var y = 0
for (command in commands) {
when (command) {
"UP" -> if (x > 0) {
x--
}
"DOWN" -> if (x < n - 1) {
x++
}
"LEFT" -> if (y > 0) {
y--
}
"RIGHT" -> if (y < n - 1) {
y++
}
}
}
return (x * n) + y
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions src/main/kotlin/g3201_3300/s3248_snake_in_matrix/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
3248\. Snake in Matrix

Easy

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

The snake starts at cell 0 and follows a sequence of commands.

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.

Return the position of the final cell where the snake ends up after executing `commands`.

**Example 1:**

**Input:** n = 2, commands = ["RIGHT","DOWN"]

**Output:** 3

**Explanation:**

![image](image01.png)

**Example 2:**

**Input:** n = 3, commands = ["DOWN","RIGHT","UP"]

**Output:** 1

**Explanation:**

![image](image02.png)

**Constraints:**

* `2 <= n <= 10`
* `1 <= commands.length <= 100`
* `commands` consists only of `"UP"`, `"RIGHT"`, `"DOWN"`, and `"LEFT"`.
* The input is generated such the snake will not move outside of the boundaries.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package g3201_3300.s3249_count_the_number_of_good_nodes

// #Medium #Depth_First_Search #Tree #2024_08_13_Time_1190_ms_(100.00%)_Space_127.6_MB_(77.27%)

class Solution {
private var count = 0

fun countGoodNodes(edges: Array<IntArray>): Int {
val n = edges.size + 1
val nodes = arrayOfNulls<TNode>(n)
nodes[0] = TNode()
for (edge in edges) {
val a = edge[0]
val b = edge[1]
if (nodes[b] != null && nodes[a] == null) {
nodes[a] = TNode()
nodes[b]!!.children.add(nodes[a])
} else {
if (nodes[a] == null) {
nodes[a] = TNode()
}
if (nodes[b] == null) {
nodes[b] = TNode()
}
nodes[a]!!.children.add(nodes[b])
}
}
sizeOfTree(nodes[0])
return count
}

private fun sizeOfTree(node: TNode?): Int {
if (node!!.size > 0) {
return node.size
}
val children: List<TNode?> = node.children
if (children.isEmpty()) {
count++
node.size = 1
return 1
}
val size = sizeOfTree(children[0])
var sum = size
var goodNode = true
for (i in 1 until children.size) {
val child = children[i]
if (size != sizeOfTree(child)) {
goodNode = false
}
sum += sizeOfTree(child)
}
if (goodNode) {
count++
}
sum++
node.size = sum
return sum
}

private class TNode {
var size: Int = -1
var children: MutableList<TNode?> = ArrayList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
3249\. Count the Number of Good Nodes

Medium

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.

A node is **good** if all the subtrees rooted at its children have the same size.

Return the number of **good** nodes in the given tree.

A **subtree** of `treeName` is a tree consisting of a node in `treeName` and all of its descendants.

**Example 1:**

**Input:** edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]

**Output:** 7

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/05/26/tree1.png)

All of the nodes of the given tree are good.

**Example 2:**

**Input:** edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]

**Output:** 6

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-193552.png)

There are 6 good nodes in the given tree. They are colored in the image above.

**Example 3:**

**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]]

**Output:** 12

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/08/08/rob.jpg)

All nodes except node 9 are good.

**Constraints:**

* <code>2 <= n <= 10<sup>5</sup></code>
* `edges.length == n - 1`
* `edges[i].length == 2`
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
* The input is generated such that `edges` represents a valid tree.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g3201_3300.s3250_find_the_count_of_monotonic_pairs_i

// #Hard #Array #Dynamic_Programming #Math #Prefix_Sum #Combinatorics
// #2024_08_13_Time_241_ms_(100.00%)_Space_39.2_MB_(100.00%)

import kotlin.math.max
import kotlin.math.min

class Solution {
fun countOfPairs(nums: IntArray): Int {
val maxShift = IntArray(nums.size)
maxShift[0] = nums[0]
var currShift = 0
for (i in 1 until nums.size) {
currShift = max(currShift, (nums[i] - maxShift[i - 1]))
maxShift[i] = min(maxShift[i - 1], (nums[i] - currShift))
if (maxShift[i] < 0) {
return 0
}
}
val cases = getAllCases(nums, maxShift)
return cases[nums.size - 1]!![maxShift[nums.size - 1]]
}

private fun getAllCases(nums: IntArray, maxShift: IntArray): Array<IntArray?> {
var currCases: IntArray
val cases = arrayOfNulls<IntArray>(nums.size)
cases[0] = IntArray(maxShift[0] + 1)
for (i in cases[0]!!.indices) {
cases[0]!![i] = i + 1
}
for (i in 1 until nums.size) {
currCases = IntArray(maxShift[i] + 1)
currCases[0] = 1
for (j in 1 until currCases.size) {
val prevCases =
if (j < cases[i - 1]!!.size
) cases[i - 1]!![j]
else cases[i - 1]!![cases[i - 1]!!.size - 1]
currCases[j] = (currCases[j - 1] + prevCases) % (1000000000 + 7)
}
cases[i] = currCases
}
return cases
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3250\. Find the Count of Monotonic Pairs I

Hard

You are given an array of **positive** integers `nums` of length `n`.

We call a pair of **non-negative** integer arrays `(arr1, arr2)` **monotonic** if:

* The lengths of both arrays are `n`.
* `arr1` is monotonically **non-decreasing**, in other words, `arr1[0] <= arr1[1] <= ... <= arr1[n - 1]`.
* `arr2` is monotonically **non-increasing**, in other words, `arr2[0] >= arr2[1] >= ... >= arr2[n - 1]`.
* `arr1[i] + arr2[i] == nums[i]` for all `0 <= i <= n - 1`.

Return the count of **monotonic** pairs.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

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

**Output:** 4

**Explanation:**

The good pairs are:

1. `([0, 1, 1], [2, 2, 1])`
2. `([0, 1, 2], [2, 2, 0])`
3. `([0, 2, 2], [2, 1, 0])`
4. `([1, 2, 2], [1, 1, 0])`

**Example 2:**

**Input:** nums = [5,5,5,5]

**Output:** 126

**Constraints:**

* `1 <= n == nums.length <= 2000`
* `1 <= nums[i] <= 50`
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package g3201_3300.s3251_find_the_count_of_monotonic_pairs_ii

// #Hard #Array #Dynamic_Programming #Math #Prefix_Sum #Combinatorics
// #2024_08_13_Time_291_ms_(100.00%)_Space_47_MB_(100.00%)

import kotlin.math.max

class Solution {
fun countOfPairs(nums: IntArray): Int {
var prefixZeros = 0
val n = nums.size
// Calculate prefix zeros
for (i in 1 until n) {
prefixZeros += max((nums[i] - nums[i - 1]), 0)
}
val row = n + 1
val col = nums[n - 1] + 1 - prefixZeros
if (col <= 0) {
return 0
}
// Initialize dp array
val dp = IntArray(col)
dp.fill(1)
// Fill dp array
for (r in 1 until row) {
for (c in 1 until col) {
dp[c] = (dp[c] + dp[c - 1]) % MOD
}
}
return dp[col - 1]
}

companion object {
private const val MOD = 1000000007
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
3251\. Find the Count of Monotonic Pairs II

Hard

You are given an array of **positive** integers `nums` of length `n`.

We call a pair of **non-negative** integer arrays `(arr1, arr2)` **monotonic** if:

* The lengths of both arrays are `n`.
* `arr1` is monotonically **non-decreasing**, in other words, `arr1[0] <= arr1[1] <= ... <= arr1[n - 1]`.
* `arr2` is monotonically **non-increasing**, in other words, `arr2[0] >= arr2[1] >= ... >= arr2[n - 1]`.
* `arr1[i] + arr2[i] == nums[i]` for all `0 <= i <= n - 1`.

Return the count of **monotonic** pairs.

Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.

**Example 1:**

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

**Output:** 4

**Explanation:**

The good pairs are:

1. `([0, 1, 1], [2, 2, 1])`
2. `([0, 1, 2], [2, 2, 0])`
3. `([0, 2, 2], [2, 1, 0])`
4. `([1, 2, 2], [1, 1, 0])`

**Example 2:**

**Input:** nums = [5,5,5,5]

**Output:** 126

**Constraints:**

* `1 <= n == nums.length <= 2000`
* `1 <= nums[i] <= 1000`
Loading