Skip to content

Commit 6083f5e

Browse files
authored
Added tasks 2341, 2342.
1 parent ef77ecc commit 6083f5e

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-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.11'
18481848

18491849
| # | Title | Difficulty | Tag | Time, ms | Time, %
18501850
|------|----------------|-------------|-------------|----------|---------
1851+
| 2342 |[Max Sum of a Pair With Equal Sum of Digits](src/main/java/g2301_2400/s2342_max_sum_of_a_pair_with_equal_sum_of_digits/Solution.java)| Medium || 99 | 100.00
1852+
| 2341 |[Maximum Number of Pairs in Array](src/main/java/g2301_2400/s2341_maximum_number_of_pairs_in_array/Solution.java)| Easy || 2 | 80.00
18511853
| 2338 |[Count the Number of Ideal Arrays](src/main/java/g2301_2400/s2338_count_the_number_of_ideal_arrays/Solution.java)| Hard | Math, Dynamic_Programming, Combinatorics, Number_Theory | 21 | 99.04
18521854
| 2337 |[Move Pieces to Obtain a String](src/main/java/g2301_2400/s2337_move_pieces_to_obtain_a_string/Solution.java)| Medium | Two_Pointers, String | 23 | 82.39
18531855
| 2336 |[Smallest Number in Infinite Set](src/main/java/g2301_2400/s2336_smallest_number_in_infinite_set/SmallestInfiniteSet.java)| Medium | Hash_Table, Design, Heap_Priority_Queue | 12 | 96.69
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2301_2400.s2341_maximum_number_of_pairs_in_array;
2+
3+
// #Easy #2022_07_18_Time_2_ms_(80.00%)_Space_42.3_MB_(80.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] numberOfPairs(int[] nums) {
9+
Arrays.sort(nums);
10+
int pairs = 0;
11+
for (int i = 0; i < nums.length; i++) {
12+
if (i > 0 && nums[i] == nums[i - 1]) {
13+
nums[i] = -1;
14+
nums[i - 1] = -1;
15+
pairs++;
16+
}
17+
}
18+
return new int[] {pairs, nums.length - (2 * pairs)};
19+
}
20+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2341\. Maximum Number of Pairs in Array
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums`. In one operation, you may do the following:
6+
7+
* Choose **two** integers in `nums` that are **equal**.
8+
* Remove both integers from `nums`, forming a **pair**.
9+
10+
The operation is done on `nums` as many times as possible.
11+
12+
Return _a **0-indexed** integer array_ `answer` _of size_ `2` _where_ `answer[0]` _is the number of pairs that are formed and_ `answer[1]` _is the number of leftover integers in_ `nums` _after doing the operation as many times as possible_.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,2,1,3,2,2]
17+
18+
**Output:** [3,1]
19+
20+
**Explanation:**
21+
22+
Form a pair with nums[0] and nums[3] and remove them from nums. Now, nums = [3,2,3,2,2].
23+
24+
Form a pair with nums[0] and nums[2] and remove them from nums. Now, nums = [2,2,2].
25+
26+
Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [2].
27+
28+
No more pairs can be formed. A total of 3 pairs have been formed, and there is 1 number leftover in nums.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [1,1]
33+
34+
**Output:** [1,0]
35+
36+
**Explanation:** Form a pair with nums[0] and nums[1] and remove them from nums. Now, nums = [].
37+
38+
No more pairs can be formed. A total of 1 pair has been formed, and there are 0 numbers leftover in nums.
39+
40+
**Example 3:**
41+
42+
**Input:** nums = [0]
43+
44+
**Output:** [0,1]
45+
46+
**Explanation:** No pairs can be formed, and there is 1 number leftover in nums.
47+
48+
**Constraints:**
49+
50+
* `1 <= nums.length <= 100`
51+
* `0 <= nums[i] <= 100`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g2301_2400.s2342_max_sum_of_a_pair_with_equal_sum_of_digits;
2+
3+
// #Medium #2022_07_18_Time_99_ms_(100.00%)_Space_121.2_MB_(100.00%)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class Solution {
9+
public int maximumSum(int[] nums) {
10+
Map<Integer, Integer> map = new HashMap<>();
11+
int res = -1;
12+
for (int num : nums) {
13+
int s = 0;
14+
for (char digit : String.valueOf(num).toCharArray()) {
15+
s += Integer.valueOf(digit - '0');
16+
}
17+
if (!map.containsKey(s)) {
18+
map.put(s, num);
19+
} else {
20+
res = Math.max(res, map.get(s) + num);
21+
map.put(s, Math.max(map.get(s), num));
22+
}
23+
}
24+
return res;
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2342\. Max Sum of a Pair With Equal Sum of Digits
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` consisting of **positive** integers. You can choose two indices `i` and `j`, such that `i != j`, and the sum of digits of the number `nums[i]` is equal to that of `nums[j]`.
6+
7+
Return _the **maximum** value of_ `nums[i] + nums[j]` _that you can obtain over all possible indices_ `i` _and_ `j` _that satisfy the conditions._
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [18,43,36,13,7]
12+
13+
**Output:** 54
14+
15+
**Explanation:** The pairs (i, j) that satisfy the conditions are:
16+
17+
- (0, 2), both numbers have a sum of digits equal to 9, and their sum is 18 + 36 = 54.
18+
19+
- (1, 4), both numbers have a sum of digits equal to 7, and their sum is 43 + 7 = 50.
20+
21+
So the maximum sum that we can obtain is 54.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [10,12,19,14]
26+
27+
**Output:** -1
28+
29+
**Explanation:** There are no two numbers that satisfy the conditions, so we return -1.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
34+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g2301_2400.s2341_maximum_number_of_pairs_in_array;
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 numberOfPairs() {
11+
assertThat(
12+
new Solution().numberOfPairs(new int[] {1, 3, 2, 1, 3, 2, 2}),
13+
equalTo(new int[] {3, 1}));
14+
}
15+
16+
@Test
17+
void numberOfPairs2() {
18+
assertThat(new Solution().numberOfPairs(new int[] {1, 1}), equalTo(new int[] {1, 0}));
19+
}
20+
21+
@Test
22+
void numberOfPairs3() {
23+
assertThat(new Solution().numberOfPairs(new int[] {0}), equalTo(new int[] {0, 1}));
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2342_max_sum_of_a_pair_with_equal_sum_of_digits;
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 maximumSum() {
11+
assertThat(new Solution().maximumSum(new int[] {18, 43, 36, 13, 7}), equalTo(54));
12+
}
13+
14+
@Test
15+
void maximumSum2() {
16+
assertThat(new Solution().maximumSum(new int[] {10, 12, 19, 14}), equalTo(-1));
17+
}
18+
}

0 commit comments

Comments
 (0)