-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFIFO.java
More file actions
60 lines (51 loc) · 1.69 KB
/
MyFIFO.java
File metadata and controls
60 lines (51 loc) · 1.69 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
package Stack;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/*
Create your own FIFO queue. WIll be given a list of commands and
a max queue size. You will return the an array of the results for command The three commands:
1. "SIZE" calcualte size of queue.
2. "OFFER XXXXX": add XXXXX to queue (if its not full).
Should add if true or false if it can be added to queue.
3. TAKE: POP the first value off the queue and put it in result queue (if there is a value, if not put false).
*/
// Time Complexity: O(1)
// Space: O(N)
public class MyFIFO {
public static String[] FIFO(String[] cmds, int maxQueue){
List<String> results = new ArrayList<>();
Queue<Integer> q = new LinkedList<>();
int i =0;
while(i<cmds.length){
String cmd = cmds[i];
if(cmd.contains("SIZE")){
results.add(String.valueOf(q.size()));
}
else if(cmd.contains("OFFER")){
String value = cmd.split(" ")[1];
if(q.size()<maxQueue){
results.add("true");
q.add(Integer.valueOf(value));
}
else {
results.add("false");
}
}
else if(cmd.contains("POP")){
if(q.size()<=0){
results.add("false");
}
else {
results.add(String.valueOf(q.remove()));
}
}
else {
results.add("fail");
}
i++;
}
return results.toArray(String[]::new);
}
}