forked from builder-of-web3/HackR_Java_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMilly and chocolates
More file actions
81 lines (74 loc) · 3.15 KB
/
Milly and chocolates
File metadata and controls
81 lines (74 loc) · 3.15 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Milly loves chocolates very much. She is at the land of chocolates. This land has N rooms such that there are some chocolates of different brands in every room. It is possible that there can be multiple chocolates of same brand in a particular room. Now she is in a dilemma that whether she can eat at least K distinct branded chocolates at least once. Your task is to print "Yes" or "No" so that she can get her answer of her dilemma.
Input
First line of the input will contain a integer T (number of test cases).
Then for every test case there will be one line containing the values of N (denoting the number of rooms) and K separated by a space.
Now each of the next N lines will first have a value P then P space separated strings denoting the names of the brands.
Output
For every test case, print "Yes" if she can eat all of those K brands at least once else "No".
Constraints
1 ≤ T ≤ 5
1 ≤ N ≤ 102
1 ≤ K ≤ 103
1 ≤ P ≤ 102
1 ≤ Length of the strings ≤ 10
Required Time Complexity: O(T*log(N * P))
SAMPLE INPUT
1
3 2
1 KITKAT
2 FIVESTAR KITKAT
1 KITKAT
SAMPLE OUTPUT
Yes
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class test {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
long noOfRooms,distinctChocolates,noOfChocolates,max,size;
TreeMap<Long,HashSet<String>> map;
HashSet<String> set;
StringTokenizer tokenizer;
Map.Entry<Long, HashSet<String>> mapEntry = null;
for (int i = 0; i < N; i++) {
map = new TreeMap<>();
tokenizer = new StringTokenizer(br.readLine()," ");
noOfRooms = Long.parseLong(tokenizer.nextToken());
distinctChocolates = Long.parseLong(tokenizer.nextToken());
for(long j=0;j<noOfRooms;j++)
{
tokenizer = new StringTokenizer(br.readLine()," ");
noOfChocolates = Long.parseLong(tokenizer.nextToken());
set = new HashSet<>();
for(long k=0;k<noOfChocolates;k++)
set.add(tokenizer.nextToken());
map.put(j,set);
}
for(long j=0;j<noOfRooms;j++) {
max = -1;
for (Map.Entry<Long, HashSet<String>> entry : map.entrySet()) {
size = entry.getValue().size();
if(max<size) {
max = size;
mapEntry = entry;
}
}
distinctChocolates -= mapEntry.getValue().size();
if(distinctChocolates<=0)
{
System.out.println("YES");
return;
}
map.remove(mapEntry.getKey());
for(Map.Entry<Long, HashSet<String>> entry : map.entrySet())
entry.getValue().removeAll(mapEntry.getValue());
}
System.out.println("NO");
}
}
}