Skip to content

Commit 77df0fb

Browse files
authored
Added tasks 3200-3203
1 parent afa00d9 commit 77df0fb

File tree

12 files changed

+467
-0
lines changed

12 files changed

+467
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3101_3200.s3200_maximum_height_of_a_triangle
2+
3+
// #Easy #Array #Enumeration #2024_07_06_Time_136_ms_(81.36%)_Space_33.8_MB_(28.81%)
4+
5+
import kotlin.math.max
6+
7+
@Suppress("NAME_SHADOWING")
8+
class Solution {
9+
private fun count(v1: Int, v2: Int): Int {
10+
var v1 = v1
11+
var v2 = v2
12+
var ct = 1
13+
var flag = true
14+
while (true) {
15+
if (flag) {
16+
if (ct <= v1) {
17+
v1 -= ct
18+
} else {
19+
break
20+
}
21+
} else {
22+
if (ct <= v2) {
23+
v2 -= ct
24+
} else {
25+
break
26+
}
27+
}
28+
ct++
29+
flag = !flag
30+
}
31+
return ct - 1
32+
}
33+
34+
fun maxHeightOfTriangle(red: Int, blue: Int): Int {
35+
return max(count(red, blue), count(blue, red))
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3200\. Maximum Height of a Triangle
2+
3+
Easy
4+
5+
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.
6+
7+
All the balls in a particular row should be the **same** color, and adjacent rows should have **different** colors.
8+
9+
Return the **maximum** _height of the triangle_ that can be achieved.
10+
11+
**Example 1:**
12+
13+
**Input:** red = 2, blue = 4
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
![](https://assets.leetcode.com/uploads/2024/06/16/brb.png)
20+
21+
The only possible arrangement is shown above.
22+
23+
**Example 2:**
24+
25+
**Input:** red = 2, blue = 1
26+
27+
**Output:** 2
28+
29+
**Explanation:**
30+
31+
![](https://assets.leetcode.com/uploads/2024/06/16/br.png)
32+
The only possible arrangement is shown above.
33+
34+
**Example 3:**
35+
36+
**Input:** red = 1, blue = 1
37+
38+
**Output:** 1
39+
40+
**Example 4:**
41+
42+
**Input:** red = 10, blue = 1
43+
44+
**Output:** 2
45+
46+
**Explanation:**
47+
48+
![](https://assets.leetcode.com/uploads/2024/06/16/br.png)
49+
The only possible arrangement is shown above.
50+
51+
**Constraints:**
52+
53+
* `1 <= red, blue <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g3201_3300.s3201_find_the_maximum_length_of_valid_subsequence_i
2+
3+
// #Medium #Array #Dynamic_Programming #2024_07_06_Time_512_ms_(89.36%)_Space_62.1_MB_(76.60%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumLength(nums: IntArray): Int {
9+
val n = nums.size
10+
var alter = 1
11+
var odd = 0
12+
var even = 0
13+
if (nums[0] % 2 == 0) {
14+
even++
15+
} else {
16+
odd++
17+
}
18+
var lastodd = nums[0] % 2 != 0
19+
for (i in 1 until n) {
20+
val flag = nums[i] % 2 == 0
21+
if (flag) {
22+
if (lastodd) {
23+
alter++
24+
lastodd = false
25+
}
26+
even++
27+
} else {
28+
if (!lastodd) {
29+
alter++
30+
lastodd = true
31+
}
32+
odd++
33+
}
34+
}
35+
return max(alter, max(odd, even))
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3201\. Find the Maximum Length of Valid Subsequence I
2+
3+
Medium
4+
5+
You are given an integer array `nums`.
6+
7+
A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies:
8+
9+
* `(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + sub[x - 1]) % 2.`
10+
11+
Return the length of the **longest** **valid** subsequence of `nums`.
12+
13+
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.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3,4]
18+
19+
**Output:** 4
20+
21+
**Explanation:**
22+
23+
The longest valid subsequence is `[1, 2, 3, 4]`.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,1,1,2,1,2]
28+
29+
**Output:** 6
30+
31+
**Explanation:**
32+
33+
The longest valid subsequence is `[1, 2, 1, 2, 1, 2]`.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [1,3]
38+
39+
**Output:** 2
40+
41+
**Explanation:**
42+
43+
The longest valid subsequence is `[1, 3]`.
44+
45+
**Constraints:**
46+
47+
* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
48+
* <code>1 <= nums[i] <= 10<sup>7</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3201_3300.s3202_find_the_maximum_length_of_valid_subsequence_ii
2+
3+
// #Medium #Array #Dynamic_Programming #2024_07_06_Time_255_ms_(97.30%)_Space_49_MB_(78.38%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumLength(nums: IntArray, k: Int): Int {
9+
// dp array to store the index against each possible modulo
10+
val dp = Array(nums.size + 1) { IntArray(k + 1) }
11+
var longest = 0
12+
for (i in nums.indices) {
13+
for (j in 0 until i) {
14+
// Checking the modulo with each previous number
15+
val `val` = (nums[i] + nums[j]) % k
16+
// storing the number of pairs that have the same modulo.
17+
// it would be one more than the number of pairs with the same modulo at the last
18+
// index
19+
dp[i][`val`] = dp[j][`val`] + 1
20+
// Calculating the max seen till now
21+
longest = max(longest, dp[i][`val`])
22+
}
23+
}
24+
// total number of elements in the subsequence would be 1 more than the number of pairs
25+
return longest + 1
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3202\. Find the Maximum Length of Valid Subsequence II
2+
3+
Medium
4+
5+
You are given an integer array `nums` and a **positive** integer `k`.
6+
7+
A subsequence `sub` of `nums` with length `x` is called **valid** if it satisfies:
8+
9+
* `(sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... == (sub[x - 2] + sub[x - 1]) % k.`
10+
11+
Return the length of the **longest** **valid** subsequence of `nums`.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4,5], k = 2
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The longest valid subsequence is `[1, 2, 3, 4, 5]`.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,4,2,3,1,4], k = 3
26+
27+
**Output:** 4
28+
29+
**Explanation:**
30+
31+
The longest valid subsequence is `[1, 4, 1, 4]`.
32+
33+
**Constraints:**
34+
35+
* <code>2 <= nums.length <= 10<sup>3</sup></code>
36+
* <code>1 <= nums[i] <= 10<sup>7</sup></code>
37+
* <code>1 <= k <= 10<sup>3</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package g3201_3300.s3203_find_minimum_diameter_after_merging_two_trees
2+
3+
// #Hard #Depth_First_Search #Breadth_First_Search #Tree #Graph
4+
// #2024_07_06_Time_1156_ms_(100.00%)_Space_119.4_MB_(80.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun minimumDiameterAfterMerge(edges1: Array<IntArray>, edges2: Array<IntArray>): Int {
10+
val n = edges1.size + 1
11+
val g = packU(n, edges1)
12+
val m = edges2.size + 1
13+
val h = packU(m, edges2)
14+
val d1 = diameter(g)
15+
val d2 = diameter(h)
16+
var ans = max(d1[0], d2[0])
17+
ans = max(
18+
((d1[0] + 1) / 2 + ((d2[0] + 1) / 2) + 1),
19+
ans
20+
)
21+
return ans
22+
}
23+
24+
private fun diameter(g: Array<IntArray?>): IntArray {
25+
val n = g.size
26+
val f0: Int
27+
val f1: Int
28+
val d01: Int
29+
val q = IntArray(n)
30+
val ved = BooleanArray(n)
31+
var qp = 0
32+
q[qp++] = 0
33+
ved[0] = true
34+
run {
35+
var i = 0
36+
while (i < qp) {
37+
val cur = q[i]
38+
for (e in g[cur]!!) {
39+
if (!ved[e]) {
40+
ved[e] = true
41+
q[qp++] = e
42+
}
43+
}
44+
i++
45+
}
46+
}
47+
f0 = q[n - 1]
48+
val d = IntArray(n)
49+
qp = 0
50+
ved.fill(false)
51+
q[qp++] = f0
52+
ved[f0] = true
53+
var i = 0
54+
while (i < qp) {
55+
val cur = q[i]
56+
for (e in g[cur]!!) {
57+
if (!ved[e]) {
58+
ved[e] = true
59+
q[qp++] = e
60+
d[e] = d[cur] + 1
61+
}
62+
}
63+
i++
64+
}
65+
f1 = q[n - 1]
66+
d01 = d[f1]
67+
return intArrayOf(d01, f0, f1)
68+
}
69+
70+
private fun packU(n: Int, ft: Array<IntArray>): Array<IntArray?> {
71+
val g = arrayOfNulls<IntArray>(n)
72+
val p = IntArray(n)
73+
for (u in ft) {
74+
p[u[0]]++
75+
p[u[1]]++
76+
}
77+
for (i in 0 until n) {
78+
g[i] = IntArray(p[i])
79+
}
80+
for (u in ft) {
81+
g[u[0]]!![--p[u[0]]] = u[1]
82+
g[u[1]]!![--p[u[1]]] = u[0]
83+
}
84+
return g
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3203\. Find Minimum Diameter After Merging Two Trees
2+
3+
Hard
4+
5+
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.
6+
7+
You must connect one node from the first tree with another node from the second tree with an edge.
8+
9+
Return the **minimum** possible **diameter** of the resulting tree.
10+
11+
The **diameter** of a tree is the length of the _longest_ path between any two nodes in the tree.
12+
13+
**Example 1:**![](https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png)
14+
15+
**Input:** edges1 = [[0,1],[0,2],[0,3]], edges2 = [[0,1]]
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2024/04/22/example211.png)
26+
27+
**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]]
28+
29+
**Output:** 5
30+
31+
**Explanation:**
32+
33+
We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= n, m <= 10<sup>5</sup></code>
38+
* `edges1.length == n - 1`
39+
* `edges2.length == m - 1`
40+
* `edges1[i].length == edges2[i].length == 2`
41+
* <code>edges1[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>
42+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code>
43+
* <code>edges2[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
44+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < m</code>
45+
* The input is generated such that `edges1` and `edges2` represent valid trees.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3200_maximum_height_of_a_triangle
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun maxHeightOfTriangle() {
10+
assertThat(Solution().maxHeightOfTriangle(2, 4), equalTo(3))
11+
}
12+
13+
@Test
14+
fun maxHeightOfTriangle2() {
15+
assertThat(Solution().maxHeightOfTriangle(2, 1), equalTo(2))
16+
}
17+
}

0 commit comments

Comments
 (0)