diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore b/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore new file mode 100644 index 0000000..5129e5c --- /dev/null +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/.gitignore @@ -0,0 +1,4 @@ +#executable +*.elf +*.exe +*.out \ No newline at end of file diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c new file mode 100644 index 0000000..e0aab29 --- /dev/null +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c @@ -0,0 +1,128 @@ +// Using Dynamic arrays to Implement minstack +// Linked list or static arrays can also be used but there are pros and cons with each Data Structure used + +#include +#include + +#define INIT_SIZE 8 + +typedef int len_t; + +/** +* struct _minstack- stack of integers +* @capacity: capacity of stack +* @size: size of stack +* @arr: pointer to stack array +* @top: top of stack +* @minarr: stack of the minimum value at every point +*/ +typedef struct _minStack +{ + int *arr; + int *minarr; + + len_t capacity; + len_t size; + len_t top; + +}minStack; + +/** +* MyStack- initializes stack +* Return: stack pointer +* +*/ +minStack *MyStack(void) +{ + minStack *s = (minStack *)malloc(sizeof(minStack)); + s->arr = (int *)malloc(sizeof(int) * INIT_SIZE); + s->minarr = (int *)malloc(sizeof(int) * INIT_SIZE); + s->capacity = INIT_SIZE; + s->top = -1; + s->size = 0; + return (s); +} + +/** +* push- pushes to top of stack +* @s: stack pointer +* @val: value to be pushed +* +*/ +void push(minStack *s, int val) +{ + //Resize array if needed + if (s->size == s->capacity) + { + s->arr = (int *)realloc(s->arr, sizeof(int) * s->capacity * 2); + s->minarr = (int *)realloc(s->minarr, sizeof(int) * s->capacity * 2); + s->capacity *= 2; + } + + s->arr[s->size] = val; + if (s->size && val >= s->minarr[s->size - 1]) + s->minarr[s->size] = s->minarr[s->size - 1]; + else + s->minarr[s->size] = val; + s->top = s->size; + s->size++; +} + +/** +* pop- pops the top of stack +* @s: stack pointer +* +*/ +void pop(minStack *s) +{ + s->size--; + s->top--; + + //resize array if needed + if (s->size <= s->capacity / 4) + { + s->arr = (int *)realloc(s->arr, sizeof(int) * s->capacity / 2); + s->minarr = (int *)realloc(s->minarr, sizeof(int) * s->capacity / 2); + s->capacity /= 2; + } +} + +/** +* printstack- prints stack for testing +* @s: stack pointer +* +*/ +void printstack(minStack *s) +{ + int top = s->top; + + printf(" \tStack\t \t \tMinstack\n\n"); + printf("top --->|\t%i\t|\ttop --->|\t%i\t|\n", s->arr[top], s->minarr[top]); + for (int i = top - 1; i >= 0; i--) + printf(" |\t%i\t|\t |\t%i\t|\n", s->arr[i], s->minarr[i]); +} +/** +* main- let's test +* +* Return: 0 on success +*/ +int main (void) +{ + minStack *s = MyStack(); + push(s, 7); + push(s, 9); + push(s, 8); + push(s, 59); + push(s, 0); + push(s, 87); + push(s, 77); + push(s, 45); + + pop(s); + pop(s); + + //printf("\t%i\n", s->arr[s->top]); + printstack(s); + + return (0); +} diff --git a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py index d182b02..6d48978 100644 --- a/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py +++ b/codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py @@ -24,15 +24,14 @@ def printMe(self): print(self.stack) print(self.minStack) - -newStack = MinStack() -newStack.push(-2) -newStack.push(0) -newStack.push(-3) -newStack.push(-5) -newStack.printMe() -print(newStack.getMin()) -print(newStack.pop()) -print(newStack.getMin()) -newStack.printMe() - +if __name__ == "__main__": + newStack = MinStack() + newStack.push(-2) + newStack.push(0) + newStack.push(-3) + newStack.push(-5) + newStack.printMe() + print(newStack.getMin()) + print(newStack.pop()) + print(newStack.getMin()) + newStack.printMe()