forked from natural/java2python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.java
More file actions
58 lines (47 loc) · 1.71 KB
/
Code.java
File metadata and controls
58 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
public class Result {
public static int deleteProducts(List<Integer> ids, int m){
int count=0;
HashSet<Integer> keys = new HashSet<Integer>(ids);
HashMap<Integer,Integer> valueFrequency = new HashMap<Integer,Integer>();
Iterator<Integer> value = keys.iterator();
while (value.hasNext()) {
count=0;
int key = value.next();
for(int id:ids){
if(id==key){
count++;
}
}
valueFrequency.put(key,count);
}
while(m!=0){
Collection<Integer> frequencies = valueFrequency.values();
int minFrquency = Collections.min(frequencies);
if (m==minFrquency){
return keys.size()-1;
}else if (m<minFrquency){
return keys.size();
}else{
int keyToDelete=0;
Iterator<Map.Entry<Integer,Integer>> it = valueFrequency.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer,Integer> pair = (Map.Entry<Integer,Integer>) it.next();
if(pair.getValue()==minFrquency){
keyToDelete=pair.getKey();
break;
}
}
valueFrequency.remove(keyToDelete);
keys.remove(keyToDelete);
m=m-minFrquency;
}
}
return 0;
}
public static void main(String[] args) {
List<Integer> ids = new ArrayList<>() ;
ids.add(1);ids.add(2);ids.add(3);ids.add(1);ids.add(2);ids.add(2);
System.out.println(deleteProducts(ids,3));
}
}