-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaksQuestion.java
More file actions
143 lines (110 loc) · 3.91 KB
/
staksQuestion.java
File metadata and controls
143 lines (110 loc) · 3.91 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.util.Arrays;
import java.util.Scanner;
import java.util.Stack;
/* Question 1 ::- find the highest count of sum can be possible */
class twoStacks{
static int TwoStacks(int maxSum , int []a , int b[]){
return TwoStacks(maxSum, a , b ,0 , 0) - 1;
}
private static int TwoStacks (int maxSum , int[] a , int [] b , int sum , int count ){
if (sum>maxSum){
return count ;
}
if (a.length== 0 || b.length == 0 ){
return count ;
}
int ansL = TwoStacks(maxSum, Arrays.copyOfRange(a,1 ,a.length), b, sum + a[0], count+1) ;
int ansR = TwoStacks(maxSum, a , Arrays.copyOfRange(b,1 ,b.length), sum + b[0], count+1) ;
return Math.max(ansL, ansR);
}
}
/* Question 2 ::- find the largest area of histogram */
class LargestAreaHistogram{
public int largestAreaHistogram (int [] heights){
Stack <Integer> stack = new Stack<>();
int max = 0 ;
stack.push(0);
for (int i = 1 ; i < heights.length ; i++){
while(!stack.isEmpty() && heights[i] < heights [stack.peek()]){
max = getMax(heights , stack , max , i);
}
stack.push(i);
}
int j = heights.length;
while(!stack.isEmpty()){
max = getMax(heights , stack , max , j);
}
return max ;
}
private int getMax(int [] arr , Stack<Integer> stack , int max , int i){
int area ;
int popped = stack.pop();
if (stack.isEmpty()){
area = arr[popped] * i ;
}
else{
area = arr[popped] * (i - 1 - stack.peek());
}
return Math.max(max , area);
}
}
/* Question 3 :- where a bracket string is valid or not */
class bracket {
public boolean isValid (String s ){
Stack<Character> stack = new Stack<>();
for (char ch : s.toCharArray()){
if (ch == '(' || ch == '[' || ch == '{' ){
stack.push(ch);
}
else {
if (ch == ')'){
if (stack.isEmpty() || stack.pop() != '('){
return false;
}
}
if (ch == ']'){
if (stack.isEmpty() || stack.pop() != '['){
return false;
}
}
if (ch == '}'){
if (stack.isEmpty() || stack.pop() != '{'){
return false;
}
}
}
}
return stack.isEmpty() ;
}
}
public class staksQuestion {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
twoStacks ts = new twoStacks();
/*
System.out.println("no of test cases:-");
int t = sc.nextInt();
for (int i = 0 ; i < t ;i++){
System.out.println("write length of first array , length of second array , max sum");
int n = sc.nextInt();
int m = sc.nextInt() ;
int x = sc.nextInt();
int [] a = new int[n] ;
int []b = new int[m];
System.out.println("write first array ");
for (int j = 0 ;j < n ; j++){
a[j]= sc.nextInt();
}
System.out.println("write second array ");
for (int j = 0 ;j < m ; j++){
b[j]= sc.nextInt();
}
System.out.println("highest count number :- ");
System.out.println(ts.TwoStacks(x ,a, b));
}*/
/*int [] heights = {2,1,5,6,2,3};
LargestAreaHistogram l = new LargestAreaHistogram();
System.out.println(l.largestAreaHistogram(heights));
*/
}
}