Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package g1301_1400.s1325_delete_leaves_with_a_given_value;

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
// #2022_03_19_Time_2_ms_(33.61%)_Space_45_MB_(25.67%)

import com_github_leetcode.TreeNode;

public class Solution {
public TreeNode removeLeafNodes(TreeNode root, int target) {
while (hasTargetLeafNodes(root, target)) {
root = removeLeafNodes(target, root);
}
return root;
}

private TreeNode removeLeafNodes(int target, TreeNode root) {
if (root == null) {
return root;
}
if (root.val == target && root.left == null && root.right == null) {
root = null;
return root;
}
if (root.left != null
&& root.left.val == target
&& root.left.left == null
&& root.left.right == null) {
root.left = null;
}
if (root.right != null
&& root.right.val == target
&& root.right.left == null
&& root.right.right == null) {
root.right = null;
}
removeLeafNodes(target, root.left);
removeLeafNodes(target, root.right);
return root;
}

private boolean hasTargetLeafNodes(TreeNode root, int target) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null && root.val == target) {
return true;
}
return hasTargetLeafNodes(root.left, target) || hasTargetLeafNodes(root.right, target);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1325\. Delete Leaves With a Given Value

Medium

Given a binary tree `root` and an integer `target`, delete all the **leaf nodes** with value `target`.

Note that once you delete a leaf node with value `target`**,** if its parent node becomes a leaf node and has the value `target`, it should also be deleted (you need to continue doing that until you cannot).

**Example 1:**

**![](https://assets.leetcode.com/uploads/2020/01/09/sample_1_1684.png)**

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

**Output:** [1,null,3,null,4]

**Explanation:** Leaf nodes in green with value (target = 2) are removed (Picture in left). After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).

**Example 2:**

