-
Notifications
You must be signed in to change notification settings - Fork 0
/
HusbandInShop.java
72 lines (57 loc) · 1.89 KB
/
HusbandInShop.java
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
import java.util.*;
import java.io.*;
public class HusbandInShop
{
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int productsAmount = in.nextInt();
Map<String, Integer> supply = new HashMap<>();
Deque<Remaining> deque = new LinkedList<>();
while(productsAmount-- > 0) {
int amount = in.nextInt();
in.next(); //"of"
String product = in.next();
supply.put(product, amount);
}
int customers = in.nextInt();
while (customers-- > 0) {
int amount = in.nextInt();
in.next(); //"of"
String product = in.next();
Remaining remaining = new Remaining(amount, product);
deque.addLast(remaining);
}
int wait = 0;
while(deque.size() > 0) {
Remaining nextCustomer = deque.removeFirst();
Integer available = supply.get(nextCustomer.name);
if (available == null || available == 0) {
//go home
}
else if (available >= nextCustomer.amount) {
available -= nextCustomer.amount;
supply.put(nextCustomer.name, available);
}
else {
if (deque.size() > 0) {
Remaining secondCistomer = deque.removeFirst();
nextCustomer.amount = available;
deque.addFirst(nextCustomer);
deque.addFirst(secondCistomer);
}
}
wait++;
}
System.out.println(wait);
}
public static class Remaining
{
int amount;
String name;
public Remaining(int amount, String name) {
this.amount = amount;
this.name = name;
}
}
}