Skip to content

Commit dc8ff3a

Browse files
authored
Added tasks 2235, 2236, 2239, 2248.
1 parent 413987f commit dc8ff3a

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,8 +1332,12 @@
13321332

13331333
| # | Title | Difficulty | Tag | Time, ms | Time, %
13341334
|------|----------------|-------------|-------------|----------|--------
1335+
| 2248 |[Intersection of Multiple Arrays](src/main/java/g2201_2300/s2248_intersection_of_multiple_arrays)| Easy || 2 | 99.46
13351336
| 2246 |[Longest Path With Different Adjacent Characters](src/main/java/g2201_2300/s2246_longest_path_with_different_adjacent_characters)| Hard | Array, String, Depth_First_Search, Tree, Graph, Topological_Sort | 75 | 97.79
13361337
| 2241 |[Design an ATM Machine](src/main/java/g2201_2300/s2241_design_an_atm_machine)| Medium | Array, Greedy, Design | 192 | 24.16
1338+
| 2239 |[Find Closest Number to Zero](src/main/java/g2201_2300/s2239_find_closest_number_to_zero)| Easy | Array | 2 | 84.21
1339+
| 2236 |[Root Equals Sum of Children](src/main/java/g2201_2300/s2236_root_equals_sum_of_children)| Easy | Tree, Binary_Tree | 0 | 100.00
1340+
| 2235 |[Add Two Integers](src/main/java/g2201_2300/s2235_add_two_integers)| Easy | Math | 1 | 48.94
13371341
| 2234 |[Maximum Total Beauty of the Gardens](src/main/java/g2201_2300/s2234_maximum_total_beauty_of_the_gardens)| Hard | Array, Sorting, Greedy, Binary_Search, Two_Pointers | 63 | 73.03
13381342
| 2226 |[Maximum Candies Allocated to K Children](src/main/java/g2201_2300/s2226_maximum_candies_allocated_to_k_children)| Medium | Array, Binary_Search | 46 | 78.19
13391343
| 2221 |[Find Triangular Sum of an Array](src/main/java/g2201_2300/s2221_find_triangular_sum_of_an_array)| Medium | Array, Math, Simulation, Combinatorics | 78 | 83.64
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## 2235\. Add Two Integers
2+
3+
Easy
4+
5+
Given two integers `num1` and `num2`, return _the **sum** of the two integers_.
6+
7+
**Example 1:**
8+
9+
**Input:** num1 = 12, num2 = 5
10+
11+
**Output:** 17
12+
13+
**Explanation:** num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
14+
15+
**Example 2:**
16+
17+
**Input:** num1 = -10, num2 = 4
18+
19+
**Output:** -6
20+
21+
**Explanation:** num1 + num2 = -6, so -6 is returned.
22+
23+
**Constraints:**
24+
25+
* `-100 <= num1, num2 <= 100`
26+
27+
## Solution
28+
29+
```java
30+
public class Solution {
31+
public int sum(int num1, int num2) {
32+
return num1 + num2;
33+
}
34+
}
35+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## 2236\. Root Equals Sum of Children
2+
3+
Easy
4+
5+
You are given the `root` of a **binary tree** that consists of exactly `3` nodes: the root, its left child, and its right child.
6+
7+
Return `true` _if the value of the root is equal to the **sum** of the values of its two children, or_ `false` _otherwise_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2022/04/08/graph3drawio.png)
12+
13+
**Input:** root = [10,4,6]
14+
15+
**Output:** true
16+
17+
**Explanation:** The values of the root, its left child, and its right child are 10, 4, and 6, respectively.
18+
19+
10 is equal to 4 + 6, so we return true.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2022/04/08/graph3drawio-1.png)
24+
25+
**Input:** root = [5,3,1]
26+
27+
**Output:** false
28+
29+
**Explanation:** The values of the root, its left child, and its right child are 5, 3, and 1, respectively.
30+
31+
5 is not equal to 3 + 1, so we return false.
32+
33+
**Constraints:**
34+
35+
* The tree consists only of the root, its left child, and its right child.
36+
* `-100 <= Node.val <= 100`
37+
38+
## Solution
39+
40+
```java
41+
import com_github_leetcode.TreeNode;
42+
43+
public class Solution {
44+
public boolean checkTree(TreeNode root) {
45+
return root.left.val + root.right.val == root.val;
46+
}
47+
}
48+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 2239\. Find Closest Number to Zero
2+
3+
Easy
4+
5+
Given an integer array `nums` of size `n`, return _the number with the value **closest** to_ `0` _in_ `nums`. If there are multiple answers, return _the number with the **largest** value_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [-4,-2,1,4,8]
10+
11+
**Output:** 1
12+
13+
**Explanation:**
14+
15+
The distance from -4 to 0 is |-4| = 4.
16+
17+
The distance from -2 to 0 is |-2| = 2.
18+
19+
The distance from 1 to 0 is |1| = 1. The distance from 4 to 0 is |4| = 4.
20+
21+
The distance from 8 to 0 is |8| = 8.
22+
23+
Thus, the closest number to 0 in the array is 1.
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [2,-1,1]
28+
29+
**Output:** 1
30+
31+
**Explanation:** 1 and -1 are both the closest numbers to 0, so 1 being larger is returned.
32+
33+
**Constraints:**
34+
35+
* `1 <= n <= 1000`
36+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
37+
38+
## Solution
39+
40+
```java
41+
public class Solution {
42+
public int findClosestNumber(int[] nums) {
43+
int mini = Integer.MAX_VALUE;
44+
int closestNum = 0;
45+
for (int n : nums) {
46+
if (mini > Math.abs(n)) {
47+
mini = Math.abs(n);
48+
closestNum = n;
49+
} else if (mini == Math.abs(n) && closestNum < n) {
50+
closestNum = n;
51+
}
52+
}
53+
return closestNum;
54+
}
55+
}
56+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## 2248\. Intersection of Multiple Arrays
2+
3+
Easy
4+
5+
Given a 2D integer array `nums` where `nums[i]` is a non-empty array of **distinct** positive integers, return _the list of integers that are present in **each array** of_ `nums` _sorted in **ascending order**_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = \[\[**3**,1,2,**4**,5],[1,2,**3**,**4**],[**3**,**4**,5,6]]
10+
11+
**Output:** [3,4]
12+
13+
**Explanation:**
14+
15+
The only integers present in each of nums[0] = [**3**,1,2,**4**,5], nums[1] = [1,2,**3**,**4**], and nums[2] = [**3**,**4**,5,6] are 3 and 4, so we return [3,4].
16+
17+
**Example 2:**
18+
19+
**Input:** nums = \[\[1,2,3],[4,5,6]]
20+
21+
**Output:** []
22+
23+
**Explanation:**
24+
25+
There does not exist any integer present both in nums[0] and nums[1], so we return an empty list [].
26+
27+
**Constraints:**
28+
29+
* `1 <= nums.length <= 1000`
30+
* `1 <= sum(nums[i].length) <= 1000`
31+
* `1 <= nums[i][j] <= 1000`
32+
* All the values of `nums[i]` are **unique**.
33+
34+
## Solution
35+
36+
```java
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
40+
public class Solution {
41+
public List<Integer> intersection(int[][] nums) {
42+
List<Integer> ans = new ArrayList<>();
43+
int[] count = new int[1001];
44+
for (int[] arr : nums) {
45+
for (int i : arr) {
46+
++count[i];
47+
}
48+
}
49+
for (int i = 0; i < count.length; i++) {
50+
if (count[i] == nums.length) {
51+
ans.add(i);
52+
}
53+
}
54+
return ans;
55+
}
56+
}
57+
```

0 commit comments

Comments
 (0)