**![](https://assets.leetcode.com/uploads/2020/01/09/sample_2_1684.png)**

**Input:** root = [1,3,3,3,2], target = 3

**Output:** [1,3,null,null,2]

**Example 3:**

**![](https://assets.leetcode.com/uploads/2020/01/15/sample_3_1684.png)**

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

**Output:** [1]

**Explanation:** Leaf nodes in green with value (target = 2) are removed at each step.

**Constraints:**

* The number of nodes in the tree is in the range `[1, 3000]`.
* `1 <= Node.val, target <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g1301_1400.s1326_minimum_number_of_taps_to_open_to_water_a_garden;

// #Hard #Array #Dynamic_Programming #Greedy #2022_03_19_Time_3_ms_(90.04%)_Space_47.7_MB_(59.59%)

public class Solution {
public int minTaps(int n, int[] ranges) {
if (n == 0 || ranges.length == 0) {
return n == 0 ? 0 : -1;
}
int[] dp = new int[n + 1];

int nxtLargest = 0;
int current = 0;
int amount = 0;
for (int i = 0; i < ranges.length; i++) {
if (ranges[i] > 0) {
int ind = Math.max(0, i - ranges[i]);
dp[ind] = Math.max(dp[ind], i + ranges[i]);
}
}
for (int i = 0; i <= n; i++) {
nxtLargest = Math.max(nxtLargest, dp[i]);
if (i == current && i < n) {
current = nxtLargest;
amount++;
}
if (current < i) {
return -1;
}
}
return amount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
1326\. Minimum Number of Taps to Open to Water a Garden

Hard

There is a one-dimensional garden on the x-axis. The garden starts at the point `0` and ends at the point `n`. (i.e The length of the garden is `n`).

There are `n + 1` taps located at points `[0, 1, ..., n]` in the garden.

Given an integer `n` and an integer array `ranges` of length `n + 1` where `ranges[i]` (0-indexed) means the `i-th` tap can water the area `[i - ranges[i], i + ranges[i]]` if it was open.

Return _the minimum number of taps_ that should be open to water the whole garden, If the garden cannot be watered return **\-1**.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/01/16/1685_example_1.png)

**Input:** n = 5, ranges = [3,4,1,1,0,0]

**Output:** 1

**Explanation:** The tap at point 0 can cover the interval [-3,3]

The tap at point 1 can cover the interval [-3,5]

The tap at point 2 can cover the interval [1,3]

The tap at point 3 can cover the interval [2,4]

The tap at point 4 can cover the interval [4,4]

The tap at point 5 can cover the interval [5,5]

Opening Only the second tap will water the whole garden [0,5]

**Example 2:**

**Input:** n = 3, ranges = [0,0,0,0]

**Output:** -1

**Explanation:** Even if you activate all the four taps you cannot water the whole garden.

**Constraints:**

* <code>1 <= n <= 10<sup>4</sup></code>
* `ranges.length == n + 1`
* `0 <= ranges[i] <= 100`
25 changes: 25 additions & 0 deletions src/main/java/g1301_1400/s1328_break_a_palindrome/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package g1301_1400.s1328_break_a_palindrome;

// #Medium #String #Greedy #2022_03_19_Time_0_ms_(100.00%)_Space_42.3_MB_(21.67%)

public class Solution {
public String breakPalindrome(String palindrome) {
if (palindrome.length() <= 1) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < palindrome.length(); i++) {
char ch = palindrome.charAt(i);
if (ch != 'a' && i != palindrome.length() - 1 - i) {
sb.append('a');
sb.append(palindrome.substring(i + 1));
return sb.toString();
} else {
sb.append(ch);
}
}
sb.deleteCharAt(palindrome.length() - 1);
sb.append('b');
return sb.toString();
}
}
30 changes: 30 additions & 0 deletions src/main/java/g1301_1400/s1328_break_a_palindrome/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1328\. Break a Palindrome

Medium

Given a palindromic string of lowercase English letters `palindrome`, replace **exactly one** character with any lowercase English letter so that the resulting string is **not** a palindrome and that it is the **lexicographically smallest** one possible.

Return _the resulting string. If there is no way to replace a character to make it not a palindrome, return an **empty string**._

A string `a` is lexicographically smaller than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly smaller than the corresponding character in `b`. For example, `"abcc"` is lexicographically smaller than `"abcd"` because the first position they differ is at the fourth character, and `'c'` is smaller than `'d'`.

**Example 1:**

**Input:** palindrome = "abccba"

**Output:** "aaccba"

**Explanation:** There are many ways to make "abccba" not a palindrome, such as "zbccba", "aaccba", and "abacba". Of all the ways, "aaccba" is the lexicographically smallest.

**Example 2:**

**Input:** palindrome = "a"

**Output:** ""

**Explanation:** There is no way to replace a single character to make "a" not a palindrome, so return an empty string.

**Constraints:**

* `1 <= palindrome.length <= 1000`
* `palindrome` consists of only lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g1301_1400.s1329_sort_the_matrix_diagonally;

// #Medium #Array #Sorting #Matrix #2022_03_19_Time_15_ms_(26.03%)_Space_47.7_MB_(56.76%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
public int[][] diagonalSort(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[][] sorted = new int[m][n];
for (int i = m - 1; i >= 0; i--) {
int iCopy = i;
List<Integer> list = new ArrayList<>();
for (int j = 0; j < n && iCopy < m; j++, iCopy++) {
list.add(mat[iCopy][j]);
}
Collections.sort(list);
iCopy = i;
for (int j = 0; j < n && iCopy < m; j++, iCopy++) {
sorted[iCopy][j] = list.get(j);
}
}

for (int j = n - 1; j > 0; j--) {
int jCopy = j;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < m && jCopy < n; i++, jCopy++) {
list.add(mat[i][jCopy]);
}
Collections.sort(list);
jCopy = j;
for (int i = 0; i < m && jCopy < n; i++, jCopy++) {
sorted[i][jCopy] = list.get(i);
}
}
return sorted;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
1329\. Sort the Matrix Diagonally

Medium

A **matrix diagonal** is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the **matrix diagonal** starting from `mat[2][0]`, where `mat` is a `6 x 3` matrix, includes cells `mat[2][0]`, `mat[3][1]`, and `mat[4][2]`.

Given an `m x n` matrix `mat` of integers, sort each **matrix diagonal** in ascending order and return _the resulting matrix_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png)

**Input:** mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]

**Output:** [[1,1,1,1],[1,2,2,2],[1,2,3,3]]

**Example 2:**

**Input:** mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]

**Output:** [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]

**Constraints:**

* `m == mat.length`
* `n == mat[i].length`
* `1 <= m, n <= 100`
* `1 <= mat[i][j] <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g1301_1400.s1330_reverse_subarray_to_maximize_array_value;

// #Hard #Array #Math #Greedy #2022_03_19_Time_9_ms_(88.00%)_Space_63.9_MB_(20.00%)

public class Solution {

private int getAbsoluteDifference(int a, int b) {
return Math.abs(a - b);
}

public int maxValueAfterReverse(int[] nums) {
int n = nums.length;
int result = 0;
for (int i = 0; i < n - 1; i++) {
result += getAbsoluteDifference(nums[i], nums[i + 1]);
}

int minLine = Integer.MIN_VALUE;
int maxLine = Integer.MAX_VALUE;
for (int i = 0; i < n - 1; i++) {
minLine = Math.max(minLine, Math.min(nums[i], nums[i + 1]));
maxLine = Math.min(maxLine, Math.max(nums[i], nums[i + 1]));
}
int diff = Math.max(0, (minLine - maxLine) * 2);
for (int i = 1; i < n - 1; i++) {
diff =
Math.max(
diff,
getAbsoluteDifference(nums[0], nums[i + 1])
- getAbsoluteDifference(nums[i], nums[i + 1]));
}

for (int i = 0; i < n - 1; i++) {
diff =
Math.max(
diff,
getAbsoluteDifference(nums[n - 1], nums[i])
- getAbsoluteDifference(nums[i + 1], nums[i]));
}
return result + diff;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
1330\. Reverse Subarray To Maximize Array Value

Hard

You are given an integer array `nums`. The _value_ of this array is defined as the sum of `|nums[i] - nums[i + 1]|` for all `0 <= i < nums.length - 1`.

You are allowed to select any subarray of the given array and reverse it. You can perform this operation **only once**.

Find maximum possible value of the final array.

**Example 1:**

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

**Output:** 10

**Explanation:** By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.

**Example 2:**

**Input:** nums = [2,4,9,24,2,1,10]

**Output:** 68

**Constraints:**

* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Loading