Skip to content

Commit 3a2a7d0

Browse files
authored
Added tasks 2384, 2385.
1 parent 9eb162c commit 3a2a7d0

File tree

7 files changed

+240
-0
lines changed

7 files changed

+240
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,8 @@ implementation 'com.github.javadev:leetcode-in-java:1.12'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2385 |[Amount of Time for Binary Tree to Be Infected](src/main/java/g2301_2400/s2385_amount_of_time_for_binary_tree_to_be_infected/Solution.java)| Medium | Tree, Binary_Tree, Depth_First_Search, Breadth_First_Search | 20 | 100.00
1852+
| 2384 |[Largest Palindromic Number](src/main/java/g2301_2400/s2384_largest_palindromic_number/Solution.java)| Medium | String, Hash_Table, Greedy | 26 | 100.00
18511853
| 2383 |[Minimum Hours of Training to Win a Competition](src/main/java/g2301_2400/s2383_minimum_hours_of_training_to_win_a_competition/Solution.java)| Easy | Array, Greedy | 0 | 100.00
18521854
| 2382 |[Maximum Segment Sum After Removals](src/main/java/g2301_2400/s2382_maximum_segment_sum_after_removals/Solution.java)| Hard | Array, Prefix_Sum, Union_Find, Ordered_Set | 28 | 100.00
18531855
| 2381 |[Shifting Letters II](src/main/java/g2301_2400/s2381_shifting_letters_ii/Solution.java)| Medium | Array, String, Prefix_Sum | 10 | 75.00
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g2301_2400.s2384_largest_palindromic_number;
2+
3+
// #Medium #String #Hash_Table #Greedy #2022_08_25_Time_26_ms_(100.00%)_Space_54.8_MB_(100.00%)
4+
5+
public class Solution {
6+
public String largestPalindromic(String num) {
7+
int[] count = new int[10];
8+
int center = -1;
9+
StringBuilder first = new StringBuilder();
10+
StringBuilder second;
11+
for (char c : num.toCharArray()) {
12+
count[c - '0']++;
13+
}
14+
int c = 0;
15+
for (int i = 9; i >= 0; i--) {
16+
c = 0;
17+
if (count[i] % 2 == 1 && center == -1) {
18+
center = i;
19+
}
20+
if (first.length() == 0 && i == 0) {
21+
continue;
22+
}
23+
while (c < count[i] / 2) {
24+
first.append(String.valueOf(i));
25+
c++;
26+
}
27+
}
28+
second = new StringBuilder(first.toString());
29+
if (center != -1) {
30+
first.append(center);
31+
}
32+
first.append(second.reverse().toString());
33+
return first.length() == 0 ? "0" : first.toString();
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2384\. Largest Palindromic Number
2+
3+
Medium
4+
5+
You are given a string `num` consisting of digits only.
6+
7+
Return _the **largest palindromic** integer (in the form of a string) that can be formed using digits taken from_ `num`. It should not contain **leading zeroes**.
8+
9+
**Notes:**
10+
11+
* You do **not** need to use all the digits of `num`, but you must use **at least** one digit.
12+
* The digits can be reordered.
13+
14+
**Example 1:**
15+
16+
**Input:** num = "444947137"
17+
18+
**Output:** "7449447"
19+
20+
**Explanation:**
21+
22+
Use the digits "4449477" from "<ins>**44494**</ins><ins>**7**</ins>13<ins>**7**</ins>" to form the palindromic integer "7449447".
23+
24+
It can be shown that "7449447" is the largest palindromic integer that can be formed.
25+
26+
**Example 2:**
27+
28+
**Input:** num = "00009"
29+
30+
**Output:** "9"
31+
32+
**Explanation:**
33+
34+
It can be shown that "9" is the largest palindromic integer that can be formed.
35+
36+
Note that the integer returned should not contain leading zeroes.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= num.length <= 10<sup>5</sup></code>
41+
* `num` consists of digits.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package g2301_2400.s2385_amount_of_time_for_binary_tree_to_be_infected;
2+
3+
// #Medium #Tree #Binary_Tree #Depth_First_Search #Breadth_First_Search
4+
// #2022_08_25_Time_20_ms_(100.00%)_Space_111.7_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode;
7+
8+
/*
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
public class Solution {
24+
private int max = 0;
25+
26+
public int amountOfTime(TreeNode root, int start) {
27+
dfs(root, start, new Distance(-1));
28+
return max;
29+
}
30+
31+
private int dfs(TreeNode root, int start, Distance l) {
32+
if (root == null) {
33+
return 0;
34+
}
35+
Distance ld = new Distance(-1);
36+
Distance rd = new Distance(-1);
37+
int left = dfs(root.left, start, ld);
38+
int right = dfs(root.right, start, rd);
39+
if (l.val == -1 && start == root.val) {
40+
max = Math.max(left, right);
41+
l.val = 1;
42+
}
43+
if (ld.val != -1) {
44+
max = Math.max(max, ld.val + right);
45+
l.val = ld.val + 1;
46+
} else if (rd.val != -1) {
47+
max = Math.max(max, rd.val + left);
48+
l.val = rd.val + 1;
49+
}
50+
return Math.max(left, right) + 1;
51+
}
52+
53+
private static class Distance {
54+
int val;
55+
56+
Distance(int v) {
57+
this.val = v;
58+
}
59+
}
60+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2385\. Amount of Time for Binary Tree to Be Infected
2+
3+
Medium
4+
5+
You are given the `root` of a binary tree with **unique** values, and an integer `start`. At minute `0`, an **infection** starts from the node with value `start`.
6+
7+
Each minute, a node becomes infected if:
8+
9+
* The node is currently uninfected.
10+
* The node is adjacent to an infected node.
11+
12+
Return _the number of minutes needed for the entire tree to be infected._
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png)
17+
18+
**Input:** root = [1,5,3,null,4,10,6,9,2], start = 3
19+
20+
**Output:** 4
21+
22+
**Explanation:** The following nodes are infected during:
23+
24+
- Minute 0: Node 3
25+
26+
- Minute 1: Nodes 1, 10 and 6
27+
28+
- Minute 2: Node 5
29+
30+
- Minute 3: Node 4
31+
32+
- Minute 4: Nodes 9 and 2
33+
34+
It takes 4 minutes for the whole tree to be infected so we return 4.
35+
36+
**Example 2:**
37+
38+
![](https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png)
39+
40+
**Input:** root = [1], start = 1
41+
42+
**Output:** 0
43+
44+
**Explanation:** At minute 0, the only node in the tree is infected so we return 0.
45+
46+
**Constraints:**
47+
48+
* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
49+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
50+
* Each node has a **unique** value.
51+
* A node with a value of `start` exists in the tree.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2384_largest_palindromic_number;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void largestPalindromic() {
11+
assertThat(new Solution().largestPalindromic("444947137"), equalTo("7449447"));
12+
}
13+
14+
@Test
15+
void largestPalindromic2() {
16+
assertThat(new Solution().largestPalindromic("00009"), equalTo("9"));
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2301_2400.s2385_amount_of_time_for_binary_tree_to_be_infected;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void amountOfTime() {
13+
assertThat(
14+
new Solution()
15+
.amountOfTime(
16+
TreeNode.create(Arrays.asList(1, 5, 3, null, 4, 10, 6, 9, 2)), 3),
17+
equalTo(4));
18+
}
19+
20+
@Test
21+
void amountOfTime2() {
22+
assertThat(new Solution().amountOfTime(TreeNode.create(Arrays.asList(1)), 1), equalTo(0));
23+
}
24+
25+
@Test
26+
void amountOfTime3() {
27+
assertThat(
28+
new Solution()
29+
.amountOfTime(
30+
TreeNode.create(Arrays.asList(1, 2, null, 3, null, 4, null, 5)), 4),
31+
equalTo(3));
32+
}
33+
}

0 commit comments

Comments
 (0)