Skip to content

Commit f020a38

Browse files
committed
Stacks: Assignment Completed!
1 parent eef308e commit f020a38

File tree

4 files changed

+208
-11
lines changed

4 files changed

+208
-11
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package stacks.Assignment;
2+
3+
import java.util.Stack;
4+
5+
/*Given a single string mathematical expression, return true if
6+
redundant brackets are present in the expression. Brackets are redundant if
7+
there is nothing inside the bracket or more than one pair of brackets are present.
8+
Assume the given string expression is balanced and contains only one type of
9+
bracket i.e. ().
10+
Sample Input:
11+
((a+b))
12+
(a+b)
13+
Sample Output:
14+
true
15+
false*/
16+
public class CheckRedundantBrackets {
17+
18+
private static boolean find(char ch) {
19+
return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
20+
}
21+
22+
public static boolean checkRedundant(String expression) {
23+
var stack = new Stack<Character>();
24+
for (int i = 0; i < expression.length(); i++) {
25+
if (expression.charAt(i) == '(' || find(expression.charAt(i))) {
26+
stack.push(expression.charAt(i));
27+
} else if (expression.charAt(i) == ')') {
28+
boolean hasOperator = false;
29+
30+
while (!stack.isEmpty() && stack.peek() != '(') {
31+
stack.pop();
32+
hasOperator = true;
33+
}
34+
35+
if (!hasOperator) {
36+
return true;
37+
}
38+
39+
if (!stack.isEmpty()) {
40+
stack.pop();
41+
}
42+
}
43+
}
44+
return false;
45+
}
46+
47+
public static void main(String[] args) {
48+
System.out.println(checkRedundant("((a+b))"));
49+
}
50+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package stacks.Assignment;
2+
3+
import java.util.Stack;
4+
5+
/*For a given expression in the form of a string, find the minimum number of brackets that can be reversed in order to make the expression balanced. The expression will only contain curly brackets.
6+
If the expression can't be balanced, return -1.
7+
Example:
8+
Expression: {{{{
9+
If we reverse the second and the fourth opening brackets, the whole expression will get balanced. Since we have to reverse two brackets to make the expression balanced, the expected output will be 2.
10+
11+
Expression: {{{
12+
In this example, even if we reverse the last opening bracket, we would be left with the first opening bracket and hence will not be able to make the expression balanced and the output will be -1.
13+
Input Format :
14+
The first and the only line of input contains a string expression, without any spaces in between.
15+
Output Format :
16+
The only line of output will print the number of reversals required to balance the whole expression. Prints -1, otherwise.
17+
Note:
18+
You don't have to print anything. It has already been taken care of.
19+
Constraints:
20+
0 <= N <= 10^6
21+
Where N is the length of the expression.
22+
23+
Time Limit: 1sec
24+
Sample Input 1:
25+
{{{
26+
Sample Output 1:
27+
-1
28+
Sample Input 2:
29+
{{{{}}
30+
Sample Output 2:
31+
1*/
32+
public class MinimumBracketsReversal {
33+
34+
public static int countBracketsReversals(String expression) {
35+
if (expression.length() == 0) {
36+
return 0;
37+
}
38+
39+
if (expression.length() % 2 != 0) {
40+
return -1; // Only even numbers of brackets can be balanced
41+
}
42+
43+
var stack = new Stack<Character>();
44+
for (int i = 0; i < expression.length(); i++) {
45+
char currentCharacter = expression.charAt(i);
46+
47+
if (currentCharacter == '(') {
48+
stack.push(currentCharacter);
49+
} else {
50+
// pop if there is a balanced pair
51+
if (!stack.isEmpty() && stack.peek() == '{') {
52+
stack.pop();
53+
} else {
54+
stack.push(currentCharacter);
55+
}
56+
}
57+
}
58+
59+
int count = 0;
60+
61+
// only unbalanced brackets are there in stack now
62+
while (!stack.isEmpty()) {
63+
char char1 = stack.pop();
64+
char char2 = stack.pop();
65+
/*when char1 = } and char2 = {, then we need to reverse both of them so count will increase by 2*/
66+
if (char1 != char2) {
67+
count += 2;
68+
} else {
69+
count++;
70+
}
71+
}
72+
return count;
73+
}
74+
75+
public static void main(String[] args) {
76+
System.out.println(countBracketsReversals("{{{{}}"));
77+
}
78+
}

stacks/Assignment/StockSpan.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package stacks.Assignment;
2+
3+
import java.util.Arrays;
4+
import java.util.Stack;
5+
6+
/*Afzal has been working with an organization called 'Money Traders' for the past few years. The organization is into the money trading business. His manager assigned him a task. For a given array/list of stock's prices for N days, find the stock's span for each day.
7+
The span of the stock's price today is defined as the maximum number of consecutive days(starting from today and going backwards) for which the price of the stock was less than today's price.
8+
For example, if the price of a stock over a period of 7 days are [100, 80, 60, 70, 60, 75, 85], then the stock spans will be [1, 1, 1, 2, 1, 4, 6].
9+
Explanation:
10+
On the sixth day when the price of the stock was 75, the span came out to be 4, because the last 4 prices(including the current price of 75) were less than the current or the sixth day's price.
11+
12+
Similarly, we can deduce the remaining results.
13+
Afzal has to return an array/list of spans corresponding to each day's stock's price. Help him to achieve the task.
14+
Input Format:
15+
The first line of input contains an integer N, denoting the total number of days.
16+
17+
The second line of input contains the stock prices of each day. A single space will separate them.
18+
Output Format:
19+
The only line of output will print the span for each day's stock price. A single space will separate them.
20+
Note:
21+
You are not required to print the expected output explicitly. It has already been taken care of.
22+
Constraints:
23+
0 <= N <= 10^7
24+
1 <= X <= 10^9
25+
Where X denotes the stock's price for a day.
26+
27+
Time Limit: 1 second
28+
Sample Input 1:
29+
4
30+
10 10 10 10
31+
Sample Output 1:
32+
1 1 1 1
33+
Sample Input 2:
34+
8
35+
60 70 80 100 90 75 80 120
36+
Sample Output 2:
37+
1 2 3 4 1 1 2 8 */
38+
public class StockSpan {
39+
40+
public static int[] stockSpan(int[] price) {
41+
Stack<Integer> stack = new Stack<>();
42+
int n = price.length;
43+
44+
int[] output = new int[n];
45+
46+
stack.push(0);
47+
output[0] = 1;
48+
49+
for (int i = 1; i < n; i++) {
50+
while (!stack.isEmpty() && price[stack.peek()] < price[i]) {
51+
stack.pop();
52+
}
53+
54+
if (stack.isEmpty()) {
55+
output[i] = i + 1;
56+
} else {
57+
output[i] = i - stack.peek();
58+
}
59+
stack.push(i);
60+
}
61+
return output;
62+
}
63+
64+
public static void main(String[] args) {
65+
int[] X = {60, 70, 80, 100, 90, 75, 80, 120};
66+
67+
System.out.println(Arrays.toString(stockSpan(X)));
68+
}
69+
}

stacks/ReverseStack.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ public static void main(String[] args) {
2121
}
2222
}
2323

24-
private static void reverseStack(Stack<Integer> input, Stack<Integer> extra) {
25-
if (input.size() <= 1) {
24+
private static void reverseStack(Stack<Integer> stack, Stack<Integer> helper) {
25+
if (stack.size() <= 1) {
2626
return;
2727
}
2828

29-
int lastElement = input.pop();
29+
int lastElement = stack.pop();
3030

31-
reverseStack(input, extra);
31+
reverseStack(stack, helper);
3232

33-
while (!input.isEmpty()) {
34-
int top = input.pop();
35-
extra.push(top);
33+
while (!stack.isEmpty()) {
34+
int top = stack.pop();
35+
helper.push(top);
3636
}
37-
input.push(lastElement);
37+
stack.push(lastElement);
3838

39-
while (!extra.isEmpty()) {
40-
int top = extra.pop();
41-
input.push(top);
39+
while (!helper.isEmpty()) {
40+
int top = helper.pop();
41+
stack.push(top);
4242
}
4343
}
4444
}

0 commit comments

Comments
 (0)