-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay49.java
More file actions
106 lines (94 loc) · 2.66 KB
/
Copy pathDay49.java
File metadata and controls
106 lines (94 loc) · 2.66 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*Two Stacks in an Array
You are given an array of a fixed size. Your task is to efficiently implement two stacks in this single array.
The following operations must be supported:
(i) twoStacks : Initialize the data structures and variables to be used to implement 2 stacks in one array.
(ii) push1(x) : pushes element into the first stack.
(iii) push2(x) : pushes element into the second stack.
(iv) pop1() : pops an element from the first stack and returns the popped element. If the first stack is empty, it should return -1.
(v) pop2() : pops an element from the second stack and returns the popped element. If the second stack is empty, it should return -1.
Examples:
Input:
push1(2)
push1(3)
push2(4)
pop1()
pop2()
pop2()
Output: [3, 4, -1]
Explanation:
push1(2): the stack1 will be [2]
push1(3): the stack1 will be [2,3]
push2(4): the stack2 will be [4]
pop1(): the poped element will be 3 from stack1 and stack1 will be {2}
pop2(): the poped element will be 4 from stack2 and now stack2 is empty
pop2(): the stack2 is now empty hence returned -1.
Input:
push1(1)
push2(2)
pop1()
push1(3)
pop1()
pop1()
Output: [1, 3, -1]
Explanation:
push1(1): the stack1 will be [1]
push2(2): the stack2 will be [2]
pop1(): the poped element will be 1 from stack1 and stack1 will be empty
push1(3): the stack1 will be [3]
pop1(): the poped element will be 3 from stack1 and stack1 will be empty
pop1(): the stack1 is now empty hence returned -1.
Input:
push1(2)
push1(3)
push1(4)
pop2()
pop2()
pop2()
Output: [-1, -1, -1]
Explanation:
push1(2): the stack1 will be [2]
push1(3): the stack1 will be [2,3]
push1(4): the stack1 will be [2,3,4]
pop2(): the stack2 is empty hence returned -1.
pop2(): the stack2 is empty hence returned -1.
pop2(): the stack2 is empty hence returned -1. */
/*class twoStacks {
int[] arr;
int top1, top2;
int size = 100; // fixed size (GFG usually assumes size)
twoStacks() {
arr = new int[size];
top1 = -1;
top2 = size;
}
// Function to push an integer into the stack1.
void push1(int x) {
if (top1 + 1 < top2) {
arr[++top1] = x;
}
}
// Function to push an integer into the stack2.
void push2(int x) {
if (top1 + 1 < top2) {
arr[--top2] = x;
}
}
// Function to remove an element from top of the stack1.
int pop1() {
if (top1 >= 0) {
return arr[top1--];
}
return -1;
}
// Function to remove an element from top of the stack2.
int pop2() {
if (top2 < size) {
return arr[top2++];
}
return -1;
}
}
*/
/*⏱ Complexity
Push / Pop: O(1)
Space: O(n) (single array, optimal) */