Skip to content

Commit cacc513

Browse files
authored
Added tasks 2439, 2440, 2441, 2442, 2443.
1 parent f931a37 commit cacc513

File tree

15 files changed

+442
-0
lines changed

15 files changed

+442
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g2401_2500.s2439_minimize_maximum_of_array;
2+
3+
// #Medium #Array #Dynamic_Programming #Greedy #Binary_Search #Prefix_Sum
4+
// #2022_12_13_Time_10_ms_(90.25%)_Space_83_MB_(30.03%)
5+
6+
public class Solution {
7+
public int minimizeArrayValue(int[] nums) {
8+
long max = 0;
9+
long sum = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
sum += nums[i];
12+
max = Math.max(max, (sum + i) / (i + 1));
13+
}
14+
return (int) max;
15+
}
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2439\. Minimize Maximum of Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` comprising of `n` non-negative integers.
6+
7+
In one operation, you must:
8+
9+
* Choose an integer `i` such that `1 <= i < n` and `nums[i] > 0`.
10+
* Decrease `nums[i]` by 1.
11+
* Increase `nums[i - 1]` by 1.
12+
13+
Return _the **minimum** possible value of the **maximum** integer of_ `nums` _after performing **any** number of operations_.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,7,1,6]
18+
19+
**Output:** 5
20+
21+
**Explanation:** One set of optimal operations is as follows:
22+
1. Choose i = 1, and nums becomes [4,6,1,6].
23+
2. Choose i = 3, and nums becomes [4,6,2,5].
24+
3. Choose i = 1, and nums becomes [5,5,2,5].
25+
26+
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
27+
28+
Therefore, we return 5.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [10,1]
33+
34+
**Output:** 10
35+
36+
**Explanation:** It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.
37+
38+
**Constraints:**
39+
40+
* `n == nums.length`
41+
* <code>2 <= n <= 10<sup>5</sup></code>
42+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package g2401_2500.s2440_create_components_with_same_value;
2+
3+
// #Hard #Array #Math #Tree #Enumeration #Depth_First_Search
4+
// #2022_12_13_Time_114_ms_(73.23%)_Space_115.7_MB_(68.01%)
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Solution {
10+
private int[] nums;
11+
12+
public int componentValue(int[] nums, int[][] edges) {
13+
int n = nums.length;
14+
this.nums = nums;
15+
List[] graph = new ArrayList[n];
16+
for (int i = 0; i < n; i++) {
17+
graph[i] = new ArrayList<>();
18+
}
19+
for (int[] e : edges) {
20+
graph[e[0]].add(e[1]);
21+
graph[e[1]].add(e[0]);
22+
}
23+
int sum = 0;
24+
for (int i : nums) {
25+
sum += i;
26+
}
27+
for (int k = n; k > 0; k--) {
28+
if (sum % k != 0) {
29+
continue;
30+
}
31+
int ans = helper(graph, 0, -1, sum / k);
32+
if (ans == 0) {
33+
return k - 1;
34+
}
35+
}
36+
return 0;
37+
}
38+
39+
private int helper(List<Integer>[] graph, int i, int prev, int target) {
40+
if (graph[i].size() == 1 && graph[i].get(0) == prev) {
41+
if (nums[i] > target) {
42+
return -1;
43+
}
44+
if (nums[i] == target) {
45+
return 0;
46+
}
47+
return nums[i];
48+
}
49+
int sum = nums[i];
50+
for (int k : graph[i]) {
51+
if (k == prev) {
52+
continue;
53+
}
54+
int ans = helper(graph, k, i, target);
55+
if (ans == -1) {
56+
return -1;
57+
}
58+
sum += ans;
59+
}
60+
if (sum > target) {
61+
return -1;
62+
}
63+
if (sum == target) {
64+
return 0;
65+
}
66+
return sum;
67+
}
68+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2440\. Create Components With Same Value
2+
3+
Hard
4+
5+
There is an undirected tree with `n` nodes labeled from `0` to `n - 1`.
6+
7+
You are given a **0-indexed** integer array `nums` of length `n` where `nums[i]` represents the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array `edges` of length `n - 1` where <code>edges[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 tree.
8+
9+
You are allowed to **delete** some edges, splitting the tree into multiple connected components. Let the **value** of a component be the sum of **all** `nums[i]` for which node `i` is in the component.
10+
11+
Return _the **maximum** number of edges you can delete, such that every connected component in the tree has the same value._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2022/08/26/diagramdrawio.png)
16+
17+
**Input:** nums = [6,2,2,2,6], edges = [[0,1],[1,2],[1,3],[3,4]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** The above figure shows how we can delete the edges [0,1] and [3,4]. The created components are nodes [0], [1,2,3] and [4]. The sum of the values in each component equals 6. It can be proven that no better deletion exists, so the answer is 2.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [2], edges = []
26+
27+
**Output:** 0
28+
29+
**Explanation:** There are no edges to be deleted.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
34+
* `nums.length == n`
35+
* `1 <= nums[i] <= 50`
36+
* `edges.length == n - 1`
37+
* `edges[i].length == 2`
38+
* `0 <= edges[i][0], edges[i][1] <= n - 1`
39+
* `edges` represents a valid tree.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2401_2500.s2441_largest_positive_integer_that_exists_with_its_negative;
2+
3+
// #Easy #Array #Hash_Table #2022_12_13_Time_8_ms_(79.93%)_Space_42.7_MB_(84.42%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int findMaxK(int[] nums) {
9+
int[] arr = new int[nums.length];
10+
int j = 0;
11+
for (int i = 0; i < nums.length; i++) {
12+
if (nums[i] < 0) {
13+
arr[j++] = nums[i];
14+
}
15+
}
16+
Arrays.sort(arr);
17+
Arrays.sort(nums);
18+
for (int i = 0; i < nums.length; i++) {
19+
for (int num : nums) {
20+
if ((arr[i] * -1) == num) {
21+
return num;
22+
}
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2441\. Largest Positive Integer That Exists With Its Negative
2+
3+
Easy
4+
5+
Given an integer array `nums` that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array.
6+
7+
Return _the positive integer_ `k`. If there is no such integer, return `-1`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-1,2,-3,3]
12+
13+
**Output:** 3
14+
15+
**Explanation:** 3 is the only valid k we can find in the array.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [-1,10,6,7,-7,1]
20+
21+
**Output:** 7
22+
23+
**Explanation:** Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [-10,8,6,7,-2,-3]
28+
29+
**Output:** -1
30+
31+
**Explanation:** There is no a single valid k, we return -1.
32+
33+
**Constraints:**
34+
35+
* `1 <= nums.length <= 1000`
36+
* `-1000 <= nums[i] <= 1000`
37+
* `nums[i] != 0`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2401_2500.s2442_count_number_of_distinct_integers_after_reverse_operations;
2+
3+
// #Medium #Array #Hash_Table #Math #2022_12_13_Time_73_ms_(96.81%)_Space_61.1_MB_(96.28%)
4+
5+
import java.util.HashSet;
6+
7+
public class Solution {
8+
public int countDistinctIntegers(int[] nums) {
9+
HashSet<Integer> set = new HashSet<>();
10+
for (int i : nums) {
11+
set.add(i);
12+
set.add(reverseInt(i));
13+
}
14+
return set.size();
15+
}
16+
17+
private static int reverseInt(int num) {
18+
int ret = 0;
19+
while (num != 0) {
20+
ret = (num % 10) + ret * 10;
21+
num /= 10;
22+
}
23+
return ret;
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2442\. Count Number of Distinct Integers After Reverse Operations
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of **positive** integers.
6+
7+
You have to take each integer in the array, **reverse its digits**, and add it to the end of the array. You should apply this operation to the original integers in `nums`.
8+
9+
Return _the number of **distinct** integers in the final array_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,13,10,12,31]
14+
15+
**Output:** 6
16+
17+
**Explanation:** After including the reverse of each number, the resulting array is [1,13,10,12,31,<ins>1,31,1,21,13</ins>]. The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1. The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,2,2]
22+
23+
**Output:** 1
24+
25+
**Explanation:** After including the reverse of each number, the resulting array is [2,2,2,<ins>2,2,2</ins>]. The number of distinct integers in this array is 1 (The number 2).
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2401_2500.s2443_sum_of_number_and_its_reverse;
2+
3+
// #Medium #Math #Enumeration #2022_12_13_Time_265_ms_(80.33%)_Space_39.5_MB_(89.70%)
4+
5+
public class Solution {
6+
public boolean sumOfNumberAndReverse(int num) {
7+
for (int i = 0; i <= num; ++i) {
8+
int n = i;
9+
int r = 0;
10+
while (n != 0) {
11+
r = r * 10 + n % 10;
12+
n = n / 10;
13+
}
14+
if (r + i == num) {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2443\. Sum of Number and Its Reverse
2+
3+
Medium
4+
5+
Given a **non-negative** integer `num`, return `true` _if_ `num` _can be expressed as the sum of any **non-negative** integer and its reverse, or_ `false` _otherwise._
6+
7+
**Example 1:**
8+
9+
**Input:** num = 443
10+
11+
**Output:** true
12+
13+
**Explanation:** 172 + 271 = 443 so we return true.
14+
15+
**Example 2:**
16+
17+
**Input:** num = 63
18+
19+
**Output:** false
20+
21+
**Explanation:** 63 cannot be expressed as the sum of a non-negative integer and its reverse so we return false.
22+
23+
**Example 3:**
24+
25+
**Input:** num = 181
26+
27+
**Output:** true
28+
29+
**Explanation:** 140 + 041 = 181 so we return true. Note that when a number is reversed, there may be leading zeros.
30+
31+
**Constraints:**
32+
33+
* <code>0 <= num <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)