Skip to content

Commit e6cf274

Browse files
authored
Added tasks 2354, 2357.
1 parent 3289505 commit e6cf274

File tree

7 files changed

+169
-0
lines changed

7 files changed

+169
-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+
| 2357 |[Make Array Zero by Subtracting Equal Amounts](src/main/java/g2301_2400/s2357_make_array_zero_by_subtracting_equal_amounts/Solution.java)| Easy | Array, Hash_Table, Sorting, Heap_Priority_Queue, Simulation | 1 | 98.24
1852+
| 2354 |[Number of Excellent Pairs](src/main/java/g2301_2400/s2354_number_of_excellent_pairs/Solution.java)| Hard | Array, Hash_Table, Binary_Search, Bit_Manipulation | 80 | 86.77
18511853
| 2353 |[Design a Food Rating System](src/main/java/g2301_2400/s2353_design_a_food_rating_system/FoodRatings.java)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 364 | 83.35
18521854
| 2352 |[Equal Row and Column Pairs](src/main/java/g2301_2400/s2352_equal_row_and_column_pairs/Solution.java)| Medium | Array, Hash_Table, Matrix, Simulation | 7 | 98.94
18531855
| 2351 |[First Letter to Appear Twice](src/main/java/g2301_2400/s2351_first_letter_to_appear_twice/Solution.java)| Easy | Hash_Table, String, Counting | 1 | 63.38
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2301_2400.s2354_number_of_excellent_pairs;
2+
3+
// #Hard #Array #Hash_Table #Binary_Search #Bit_Manipulation
4+
// #2022_08_07_Time_80_ms_(86.77%)_Space_109.2_MB_(29.50%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public long countExcellentPairs(int[] nums, int k) {
11+
long[] cnt = new long[30];
12+
long res = 0L;
13+
Set<Integer> set = new HashSet<>();
14+
for (int a : nums) {
15+
set.add(a);
16+
}
17+
for (int a : set) {
18+
cnt[Integer.bitCount(a)]++;
19+
}
20+
for (int i = 1; i < 30; ++i) {
21+
for (int j = 1; j < 30; ++j) {
22+
if (i + j >= k) {
23+
res += cnt[i] * cnt[j];
24+
}
25+
}
26+
}
27+
return res;
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2354\. Number of Excellent Pairs
2+
3+
Hard
4+
5+
You are given a **0-indexed** positive integer array `nums` and a positive integer `k`.
6+
7+
A pair of numbers `(num1, num2)` is called **excellent** if the following conditions are satisfied:
8+
9+
* **Both** the numbers `num1` and `num2` exist in the array `nums`.
10+
* The sum of the number of set bits in `num1 OR num2` and `num1 AND num2` is greater than or equal to `k`, where `OR` is the bitwise **OR** operation and `AND` is the bitwise **AND** operation.
11+
12+
Return _the number of **distinct** excellent pairs_.
13+
14+
Two pairs `(a, b)` and `(c, d)` are considered distinct if either `a != c` or `b != d`. For example, `(1, 2)` and `(2, 1)` are distinct.
15+
16+
**Note** that a pair `(num1, num2)` such that `num1 == num2` can also be excellent if you have at least **one** occurrence of `num1` in the array.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [1,2,3,1], k = 3
21+
22+
**Output:** 5
23+
24+
**Explanation:** The excellent pairs are the following:
25+
26+
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
27+
28+
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
29+
30+
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
31+
32+
So the number of excellent pairs is 5.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [5,1,1], k = 10
37+
38+
**Output:** 0
39+
40+
**Explanation:** There are no excellent pairs for this array.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
45+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
46+
* `1 <= k <= 60`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2301_2400.s2357_make_array_zero_by_subtracting_equal_amounts;
2+
3+
// #Easy #Array #Hash_Table #Sorting #Heap_Priority_Queue #Simulation
4+
// #2022_08_07_Time_1_ms_(98.24%)_Space_41.9_MB_(50.08%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public int minimumOperations(int[] nums) {
11+
Set<Integer> set = new HashSet<>();
12+
for (int a : nums) {
13+
if (a > 0) {
14+
set.add(a);
15+
}
16+
}
17+
return set.size();
18+
}
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2357\. Make Array Zero by Subtracting Equal Amounts
2+
3+
Easy
4+
5+
You are given a non-negative integer array `nums`. In one operation, you must:
6+
7+
* Choose a positive integer `x` such that `x` is less than or equal to the **smallest non-zero** element in `nums`.
8+
* Subtract `x` from every **positive** element in `nums`.
9+
10+
Return _the **minimum** number of operations to make every element in_ `nums` _equal to_ `0`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,5,0,3,5]
15+
16+
**Output:** 3
17+
18+
**Explanation:**
19+
20+
In the first operation, choose x = 1. Now, nums = [0,4,0,2,4].
21+
22+
In the second operation, choose x = 2. Now, nums = [0,2,0,0,2].
23+
24+
In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [0]
29+
30+
**Output:** 0
31+
32+
**Explanation:** Each element in nums is already 0 so no operations are needed.
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 100`
37+
* `0 <= nums[i] <= 100`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2354_number_of_excellent_pairs;
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 countExcellentPairs() {
11+
assertThat(new Solution().countExcellentPairs(new int[] {1, 2, 3, 1}, 3), equalTo(5L));
12+
}
13+
14+
@Test
15+
void countExcellentPairs2() {
16+
assertThat(new Solution().countExcellentPairs(new int[] {5, 1, 1}, 10), equalTo(0L));
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2301_2400.s2357_make_array_zero_by_subtracting_equal_amounts;
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 minimumOperations() {
11+
assertThat(new Solution().minimumOperations(new int[] {1, 5, 0, 3, 5}), equalTo(3));
12+
}
13+
14+
@Test
15+
void minimumOperations2() {
16+
assertThat(new Solution().minimumOperations(new int[] {0}), equalTo(0));
17+
}
18+
}

0 commit comments

Comments
 (0)