Skip to content

Added tasks 3200-3203 #664

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 1 commit into from
Jul 6, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g3101_3200.s3200_maximum_height_of_a_triangle

// #Easy #Array #Enumeration #2024_07_06_Time_136_ms_(81.36%)_Space_33.8_MB_(28.81%)

import kotlin.math.max

@Suppress("NAME_SHADOWING")
class Solution {
private fun count(v1: Int, v2: Int): Int {
var v1 = v1
var v2 = v2
var ct = 1
var flag = true
while (true) {
if (flag) {
if (ct <= v1) {
v1 -= ct
} else {
break
}
} else {
if (ct <= v2) {
v2 -= ct
} else {
break
}
}
ct++
flag = !flag
}
return ct - 1
}

fun maxHeightOfTriangle(red: Int, blue: Int): Int {
return max(count(red, blue), count(blue, red))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
3200\. Maximum Height of a Triangle

Easy

You are given two integers `red` and `blue` representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.

All the balls in a particular row should be the **same** color, and adjacent rows should have **different** colors.

Return the **maximum** _height of the triangle_ that can be achieved.

**Example 1:**

**Input:** red = 2, blue = 4

**Output:** 3

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/16/brb.png)

The only possible arrangement is shown above.

**Example 2:**

**Input:** red = 2, blue = 1

**Output:** 2

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/16/br.png)
The only possible arrangement is shown above.

**Example 3:**

**Input:** red = 1, blue = 1

**Output:** 1

**Example 4:**

**Input:** red = 10, blue = 1

**Output:** 2

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/06/16/br.png)
The only possible arrangement is shown above.

**Constraints:**

