Skip to content

Commit 19c4450

Browse files
committed
add
1 parent 905d7f7 commit 19c4450

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

005 - Linked List/Practice 01/Prac Rec - 01.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,45 @@ Node* kReverse(Node* head,int k){
527527
return vishal;
528528
}
529529

530+
//Next Number
531+
Node* nextNumber(Node* head){
532+
if(head==NULL){
533+
return NULL;
534+
}
535+
Node *temp = head;
536+
int count = 1;
537+
while(temp->next!=NULL){
538+
temp = temp->next;
539+
count++;
540+
}
541+
if(temp->data != 9){
542+
temp->data = (temp->data)+1;
543+
return head;
544+
}
545+
Node *amit = head;
546+
int sum = 0;
547+
for(int i=count-1;i>=0;i--){
548+
sum = sum*10 + (amit->data);
549+
amit = amit->next;
550+
}
551+
sum = sum+1;
552+
Node *yash_head = NULL;
553+
Node *yash_tail = NULL;
554+
while(sum>0){
555+
Node *vishal = new Node(sum%10);
556+
if(yash_head==NULL){
557+
yash_head = vishal;
558+
yash_tail = vishal;
559+
}else{
560+
yash_tail->next = vishal;
561+
yash_tail = yash_tail->next;
562+
}
563+
sum/=10;
564+
}
565+
return yash_head;
566+
}
567+
568+
530569

531570
int main(){
532571
//10 20 30 40 50 60 70 80 90 100
@@ -554,5 +593,8 @@ int main(){
554593
// printLinkedList(skipMdeleteN(head,2,2));
555594
// printLinkedList(swapTwoNodes(head,2,4));
556595
// printLinkedList(kReverse(head,3));
596+
597+
598+
printLinkedList(deleteAlternative(head));
557599
return 0;
558600
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<stack>
2+
int countBracketReversals(string s) {
3+
// Write your code here
4+
if(s.length()%2!=0){
5+
return -1;
6+
}
7+
stack<char> st;
8+
for(int i=0;i<s.length();i++){
9+
if(s[i]=='}'&&st.size()!=0&&st.top()=='{'){
10+
st.pop();
11+
}else{
12+
st.push(s[i]);
13+
}
14+
15+
}
16+
char c1,c2;
17+
int count=0;
18+
while(st.size()!=0){
19+
c1=st.top();
20+
st.pop();
21+
c2=st.top();
22+
st.pop();
23+
if(c1==c2){
24+
count++;
25+
}else if(c1=='{'){
26+
count+=2;
27+
}
28+
}
29+
return count;
30+
31+
}

006 - Stack/Practice 01/practice 01.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,58 @@ bool redundantBracket(string s){
6363
return false;
6464
}
6565

66+
//Stock Span Function
67+
int* stockSpan(int* price,int size){
68+
int* data=new int[size];
69+
stack<int> st;
70+
st.push(0);
71+
data[0]=1;
72+
for(int i=1;i<size;i++){
73+
while(st.size()!=0 && price[i]>price[st.top()]){
74+
st.pop();
75+
}
76+
if(st.size()==0){
77+
data[i]=i+1;
78+
}else{
79+
data[i]=i-st.top();
80+
}
81+
st.push(i);
82+
}
83+
return data;
84+
}
85+
86+
//Minimum Brackets Reversal
87+
int minBracketRev(string s){
88+
if(s.length()%2!=0){
89+
return -1;
90+
}
91+
stack<char> st;
92+
for(int i=0;i<s.length();i++){
93+
if(s[i]=='}'&&st.size()!=0&&st.top()=='{'){
94+
st.pop();
95+
}else{
96+
st.push(s[i]);
97+
}
98+
99+
}
100+
char c1,c2;
101+
int count=0;
102+
while(st.size()!=0){
103+
c1=st.top();
104+
st.pop();
105+
c2=st.top();
106+
st.pop();
107+
if(c1==c2){
108+
count++;
109+
}else{
110+
if(c1=='}'){
111+
count+=2;
112+
}
113+
}
114+
}
115+
return count;
116+
}
117+
66118
int main(){
67119

68120

@@ -132,5 +184,6 @@ int main(){
132184

133185
// cout<<redundantBracket("(a+b)")<<endl;
134186

187+
cout<<minBracketRev("{{{{}{")<<endl;
135188

136189
}

006 - Stack/Stock Span.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stack>
2+
int* stockSpan(int *price, int size) {
3+
// Write your code here
4+
stack<int> st;
5+
int *ans = new int[size];
6+
ans[0]=1;
7+
st.push(0);
8+
for(int i=1;i<size;i++){
9+
while(st.size()!=0 && price[i]>price[st.top()]){
10+
st.pop();
11+
}
12+
if(st.size()==0){
13+
ans[i]=i+1;
14+
}else{
15+
ans[i]=i-st.top();
16+
}
17+
st.push(i);
18+
}
19+
return ans;
20+
}

0 commit comments

Comments
 (0)