Skip to content

Commit 1ad58ef

Browse files
add 1342
1 parent 857c359 commit 1ad58ef

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ _If you like this project, please leave me a star._ ★
99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1343|[Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium||
12+
|1342|[Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Medium||
1213
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
1314
|1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1333.java) | |Medium||
1415
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
/**
6+
* 1342. Reduce Array Size to The Half
7+
*
8+
* Given an array arr. You can choose a set of integers and remove all the occurrences of these integers in the array.
9+
* Return the minimum size of the set so that at least half of the integers of the array are removed.
10+
*
11+
* Example 1:
12+
* Input: arr = [3,3,3,3,5,5,5,2,2,7]
13+
* Output: 2
14+
* Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
15+
* Possible sets of size 2 are {3,5},{3,2},{5,2}.
16+
* Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.
17+
*
18+
* Example 2:
19+
* Input: arr = [7,7,7,7,7,7]
20+
* Output: 1
21+
* Explanation: The only possible set you can choose is {7}. This will make the new array empty.
22+
*
23+
* Example 3:
24+
* Input: arr = [1,9]
25+
* Output: 1
26+
*
27+
* Example 4:
28+
* Input: arr = [1000,1000,3,7]
29+
* Output: 1
30+
*
31+
* Example 5:
32+
* Input: arr = [1,2,3,4,5,6,7,8,9,10]
33+
* Output: 5
34+
*
35+
* Constraints:
36+
* 1 <= arr.length <= 10^5
37+
* arr.length is even.
38+
* 1 <= arr[i] <= 10^5
39+
* */
40+
public class _1342 {
41+
public static class Solution1 {
42+
public int minSetSize(int[] arr) {
43+
Map<Integer, Integer> map = new HashMap<>();
44+
for (int i : arr) {
45+
map.put(i, map.getOrDefault(i, 0) + 1);
46+
}
47+
List<Integer> list = new ArrayList<>();
48+
for (int k : map.keySet()) {
49+
list.add(map.get(k));
50+
}
51+
Collections.sort(list, Collections.reverseOrder());
52+
int sum = 0;
53+
int i = 0;
54+
while (sum < arr.length / 2) {
55+
sum += list.get(i++);
56+
}
57+
return i--;
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1342;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1342Test {
10+
private static _1342.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1342.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{3, 3, 3, 3, 5, 5, 5, 2, 2, 7};
21+
assertEquals(2, solution1.minSetSize(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{7, 7, 7, 7, 7, 7};
27+
assertEquals(1, solution1.minSetSize(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{1, 9};
33+
assertEquals(1, solution1.minSetSize(arr));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
arr = new int[]{1000, 1000, 3, 7};
39+
assertEquals(1, solution1.minSetSize(arr));
40+
}
41+
42+
@Test
43+
public void test5() {
44+
arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
45+
assertEquals(5, solution1.minSetSize(arr));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)