-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrays-hashmaps.tex
387 lines (287 loc) · 10.6 KB
/
arrays-hashmaps.tex
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
\input{preamble}
% Doc Data
\title{\textbf{\textcolor{ctpPink}{Arrays (and Hashmaps)}}}
\author{\textbf{\color{ctpGreen}{Brandon W}}}
\date{}
\begin{document}
\maketitle
\tableofcontents
\chapter{Overview}
\section{Arrays}
\subsection{Big-O Notation}
\begin{itemize}
\item \textbf{Space}: \(O(n)\)
\item \textbf{Search}: \(O(n)\)
\item \textbf{Access}: \(O(1)\)
\item \textbf{Insertion}: \(O(n)\)
\item \textbf{Deletion}: \(O(n)\)
\end{itemize}
\subsection{Creation}
\begin{lstlisting}[language=Python,style=python,caption={Arrays in Python}]
# Create empty list
my_nums = []
# Create list with values
my_nums = [5,6,7]
# Create list from string
chars = list('Hello')
# chars => ['h', 'e', 'l', 'l', 'o']
# List from set
unique_set = set(1,2,3)
unique_list = list(unique_set)
# unique_list => [1, 2, 3]
// Size of unique_list
arrLen = len(unique_list)
\end{lstlisting}
\pagebreak
\begin{lstlisting}[language=C,style=c,caption={Arrays in C}]
int main() {
// Create array with fixed size
int array[5];
array = {0, 1, 2, 3, 4};
// Or in one fell swoop
int array2[5] = {0, 1, 2, 3, 4};
// With chars
char myString = "Hello World";
// Array with 5 items with malloc()
int *myInts = (int) malloc(sizeof(int) * 5);
// Size of myInts
int arrLen = sizeof(myInts) / sizeof(myInts[0]);
}
\end{lstlisting}
\vspace{\baselineskip} % Add a blank line or adjust the space as needed
\begin{lstlisting}[language=C++,style=c, caption={Arrays in C++}]
#include <vector>
#include <string>
using namespace std;
int main() {
// Create a vector (a list) of strings
vector<string> myStrings{ "hello", "world" };
// Create a vector and add items after
vector<int> myInts;
myInts.push_back(7);
myInts.push_back(2);
// Size of myInts
int arrLen = myInts.size();
return 0;
}
\end{lstlisting}
\pagebreak
\section{Hashmaps}
\subsection{Big-O Notation}
\begin{itemize}
\item \textbf{Space}: \(O(n)\)
\item \textbf{Get (Access)}: \(O(1)\)
\item \textbf{Contains Key (Access)}: \(O(1)\)
\item \textbf{Insertion}: \(O(1)\)
\item \textbf{Delete Key}: \(O(1)\)
\end{itemize}
\subsection{Creation}
\begin{lstlisting}[language=Python,style=python,caption={Dicts (maps) in Python}]
# Create empty dict
fruits = {}
# Add item to dict
fruits['apple'] = 7
# Create dict with items in it
fruits = {
'apple': 10,
'banana': 2
}
# Create dict with 'dict' builtin func
cat_names = dict(one='Ham', two='Beanbag')
print(cat_names['two']) # => Beanbag
# You can set a default value for keys
# using a default dict so you don't
# get an error when trying to access
# an undefined key.
#
# This is very helpful for
# frequency lists.
from collections import defaultdict
values = defaultdict(int)
values['a'] = 10
print(values['a']) # => 10
print(values['b']) # => 0
\end{lstlisting}
\pagebreak
\begin{lstlisting}[language=C++,style=c,caption={Maps in C++}]
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main() {
map<string, int> fruits;
fruits.insert(pair<string,int>("Apples", 10));
fruits.insert(pair<string,int>("Bananas", 2));
// Fetch specific value
map<string,int>::iterator it;
it = fruits.find("Apples");
cout << it->first << ": " << it->second;
// => Apples: 10
return 0;
}
\end{lstlisting}
\pagebreak
\chapter{Algorithms}
\section{Binary Search}
Binary Search is a popular algorithm often used to find a target in an already-sorted list. The methodology goes as such:
\begin{itemize}
\item Create two variables that will act as a \textbf{left pointer} and a \textbf{right pointer}, with \lstinline{left} initialized to 0 to represent the beginning of the array and \lstinline{right} initialized to the last index of the array (size - 1)
\item Start a while-loop for condition \lstinline{left <= right}
\item{Calculate the middle index between \lstinline{left} and \lstinline{right}:
\newline
\lstinline{middle = left + (right - left) / 2}
}
\item Check if we found target, if so return
\item{Check if current middle item is smaller than target. If so, discard the smaller (left-hand) half, and set our left pointer to 1 after the middle: \newline
\lstinline{left = middle + 1}
}
\item{Otherwise, our current item is larger than the target. So we discard the larger half, and set our right pointer to 1 less than the middle: \newline
\lstinline{right = middle - 1}
}
\end{itemize}
The time complexity is $O(log(n))$. This is because at every iteration of the loop, we are halving what part of the array we're looking at.
\begin{lstlisting}[language=Python,style=python,caption={Binary Search In Python}]
def binary_search(items, target):
left = 0
right = len(items) - 1
while left <= right:
# // in Python rounds to the nearest integer
mid = (left + right) // 2
if items[mid] == target:
return mid
elsif items[mid] < target:
left = mid + 1
else items[mid] > target:
right = mid - 1
return -1
binary_search([2,3,4,5,6,7,8,9], 3)
\end{lstlisting}
\begin{lstlisting}[language=C++,style=c,caption={Binary Search In C++}]
int binary_search(vector<int>& items, int target) {
int left = 0;
int right = items.size() - 1;
while (left <= right):
int mid = left + (right - left) / 2;
if(items[mid] == target) {
return mid;
} else if (items[mid] < target) {
left = mid + 1;
} else (items[mid] > target) {
right = mid - 1;
}
return -1;
}
\end{lstlisting}
\pagebreak
\section{Sliding Window}
Sliding Window is a technique used to search or traverse an array while looking at a subsection of the array (a "window") at a time. The goal of sliding window algorithms is to minimize nested loops and reduce their time complexities. These algorithms are used often for the following types of problems:
\begin{itemize}
\item Minimum / Maximum Sum Array
\item Longest Sequence / Substring
\item K Closest Elements
\end{itemize}
Assume we have a problem where we want to find the largest subarray in a given array:
\begin{lstlisting}[language=Python,style=python,caption={Sliding Window In Python}]
# k is the target size of the subarray
def maximum_subarray(nums, k):
# Set the current maximum to lowest number
max_sum = float('-inf')
# Keep track of local sums
current_sum = 0
# Calculate the initial max sum
# in the first window (first k items)
for i in range(k):
current_sum += nums[i]
# Loop through the rest of array
for i in range(k, len(nums)):
# Update the largest subarray that we've seen
max_sum = max(max_sum, current_sum)
# Add the current num to the current_sum
# Subtracting the first item from the last window
current_sum += nums[i] - nums[i - k]
return max_sum
maximum_subarray([5,8,3,6,9,1,0,9], 3)
"""
So for the above example, we effectively end up seeing:
Step 1:
Our window is [5,8,3], the first k elements.
The pointer is at index 3 (value of k), but we only look
at the values before that (exclusive).
[5, 8, 3, 6, 9, 1, 0, 9]
^ ^
| |
left right
Subarray (window) sum: 5 + 8 + 3 = 16
Step 2:
[5, 8, 3, 6, 9, 1, 0, 9]
^ ^
| |
left right
"""
\end{lstlisting}
\pagebreak
\begin{lstlisting}[language=C,style=c,caption={Sliding Window In C}]
#include <stdio.h>
int maximumSubarray(int* nums, int numsSize, int target);
int max(int, int);
int main() {
int nums[8] = {5, 8, 3, 6, 9, 1, 0, 9};
int numsSize = sizeof(nums) / sizeof(int);
int maxSum = maximumSubarray(nums, numsSize, 3);
printf("Maximum sub in subarray: %d", maxSum);
}
int maximumSubarray(int* nums, int numsSize, int k) {
int maxSum = -__INT_MAX__;
int currentSum = 0;
for (int i = 0; i < k; i++) {
currentSum += nums[i];
}
for (int i = k; i < numsSize; i++) {
maxSum = max(maxSum, currentSum);
currentSum += nums[i] - nums[i - k];
}
return maxSum;
}
int max(int a, int b) {
if (a > b)
return a;
return b;
}
\end{lstlisting}
\pagebreak
\section{Two Sum}
The two sum algorithm is a simple one used when needing to find if two numbers in a list can be added to equate to a given target.\newline
For the simplest form of two sum (exemplified below), the algorithm goes as such:
\begin{itemize}
\item Loop through input
\item Calculate the difference between the current item and the target
\item If the complement is in the hashmap, return the current index and the index of the complement
\item Add the current number to a hashmap as a key, who's value is the index
\end{itemize}
This can be done in $O(n)$ time. The reason this works is because at every iteration of our loop, we're seeing if we already came across a number (an appropriate complement of the current number) that will make the current number equal our target when added together.
\begin{lstlisting}[language=Python, style=python, caption={Two Sum in Python}]
def two_sum(nums, target):
complements = {}
for i in range(len(arr)):
diff = target - nums[i]
if diff in complements:
return [i, complements[diff]]
complements[nums[i]] = i
two_sum([9,6,11,15], 17)
\end{lstlisting}
\begin{lstlisting}[language=C++,style=c, caption={Two Sum in C++}]
vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> complements;
vector<int> result;
for(int i = 0; i < nums.size(); i++) {
if (complements.count(target - nums[i]) > 0) {
return {i, complements[target - nums[i]]};
}
complements.insert(pair<int,int>(nums[i], i));
}
return {-1, -1};
}
\end{lstlisting}
\pagebreak
\section{Top K Frequent Elements}
\end{document}