summaryrefslogtreecommitdiff
path: root/src/01/06/README.md
blob: bb2d591b19c7503b81d0c40240baff06d3b5432c (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Learning Profile for Assignment #1 - Question #6 - Computer Science 272: Data Structures and Algorithms

Name: Mo Khan
Student ID: 3431709

## Problem Statement

Design and implement a MinStack data structure that can store comparable elements and supports the stack operations `push(x)`, `pop()`, and `size()`,
as well as the `min()` operation, which returns the minimum value currently stored in the data structure.

All operations should run in constant time.

## Description of the Code

To keep track of the min value, I chose to use two Stacks.
One stack keeps track of each item that is pushed on.
The second stack keeps track of each new minimum value that is pushed on to the stack.

`push()`, `pop()` and `size()` are operate in constant time.
However, `min` operates in constant time until each of the min values
have been popped off of the min stack. After that it falls back to a linear
time algorithm to determine the next min.

## Errors and Warnings

```bash
モ make run_test
clang build/min_stack.o build/min_stack_test.o -lcgreen -o build/test
cgreen-runner -c -v build/test
Discovered MinStack:when_a_single_item_is_on_the_stack_it_has_a_size_of_one (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_it_has_a_size_of_one__)
Discovered MinStack:when_a_single_item_is_on_the_stack_it_is_the_min (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_it_is_the_min__)
Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null__)
Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero__)
Discovered MinStack:when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item (CgreenSpec__MinStack__when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item__)
Discovered MinStack:when_empty (CgreenSpec__MinStack__when_empty__)
Discovered MinStack:when_empty_it_has_a_min_of_null (CgreenSpec__MinStack__when_empty_it_has_a_min_of_null__)
Discovered MinStack:when_empty_it_has_a_size_of_zero (CgreenSpec__MinStack__when_empty_it_has_a_size_of_zero__)
Discovered MinStack:when_pushing_a_single_integer (CgreenSpec__MinStack__when_pushing_a_single_integer__)
Discovered MinStack:when_pushing_duplicate_values_on_to_the_stack (CgreenSpec__MinStack__when_pushing_duplicate_values_on_to_the_stack__)
Discovered MinStack:when_pushing_multiple_integers_out_of_order (CgreenSpec__MinStack__when_pushing_multiple_integers_out_of_order__)
Discovered 11 test(s)
Opening [build/test] to run all 11 discovered tests ...
Running "test" (11 tests)...
  "MinStack": 39 passes in 7ms.
Completed "test": 39 passes in 7ms.
```

## Sample Input and Output

```bash
モ make run
clang    -c -o build/main.o main.c
clang build/min_stack.o build/main.o -o build/program
./build/program
=== COMP-272 - Assignment 1 - Question 6 ===
Pushing:
==========
Push: 7, Min: 7
	[7]
Push: 24, Min: 7
	[24][7]
Push: 23, Min: 7
	[23][24][7]
Push: 8, Min: 7
	[8][23][24][7]
Push: 5, Min: 5
	[5][8][23][24][7]
Push: 22, Min: 5
	[22][5][8][23][24][7]
Push: 19, Min: 5
	[19][22][5][8][23][24][7]
Push: 3, Min: 3
	[3][19][22][5][8][23][24][7]
Push: 23, Min: 3
	[23][3][19][22][5][8][23][24][7]
Push: 9, Min: 3
	[9][23][3][19][22][5][8][23][24][7]
Push: 15, Min: 3
	[15][9][23][3][19][22][5][8][23][24][7]
Push: 15, Min: 3
	[15][15][9][23][3][19][22][5][8][23][24][7]
Push: 17, Min: 3
	[17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 17, Min: 3
	[17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 12, Min: 3
	[12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 3, Min: 3
	[3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 2, Min: 2
	[2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 4, Min: 2
	[4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 15, Min: 2
	[15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 12, Min: 2
	[12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 3, Min: 2
	[3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 19, Min: 2
	[19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 9, Min: 2
	[9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 7, Min: 2
	[7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Push: 10, Min: 2
	[10][7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Popping:
==========
	[10][7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 10, Min: 2
	[7][9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 7, Min: 2
	[9][19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 9, Min: 2
	[19][3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 19, Min: 2
	[3][12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 3, Min: 2
	[12][15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 12, Min: 2
	[15][4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 15, Min: 2
	[4][2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 4, Min: 2
	[2][3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 2, Min: 3
	[3][12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 3, Min: 5
	[12][17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 12, Min: 5
	[17][17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 17, Min: 5
	[17][15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 17, Min: 5
	[15][15][9][23][3][19][22][5][8][23][24][7]
Pop: 15, Min: 5
	[15][9][23][3][19][22][5][8][23][24][7]
Pop: 15, Min: 5
	[9][23][3][19][22][5][8][23][24][7]
Pop: 9, Min: 5
	[23][3][19][22][5][8][23][24][7]
Pop: 23, Min: 5
	[3][19][22][5][8][23][24][7]
Pop: 3, Min: 5
	[19][22][5][8][23][24][7]
Pop: 19, Min: 5
	[22][5][8][23][24][7]
Pop: 22, Min: 5
	[5][8][23][24][7]
Pop: 5, Min: 7
	[8][23][24][7]
Pop: 8, Min: 7
	[23][24][7]
Pop: 23, Min: 7
	[24][7]
Pop: 24, Min: 7
	[7]
Pop: 7, Min: 0

Bye
```

## Discussion

I struggled to find a good algorithm that would guarantee a constant time
implementation of `min()`.

The worst case scenario for this implementation is when the min value is pushed on to
the stack right in the center. Half of the pops will would yield a constant time
implementation of `min()` and the other half would yield a linear time implementation
of `min()`.