Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#executable
*.elf
*.exe
*.out
128 changes: 128 additions & 0 deletions codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>

#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);
}
23 changes: 11 additions & 12 deletions codefest-2022/Algorithm Challenge/Algorithm Solutions/Minstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()