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 563, 564, 565
  • Loading branch information
ThanhNIT committed Jan 5, 2022
commit 3a2e62957c4d28685a4f6226aa7ba60332f34889
21 changes: 21 additions & 0 deletions src/main/java/g0501_0600/s0563_binary_tree_tilt/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package g0501_0600.s0563_binary_tree_tilt;

// #Easy #Depth_First_Search #Tree #Binary_Tree

import com_github_leetcode.TreeNode;

public class Solution {
private int sum=0;
private int sumTilt(TreeNode root){
if(root==null)return 0;
int ls=sumTilt(root.left);
int rs=sumTilt(root.right);
sum+=Math.abs(ls-rs);
return ls+rs+root.val;
}
public int findTilt(TreeNode root) {
sum=0;
sumTilt(root);
return sum;
}
}
53 changes: 53 additions & 0 deletions src/main/java/g0501_0600/s0563_binary_tree_tilt/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
563\. Binary Tree Tilt

Easy

Given the `root` of a binary tree, return _the sum of every tree node's **tilt**._

The **tilt** of a tree node is the **absolute difference** between the sum of all left subtree node **values** and all right subtree node **values**. If a node does not have a left child, then the sum of the left subtree node **values** is treated as `0`. The rule is similar if the node does not have a right child.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/20/tilt1.jpg)

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

**Output:** 1

**Explanation:**

Tilt of node 2 : |0-0| = 0 (no children)
Tilt of node 3 : |0-0| = 0 (no children)
Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3)
Sum of every tilt : 0 + 0 + 1 = 1

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/10/20/tilt2.jpg)

**Input:** root = [4,2,9,3,5,null,7]

**Output:** 15

**Explanation:**

Tilt of node 3 : |0-0| = 0 (no children)
Tilt of node 5 : |0-0| = 0 (no children)
Tilt of node 7 : |0-0| = 0 (no children)
Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5)
Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7)
Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16)
Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/10/20/tilt3.jpg)

**Input:** root = [21,7,14,1,1,2,2,3,3]

**Output:** 9

**Constraints:**

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

// #Hard #String #Math

public class Solution {
public String nearestPalindromic(String n) {
if (n.length() == 1){
return String.valueOf(Integer.parseInt(n) - 1);
}
long num = Long.parseLong(n);
int offset = (int) Math.pow(10, n.length() / 2);
long first = isPalindrome(n)?
palindromeGenerator(num + offset, n.length()) :
palindromeGenerator(num, n.length());
long second = first < num ?
palindromeGenerator(num + offset, n.length()) :
palindromeGenerator(num - offset, n.length());

if (first + second == 2 * num){
return first < second? String.valueOf(first) : String.valueOf(second);
}

return Math.abs(num - first) > Math.abs(num - second)? String.valueOf(second) : String.valueOf(first);
}

private long palindromeGenerator(long num, int length) {
if (num < 10) return 9;

int numOfDigits = String.valueOf(num).length();
if (numOfDigits > length) {
return ((long) Math.pow(10, numOfDigits - 1) + 1);
}else if (numOfDigits < length)
return ((long) Math.pow(10, numOfDigits) - 1);

num = num - num % (long) Math.pow(10, numOfDigits / 2);
long temp = num;
for (int j = 0; j < numOfDigits / 2; j++) {
long digit = (long) Math.pow(10, numOfDigits - j - 1);
num += (int) ((temp / digit) * Math.pow(10, j));
temp = temp % digit;
}

return num;
}

private boolean isPalindrome(String str) {
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
564\. Find the Closest Palindrome

Hard

Given a string `n` representing an integer, return _the closest integer (not including itself), which is a palindrome_. If there is a tie, return _**the smaller one**_.

The closest is defined as the absolute difference minimized between two integers.

**Example 1:**

**Input:** n = "123"

**Output:** "121"

**Example 2:**

**Input:** n = "1"

**Output:** "0"

**Explanation:** 0 and 2 are the closest palindromes but we return the smallest which is 0.

**Constraints:**

* `1 <= n.length <= 18`
* `n` consists of only digits.
* `n` does not have leading zeros.
* `n` is representing an integer in the range <code>[1, 10<sup>18</sup> - 1]</code>.
26 changes: 26 additions & 0 deletions src/main/java/g0501_0600/s0565_array_nesting/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package g0501_0600.s0565_array_nesting;

// #Medium #Array #Depth_First_Search

public class Solution {
public int arrayNesting(int[] nums) {
int index = -1;
int value = 0;
int maxLen = 0;
int len = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != -1) {
index = i;
len = 0;
while (nums[index] != -1) {
value = nums[index];
nums[index] = -1;
index = value;
len++;
}
maxLen = Math.max(len, maxLen);
}
}
return maxLen;
}
}
39 changes: 39 additions & 0 deletions src/main/java/g0501_0600/s0565_array_nesting/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
565\. Array Nesting

Medium

You are given an integer array `nums` of length `n` where `nums` is a permutation of the numbers in the range `[0, n - 1]`.

You should build a set `s[k] = {nums[k], nums[nums[k]], nums[nums[nums[k]]], ... }` subjected to the following rule:

* The first element in `s[k]` starts with the selection of the element `nums[k]` of `index = k`.
* The next element in `s[k]` should be `nums[nums[k]]`, and then `nums[nums[nums[k]]]`, and so on.
* We stop adding right before a duplicate element occurs in `s[k]`.

Return _the longest length of a set_ `s[k]`.

**Example 1:**

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

**Output:** 4

**Explanation:**

nums[0] = 5, nums[1] = 4, nums[2] = 0, nums[3] = 3, nums[4] = 1, nums[5] = 6, nums[6] = 2.

One of the longest sets s[k]:

s[0] = {nums[0], nums[5], nums[6], nums[2]} = {5, 6, 2, 0}

**Example 2:**

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

**Output:** 1

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* `0 <= nums[i] < nums.length`
* All the values of `nums` are **unique**.
31 changes: 31 additions & 0 deletions src/test/java/g0501_0600/s0563_binary_tree_tilt/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g0501_0600.s0563_binary_tree_tilt;

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

import java.util.ArrayList;
import java.util.Arrays;

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

class SolutionTest {
@Test
void findTilt() {
TreeNode treeNode = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(1,2,3)));
assertThat(new Solution().findTilt(treeNode), equalTo(1));
}

@Test
void findTilt2() {
TreeNode treeNode = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(4,2,9,3,5,null,7)));
assertThat(new Solution().findTilt(treeNode), equalTo(15));
}

@Test
void findTilt3() {
TreeNode treeNode = TreeUtils.constructBinaryTree(new ArrayList<>(Arrays.asList(21,7,14,1,1,2,2,3,3)));
assertThat(new Solution().findTilt(treeNode), equalTo(9));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0501_0600.s0564_find_the_closest_palindrome;

import org.junit.jupiter.api.Test;

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

class SolutionTest {
@Test
void nearestPalindromic() {
assertThat(new Solution().nearestPalindromic("123"), equalTo("121"));
}

@Test
void nearestPalindromic2() {
assertThat(new Solution().nearestPalindromic("1"), equalTo("0"));
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0501_0600/s0565_array_nesting/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g0501_0600.s0565_array_nesting;

import org.junit.jupiter.api.Test;

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

class SolutionTest {
@Test
void arrayNesting() {
assertThat(new Solution().arrayNesting(new int[]{5,4,0,3,1,6,2}), equalTo(4));
}

@Test
void arrayNesting2() {
assertThat(new Solution().arrayNesting(new int[]{0,1,2}), equalTo(1));
}
}