Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tasks 1372, 1373, 1374, 1375
  • Loading branch information
ThanhNIT committed Mar 21, 2022
commit 10881019d9ee91607b1dc854f78cea0ac575cfa0
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g1301_1400.s1372_longest_zigzag_path_in_a_binary_tree;

// #Medium #Dynamic_Programming #Depth_First_Search #Tree #Binary_Tree
// #2022_03_21_Time_9_ms_(64.47%)_Space_74_MB_(56.45%)

import com_github_leetcode.TreeNode;

public class Solution {
private int maxLength = 0;

public int longestZigZag(TreeNode root) {
dfs(root, true);
return maxLength;
}

private int dfs(TreeNode root, boolean isLeft) {
if (root == null) {
return 0;
}
int left = dfs(root.left, false);
int right = dfs(root.right, true);
maxLength = Math.max(maxLength, left);
maxLength = Math.max(maxLength, right);
return 1 + (isLeft ? left : right);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
1372\. Longest ZigZag Path in a Binary Tree

Medium

You are given the `root` of a binary tree.

A ZigZag path for a binary tree is defined as follow:

* Choose **any** node in the binary tree and a direction (right or left).
* If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
* Change the direction from right to left or from left to right.
* Repeat the second and third steps until you can't move in the tree.

Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).

Return _the longest **ZigZag** path contained in that tree_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/01/22/sample_1_1702.png)

**Input:** root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]

**Output:** 3

**Explanation:** Longest ZigZag path in blue nodes (right -> left -> right).

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/01/22/sample_2_1702.png)

**Input:** root = [1,1,1,null,1,null,null,1,1,null,1]

**Output:** 4

**Explanation:** Longest ZigZag path in blue nodes (left -> right -> left -> right).

**Example 3:**

**Input:** root = [1]

