Skip to content

Added tasks 3200-3203 #1781

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 2 commits into from
Jul 4, 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,33 @@
package g3101_3200.s3200_maximum_height_of_a_triangle;

// #Easy #Array #Enumeration #2024_07_04_Time_1_ms_(86.34%)_Space_40.5_MB_(90.34%)

@SuppressWarnings("java:S135")
public class Solution {
private int count(int v1, int v2) {
int ct = 1;
boolean 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;
}

public int maxHeightOfTriangle(int red, int blue) {
return Math.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,35 @@
package g3201_3300.s3201_find_the_maximum_length_of_valid_subsequence_i;

// #Medium #Array #Dynamic_Programming #2024_07_04_Time_5_ms_(82.23%)_Space_61.7_MB_(91.46%)

class Solution {
public int maximumLength(int[] nums) {
int n = nums.length;
int alter = 1;
int odd = 0;
int even = 0;
if (nums[0] % 2 == 0) {
even++;
} else {
odd++;
}
boolean lastodd = nums[0] % 2 != 0;
for (int i = 1; i < n; i++) {
boolean flag = nums[i] % 2 == 0;
if (flag) {
if (lastodd) {
alter++;
lastodd = false;
}
even++;
} else {
if (!lastodd) {
alter++;
lastodd = true;
}
odd++;
}
}
return Math.max(alter, Math.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,25 @@
package g3201_3300.s3202_find_the_maximum_length_of_valid_subsequence_ii;

// #Medium #Array #Dynamic_Programming #2024_07_04_Time_34_ms_(92.46%)_Space_51_MB_(45.95%)

public class Solution {
public int maximumLength(int[] nums, int k) {
// dp array to store the index against each possible modulo
int[][] dp = new int[nums.length + 1][k + 1];
int longest = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
// Checking the modulo with each previous number
int 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 = Math.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,77 @@
package g3201_3300.s3203_find_minimum_diameter_after_merging_two_trees;

// #Hard #Tree #Graph #Depth_First_Search #Breadth_First_Search
// #2024_07_04_Time_29_ms_(99.83%)_Space_110.9_MB_(86.36%)

import java.util.Arrays;

public class Solution {
public int minimumDiameterAfterMerge(int[][] edges1, int[][] edges2) {
int n = edges1.length + 1;
int[][] g = packU(n, edges1);
int m = edges2.length + 1;
int[][] h = packU(m, edges2);
int[] d1 = diameter(g);
int[] d2 = diameter(h);
int ans = Math.max(d1[0], d2[0]);
ans = Math.max((d1[0] + 1) / 2 + (d2[0] + 1) / 2 + 1, ans);
return ans;
}

public static int[] diameter(int[][] g) {
int n = g.length;
int f0;
int f1;
int d01;
int[] q = new int[n];
boolean[] ved = new boolean[n];
int qp = 0;
q[qp++] = 0;
ved[0] = true;
for (int i = 0; i < qp; i++) {
int cur = q[i];
for (int e : g[cur]) {
if (!ved[e]) {
ved[e] = true;
q[qp++] = e;
}
}
}
f0 = q[n - 1];
int[] d = new int[n];
qp = 0;
Arrays.fill(ved, false);
q[qp++] = f0;
ved[f0] = true;
for (int i = 0; i < qp; i++) {
int cur = q[i];
for (int e : g[cur]) {
if (!ved[e]) {
ved[e] = true;
q[qp++] = e;
d[e] = d[cur] + 1;
}
}
}
f1 = q[n - 1];
d01 = d[f1];
return new int[] {d01, f0, f1};
}

public static int[][] packU(int n, int[][] ft) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int[] u : ft) {
p[u[0]]++;
p[u[1]]++;
}
for (int i = 0; i < n; i++) {
g[i] = new int[p[i]];
}
for (int[] u : 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,18 @@
package g3101_3200.s3200_maximum_height_of_a_triangle;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void maxHeightOfTriangle() {
assertThat(new Solution().maxHeightOfTriangle(2, 4), equalTo(3));
}

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