* `1 <= red, blue <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package g3201_3300.s3201_find_the_maximum_length_of_valid_subsequence_i

// #Medium #Array #Dynamic_Programming #2024_07_06_Time_512_ms_(89.36%)_Space_62.1_MB_(76.60%)

import kotlin.math.max

class Solution {
fun maximumLength(nums: IntArray): Int {
val n = nums.size
var alter = 1
var odd = 0
var even = 0
if (nums[0] % 2 == 0) {
even++
} else {
odd++
}
var lastodd = nums[0] % 2 != 0
for (i in 1 until n) {
val flag = nums[i] % 2 == 0
if (flag) {
if (lastodd) {
alter++
lastodd = false
}
even++
} else {
if (!lastodd) {
alter++
lastodd = true
}
odd++
}
}
return max(alter, max(odd, even))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3201\. Find the Maximum Length of Valid Subsequence I

Medium

You are given an integer array `nums`.

A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies:

* `(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.`

Return the length of the **longest** **valid** subsequence of `nums`.

A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.

**Example 1:**

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

**Output:** 4

**Explanation:**

The longest valid subsequence is `[1, 2, 3, 4]`.

**Example 2:**

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

**Output:** 6

**Explanation:**

The longest valid subsequence is `[1, 2, 1, 2, 1, 2]`.

**Example 3:**

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

**Output:** 2

**Explanation:**

The longest valid subsequence is `[1, 3]`.

**Constraints:**

* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>7</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package g3201_3300.s3202_find_the_maximum_length_of_valid_subsequence_ii

// #Medium #Array #Dynamic_Programming #2024_07_06_Time_255_ms_(97.30%)_Space_49_MB_(78.38%)

import kotlin.math.max

class Solution {
fun maximumLength(nums: IntArray, k: Int): Int {
// dp array to store the index against each possible modulo
val dp = Array(nums.size + 1) { IntArray(k + 1) }
var longest = 0
for (i in nums.indices) {
for (j in 0 until i) {
// Checking the modulo with each previous number
val `val` = (nums[i] + nums[j]) % k
// storing the number of pairs that have the same modulo.
// it would be one more than the number of pairs with the same modulo at the last
// index
dp[i][`val`] = dp[j][`val`] + 1
// Calculating the max seen till now
longest = max(longest, dp[i][`val`])
}
}
// total number of elements in the subsequence would be 1 more than the number of pairs
return longest + 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
3202\. Find the Maximum Length of Valid Subsequence II

Medium

You are given an integer array `nums` and a **positive** integer `k`.

A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies:

* `(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.`

Return the length of the **longest** **valid** subsequence of `nums`.

**Example 1:**

**Input:** nums = [1,2,3,4,5], k = 2

**Output:** 5

**Explanation:**

The longest valid subsequence is `[1, 2, 3, 4, 5]`.

**Example 2:**

**Input:** nums = [1,4,2,3,1,4], k = 3

**Output:** 4

**Explanation:**

The longest valid subsequence is `[1, 4, 1, 4]`.

**Constraints:**

* <code>2 <= nums.length <= 10<sup>3</sup></code>
* <code>1 <= nums[i] <= 10<sup>7</sup></code>
* <code>1 <= k <= 10<sup>3</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package g3201_3300.s3203_find_minimum_diameter_after_merging_two_trees

// #Hard #Depth_First_Search #Breadth_First_Search #Tree #Graph
// #2024_07_06_Time_1156_ms_(100.00%)_Space_119.4_MB_(80.00%)

import kotlin.math.max

class Solution {
fun minimumDiameterAfterMerge(edges1: Array<IntArray>, edges2: Array<IntArray>): Int {
val n = edges1.size + 1
val g = packU(n, edges1)
val m = edges2.size + 1
val h = packU(m, edges2)
val d1 = diameter(g)
val d2 = diameter(h)
var ans = max(d1[0], d2[0])
ans = max(
((d1[0] + 1) / 2 + ((d2[0] + 1) / 2) + 1),
ans
)
return ans
}

private fun diameter(g: Array<IntArray?>): IntArray {
val n = g.size
val f0: Int
val f1: Int
val d01: Int
val q = IntArray(n)
val ved = BooleanArray(n)
var qp = 0
q[qp++] = 0
ved[0] = true
run {
var i = 0
while (i < qp) {
val cur = q[i]
for (e in g[cur]!!) {
if (!ved[e]) {
ved[e] = true
q[qp++] = e
}
}
i++
}
}
f0 = q[n - 1]
val d = IntArray(n)
qp = 0
ved.fill(false)
q[qp++] = f0
ved[f0] = true
var i = 0
while (i < qp) {
val cur = q[i]
for (e in g[cur]!!) {
if (!ved[e]) {
ved[e] = true
q[qp++] = e
d[e] = d[cur] + 1
}
}
i++
}
f1 = q[n - 1]
d01 = d[f1]
return intArrayOf(d01, f0, f1)
}

private fun packU(n: Int, ft: Array<IntArray>): Array<IntArray?> {
val g = arrayOfNulls<IntArray>(n)
val p = IntArray(n)
for (u in ft) {
p[u[0]]++
p[u[1]]++
}
for (i in 0 until n) {
g[i] = IntArray(p[i])
}
for (u in ft) {
g[u[0]]!![--p[u[0]]] = u[1]
g[u[1]]!![--p[u[1]]] = u[0]
}
return g
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3203\. Find Minimum Diameter After Merging Two Trees

Hard

There exist two **undirected** trees with `n` and `m` nodes, numbered from `0` to `n - 1` and from `0` to `m - 1`, respectively. You are given two 2D integer arrays `edges1` and `edges2` of lengths `n - 1` and `m - 1`, respectively, where <code>edges1[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 first tree and <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the second tree.

You must connect one node from the first tree with another node from the second tree with an edge.

Return the **minimum** possible **diameter** of the resulting tree.

The **diameter** of a tree is the length of the _longest_ path between any two nodes in the tree.

**Example 1:**![](https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png)

**Input:** edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]

**Output:** 3

**Explanation:**

We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.

**Example 2:**

![](https://assets.leetcode.com/uploads/2024/04/22/example211.png)

**Input:** edges1 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]], edges2 = [[0,1],[0,2],[0,3],[2,4],[2,5],[3,6],[2,7]]

**Output:** 5

**Explanation:**

We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.

**Constraints:**

* <code>1 <= n, m <= 10<sup>5</sup></code>
* `edges1.length == n - 1`
* `edges2.length == m - 1`
* `edges1[i].length == edges2[i].length == 2`
* <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
* <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code>
* The input is generated such that `edges1` and `edges2` represent valid trees.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package g3101_3200.s3200_maximum_height_of_a_triangle

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Test

internal class SolutionTest {
@Test
fun maxHeightOfTriangle() {
assertThat(Solution().maxHeightOfTriangle(2, 4), equalTo(3))
}

@Test
fun maxHeightOfTriangle2() {
assertThat(Solution().maxHeightOfTriangle(2, 1), equalTo(2))
}
}
Loading