-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFoodPacketsDistribution.java
More file actions
74 lines (45 loc) · 1.42 KB
/
Copy pathFoodPacketsDistribution.java
File metadata and controls
74 lines (45 loc) · 1.42 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
/**
*
Problem Description
The government wants to set up B distribution offices across N cities for the distribution of food packets.
The population of the ith city is A[i]. Each city must have at least 1 office and every person is assigned to exactly one office in their own city.
Let M denote the minimum number of people that are assigned to any of the offices. Find the maximum value of M possible.
Problem Constraints
1 <= N <= 105
1 <= A[i] <= 106
1 <= B <= 5 x 105
Input Format
The first line of input contains an integer array A.
The second line of input contains an integer B.
Output Format
Return one integer representing the maximum number of people who can get food in any single office.
Example Input
Input 1:
A = [10000, 22000, 36000]
B = 6
Input 2:
A = [1, 1, 1]
B = 4
Example Output
Output 1:
10000
Output 2:
0
*
*/
public class Solution {
public ArrayList<Integer> solve(ArrayList<Integer> A, ArrayList<Integer> B) {
ArrayList<Integer> ans = new ArrayList<Integer>();
Map<Integer, Integer> storer = new HashMap<Integer, Integer>();
for(int a: A){
storer.put(a, storer.getOrDefault(a, 0) + 1);
}
for(int b : B){
if(storer.containsKey(b) && storer.get(b) > 0){
ans.add(b);
storer.put(b, storer.get(b) - 1);
}
}
return ans;
}
}