**Output:** 0

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.
* `1 <= Node.val <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package g1301_1400.s1373_maximum_sum_bst_in_binary_tree;

// #Hard #Dynamic_Programming #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
// #2022_03_22_Time_15_ms_(47.56%)_Space_81.5_MB_(11.51%)

import com_github_leetcode.TreeNode;

public class Solution {
public int maxSumBST(TreeNode root) {
isBST temp = checkBST(root);

return Math.max(temp.maxSum, 0);
}

private static class isBST {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
boolean isBST = true;
int sum = 0;
int maxSum = Integer.MIN_VALUE;
}

private isBST checkBST(TreeNode root) {
if(root == null) {
return new isBST();
}

isBST lp = checkBST(root.left);
isBST rp = checkBST(root.right);

isBST mp = new isBST();
mp.max = Math.max(root.val, Math.max(lp.max, rp.max));
mp.min = Math.min(root.val, Math.min(lp.min, rp.min));
mp.sum = lp.sum + rp.sum + root.val;

boolean check = root.val > lp.max && root.val < rp.min;
if(lp.isBST && rp.isBST && check) {
mp.isBST = true;
int tempMax = Math.max(mp.sum, Math.max(lp.sum, rp.sum));
mp.maxSum = Math.max(tempMax, Math.max(lp.maxSum, rp.maxSum));

} else {
mp.isBST = false;
mp.maxSum = Math.max(lp.maxSum, rp.maxSum);
}
return mp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
1373\. Maximum Sum BST in Binary Tree

Hard

Given a **binary tree** `root`, return _the maximum sum of all keys of **any** sub-tree which is also a Binary Search Tree (BST)_.

Assume a BST is defined as follows:

* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* Both the left and right subtrees must also be binary search trees.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/01/30/sample_1_1709.png)

**Input:** root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]

**Output:** 20

**Explanation:** Maximum sum in a valid Binary search tree is obtained in root node with key equal to 3.

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/01/30/sample_2_1709.png)

**Input:** root = [4,3,null,1,2]

**Output:** 2

**Explanation:** Maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2.

**Example 3:**

**Input:** root = [-4,-2,-5]

**Output:** 0

**Explanation:** All values are negatives. Return an empty BST.

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 4 * 10<sup>4</sup>]</code>.
* <code>-4 * 10<sup>4</sup> <= Node.val <= 4 * 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g1301_1400.s1374_generate_a_string_with_characters_that_have_odd_counts;

// #Easy #String #2022_03_22_Time_1_ms_(90.52%)_Space_39.4_MB_(87.85%)

public class Solution {
public String generateTheString(int n) {
StringBuilder sb = new StringBuilder();
if (n > 1 && n % 2 == 0) {
while (n-- > 1) {
sb.append("a");
}
} else if (n > 1) {
while (n-- > 2) {
sb.append("a");
}
sb.append("b");
}
sb.append("z");
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1374\. Generate a String With Characters That Have Odd Counts

Easy

Given an integer `n`, _return a string with `n` characters such that each character in such string occurs **an odd number of times**_.

The returned string must contain only lowercase English letters. If there are multiples valid strings, return **any** of them.

**Example 1:**

**Input:** n = 4

**Output:** "pppz"

**Explanation:** "pppz" is a valid string since the character 'p' occurs three times and the character 'z' occurs once. Note that there are many other valid strings such as "ohhh" and "love".

**Example 2:**

**Input:** n = 2

**Output:** "xy"

**Explanation:** "xy" is a valid string since the characters 'x' and 'y' occur once. Note that there are many other valid strings such as "ag" and "ur".

**Example 3:**

**Input:** n = 7

**Output:** "holasss"

**Constraints:**

* `1 <= n <= 500`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g1301_1400.s1375_number_of_times_binary_string_is_prefix_aligned;

// #Medium #Array #2022_03_22_Time_2_ms_(89.02%)_Space_64.5_MB_(21.14%)

class Solution {
public int numTimesAllBlue(int[] flips) {

int ans = 0;
int max = 0;
for(int i=0; i<flips.length; i++){
max = Math.max(max, flips[i]);

if(max == i+1){
++ans;
}

}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
1375\. Number of Times Binary String Is Prefix-Aligned

Medium

You have a **1-indexed** binary string of length `n` where all the bits are `0` initially. We will flip all the bits of this binary string (i.e., change them from `0` to `1`) one by one. You are given a **1-indexed** integer array `flips` where `flips[i]` indicates that the bit at index `i` will be flipped in the <code>i<sup>th</sup></code> step.

A binary string is **prefix-aligned** if, after the <code>i<sup>th</sup></code> step, all the bits in the **inclusive** range `[1, i]` are ones and all the other bits are zeros.

Return _the number of times the binary string is **prefix-aligned** during the flipping process_.

**Example 1:**

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

**Output:** 2

**Explanation:** The binary string is initially "00000".

After applying step 1: The string becomes "00100", which is not prefix-aligned.

After applying step 2: The string becomes "01100", which is not prefix-aligned.

After applying step 3: The string becomes "01110", which is not prefix-aligned.

After applying step 4: The string becomes "11110", which is prefix-aligned.

After applying step 5: The string becomes "11111", which is prefix-aligned.

We can see that the string was prefix-aligned 2 times, so we return 2.

**Example 2:**

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

**Output:** 1

**Explanation:** The binary string is initially "0000".

After applying step 1: The string becomes "0001", which is not prefix-aligned.

After applying step 2: The string becomes "1001", which is not prefix-aligned.

After applying step 3: The string becomes "1101", which is not prefix-aligned.

After applying step 4: The string becomes "1111", which is prefix-aligned.

We can see that the string was prefix-aligned 1 time, so we return 1.

**Constraints:**

* `n == flips.length`
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
* `flips` is a permutation of the integers in the range `[1, n]`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g1301_1400.s1372_longest_zigzag_path_in_a_binary_tree;

import com_github_leetcode.TreeNode;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;

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

class SolutionTest {
@Test
void longestZigZag() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1));
assertThat(new Solution().longestZigZag(treeNode), equalTo(3));
}

@Test
void longestZigZag2() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1,1,1,null,1,null,null,1,1,null,1));
assertThat(new Solution().longestZigZag(treeNode), equalTo(4));
}

@Test
void longestZigZag3() {
TreeNode treeNode = TreeNode.create(Collections.singletonList(1));
assertThat(new Solution().longestZigZag(treeNode), equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g1301_1400.s1373_maximum_sum_bst_in_binary_tree;

import com_github_leetcode.TreeNode;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

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

class SolutionTest {
@Test
void maxSumBST() {
TreeNode treeNode = TreeNode.create(Arrays.asList(1,4,3,2,4,2,5,null,null,null,null,null,null,4,6));
assertThat(new Solution().maxSumBST(treeNode), equalTo(20));
}

@Test
void maxSumBST2() {
TreeNode treeNode = TreeNode.create(Arrays.asList(4,3,null,1,2));
assertThat(new Solution().maxSumBST(treeNode), equalTo(2));
}

@Test
void maxSumBST3() {
TreeNode treeNode = TreeNode.create(Arrays.asList(-4,-2,-5));
assertThat(new Solution().maxSumBST(treeNode), equalTo(0));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package g1301_1400.s1374_generate_a_string_with_characters_that_have_odd_counts;

import org.junit.jupiter.api.Test;

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

class SolutionTest {
@Test
void generateTheString() {
assertThat(new Solution().generateTheString(4), equalTo("aaaz"));
}

@Test
void generateTheString2() {
assertThat(new Solution().generateTheString(2), equalTo("az"));
}

@Test
void generateTheString3() {
assertThat(new Solution().generateTheString(7), equalTo("aaaaabz"));
}
}
Loading