-
Notifications
You must be signed in to change notification settings - Fork 0
/
946.c
92 lines (80 loc) · 1.68 KB
/
946.c
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
#include <stdio.h>
#include <stdbool.h>
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize)
{
int i = 0,j = 0;
int top = 0;
int k = 0;
int *arr=(int*)malloc(pushedSize * (sizeof(int)));
for(i = 0; i < pushedSize; i++)
{
arr[top]=pushed[i];
while(top >= 0 && j < pushedSize && popped[j] == arr[top])
{
top--;
j++;
}
top++;
}
if(top == 0) return 1;
return 0;
}
/*
Best Solution
Using Stack
typedef struct
{
int index;
int *arr;
}STACK;
STACK* init_stack(int n)
{
STACK *s = malloc(sizeof(STACK));
s->index = 0;
s->arr = calloc(n, sizeof(int));
return s;
}
void push_stack(int val, STACK* s)
{
s->arr[s->index++] = val;
}
void pop_stack(STACK* s)
{
s->index--;
}
bool is_empty_stack(STACK* s)
{
return s->index == 0;
}
void free_stack(STACK* s)
{
free(s->arr);
free(s);
}
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize){
if(!memcmp(pushed, popped, pushedSize*sizeof(int)))
return true;
STACK *s = init_stack(pushedSize);
int popped_index = 0;
for(int i = 0; i< pushedSize; i++)
{
while(s->index > 0 && s->arr[s->index-1] == popped[popped_index])
{
pop_stack(s);
popped_index++;
}
if(pushed[i] != popped[popped_index])
push_stack(pushed[i], s);
else
popped_index++;
}
for(int i = popped_index; i< poppedSize; i++)
{
if(popped[i] == s->arr[s->index-1])
pop_stack(s);
}
bool b = is_empty_stack(s);
free_stack(s);
return b;
}
*/