Skip to content

Commit a40ccb6

Browse files
committed
Improve cheat sheets
1 parent d112902 commit a40ccb6

File tree

2 files changed

+100
-108
lines changed

2 files changed

+100
-108
lines changed
Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,11 @@
11
# Naming conventions & mapping
22

3-
## SplHeap
4-
SplMinHeap / SplMaxHeap
5-
6-
Construct โž™ ๐‘‚โŸฎ๐‘›โŸฏ
7-
8-
#### Add an element
9-
`add()`โž™`insert()`
10-
11-
TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ<br>
12-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
13-
14-
#### Pop an element
15-
`pop()`โž™`extract()`
16-
17-
TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ<br>
18-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
19-
20-
#### Peek element
21-
`peek()`โž™`top()`
22-
23-
TC: ๐‘‚โŸฎ๐ŸทโŸฏ<br>
24-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
25-
26-
#### DS size
27-
`size()`โž™`count()`
28-
29-
TC: ๐‘‚โŸฎ๐ŸทโŸฏ<br>
30-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
31-
32-
33-
## SplPriorityQueue
34-
โ„น๏ธ Implemented using a `SplMaxHeap`
35-
36-
#### Add an element
37-
`add()`โž™`insert()`
38-
39-
TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ<br>
40-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
41-
42-
#### Pop an element
43-
`pop()`โž™`extract()`
44-
45-
TC: ๐‘‚โŸฎใ’ ๐‘›โŸฏ<br>
46-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
47-
48-
#### Peek element
49-
`peek()`โž™`top()`
50-
51-
TC: ๐‘‚โŸฎ๐ŸทโŸฏ<br>
52-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
53-
54-
#### DS size
55-
`size()`โž™`count()`
56-
57-
TC: ๐‘‚โŸฎ๐ŸทโŸฏ<br>
58-
SC: ๐‘‚โŸฎ๐ŸทโŸฏ
59-
60-
61-
## SplQueue
62-
โ„น๏ธ Implemented using a `SplDoublyLinkedList`
63-
64-
#### Enqueue an element
65-
`enqueue()`
66-
67-
#### Dequeue an element
68-
`dequeue()`
69-
70-
#### Peek element
71-
`peek()`โž™`bottom()`
72-
73-
#### DS size
74-
`size()`โž™`count()`
75-
76-
#### Front / Rear
77-
โš ๏ธ Not actually available on "real" queue !
78-
79-
`front()`โž™`bottom()` โ€” `peek()`<br>
80-
`rear()`โž™`top()`
81-
82-
## SplStack
83-
โ„น๏ธ Implemented using a `SplDoublyLinkedList`
84-
85-
#### push an element
86-
`push()`
87-
88-
#### pop an element
89-
`pop()`
90-
91-
#### Peek element
92-
`peek()`โž™`top()`
93-
94-
#### DS size
95-
`size()`โž™`count()`
96-
97-
#### Front / Rear
98-
โš ๏ธ Not actually available on "real" stack !
99-
100-
`front()`โž™`top()` โ€” `peek()`<br>
101-
`rear()`โž™`bottom()`
3+
| Operation | SplHeap | SplPriorityQueue | SplQueue | SplStack |
4+
|:-----------|:---------------------|:--------------------|:----------------------|:----------------------|
5+
| Add | `add()`โž™`insert()` | `add()`โž™`insert()` | `enqueue()` | `push()` |
6+
| Remove | `pop()`โž™`extract()` | `pop()`โž™`extract()` | `dequeue()` | `pop()` |
7+
| Peek | `peek()`โž™`top()` | `peek()`โž™`top()` | `peek()`โž™`bottom()` | `peek()`โž™`top()` |
8+
| Size | `size()`โž™`count()` | `size()`โž™`count()` | `size()`โž™`count()` | `size()`โž™`count()` |
9+
| Front | โ€” | โ€” | `front()`โž™`bottom()` | `front()`โž™`top()` |
10+
| Rear | โ€” | โ€” | `rear()`โž™`top()` | `rear()`โž™`bottom()` |
11+
| Note โ„น๏ธ | โ€” | `SplMaxHeap` | `SplDoublyLinkedList` | `SplDoublyLinkedList` |

โ€Žcheat_sheets/complexity.MDโ€Ž

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Complexity
22

3-
## Time Complexity
43
<!--
54
๐‘‚โŸฎ๐‘›โŸฏ
65
๐‘› / ๐‘š / ๐‘ฅ / ๐‘ฆ / ๐‘ง / ๐˜ฉ (look for "MATHEMATICAL ITALIC SMALL [THE LETTER]")
@@ -11,16 +10,99 @@
1110
๐Ÿถ / ๐Ÿท / ๐Ÿธ / ๐Ÿน / ๐Ÿบ / ๐Ÿป / ๐Ÿผ / ๐Ÿฝ / ๐Ÿพ / ๐Ÿฟ
1211
-->
1312

13+
## Array
14+
โ„น๏ธ For an array of `๐‘›` values, `๐‘ฅ` is the index of a given value in the array (from `๐Ÿถ` to `๐‘› โˆ’ ๐Ÿท`)
15+
16+
| Operation | TC | SC | Notes |
17+
|:------------------|:------------|:-------|:-------------------------------------------------------------------------------------------|
18+
| Add โ€” at index ๐‘ฅ | ๐‘‚โŸฎ๐‘› โˆ’ ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to push values from index `๐‘ฅ` to `๐‘› โˆ’ ๐Ÿท`,<br> in order to make room for new value |
19+
| Add โ€” as head | ๐‘‚โŸฎ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to push each and every values,<br> in order to make room for new head value |
20+
| Add โ€” as tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
21+
| Remove โ€” index ๐‘ฅ | ๐‘‚โŸฎ๐‘› โˆ’ ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to move back values from `๐‘ฅ ๏นข ๐Ÿท` index to tail value,<br> in order to fill the hole |
22+
| Remove โ€” head | ๐‘‚โŸฎ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to move back each and every remaining values,<br> in order to fill the hole |
23+
| Remove โ€” tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
24+
| Peek index ๐‘ฅ | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
25+
26+
## Vector
27+
โ„น๏ธ For a vector of `๐‘›` values, `๐‘ฅ` is the index of a given value in the vector (from `๐Ÿถ` to `๐‘› โˆ’ ๐Ÿท`)
28+
29+
โš ๏ธ๏ธ Amortized vs Reallocation:<br>
30+
Some operation may require copying current elements into a larger vector (when capacity execeeded).<br>
31+
Which implies to iterate over each and every values, implying ๐‘‚โŸฎ๐‘›โŸฏ time complexity (and likely ๐‘‚โŸฎ๐‘›โŸฏ space complexity then)
32+
33+
| Operation | TC | SC | Notes |
34+
|:------------------|:------------------------------------------------|:-------|:-------------------------------------------------------------------------------------------|
35+
| Add โ€” at index ๐‘ฅ | ๐‘‚โŸฎ๐‘› โˆ’ ๐‘ฅโŸฏ (Amortized) | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to push values from index `๐‘ฅ` to `๐‘› โˆ’ ๐Ÿท`,<br> in order to make room for new value |
36+
| Add โ€” as head | ๐‘‚โŸฎ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to push each and every values,<br> in order to make room for new head value |
37+
| Add โ€” as tail | ๐‘‚โŸฎ๐ŸทโŸฏ (Amortized) | ๐‘‚โŸฎ๐ŸทโŸฏ | |
38+
| Remove โ€” index ๐‘ฅ | ๐‘‚โŸฎ๐‘› โˆ’ ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to move back values from `๐‘ฅ ๏นข ๐Ÿท` index to tail value,<br> in order to fill the hole |
39+
| Remove โ€” head | ๐‘‚โŸฎ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to move back each and every remaining values,<br> in order to fill the hole |
40+
| Remove โ€” tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
41+
| Peek index ๐‘ฅ | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
42+
43+
## Linked list
44+
โ„น๏ธ For a LL of `๐‘›` nodes, `๐‘ฅ` is the position of a given node in the list (from `๐Ÿถ` to `๐‘› โˆ’ ๐Ÿท`)
45+
46+
| Operation | TC | SC | Notes |
47+
|:---------------------------------|:-------|:-------|:------------------------------------------------------------------------------------------------------------------------|
48+
| Add โ€” at position `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ โˆ’ ๐Ÿท`,<br> in order to retrieve node `๐‘ฅ โˆ’ ๐Ÿท` and be able to link it to new node |
49+
| Add โ€” **after** given node `๐‘ฅ` | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
50+
| Add โ€” **before** given node `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ โˆ’ ๐Ÿท`,<br> in order to retrieve node `๐‘ฅ โˆ’ ๐Ÿท` and be able to link it to new node |
51+
| Add โ€” as head | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
52+
| Add โ€” as tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | โš ๏ธ Assuming tail node is known !<br>Otherwise ๐‘‚โŸฎ๐‘›โŸฏ, in order to retrieve current tail node |
53+
| Remove โ€” at position `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ โˆ’ ๐Ÿท`,<br> in order to be able to link node `๐‘ฅ โˆ’ ๐Ÿท` to node `๐‘ฅ ๏นข ๐Ÿท` |
54+
| Remove โ€” given node `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ โˆ’ ๐Ÿท`,<br> in order to be able to link node `๐‘ฅ โˆ’ ๐Ÿท` to node `๐‘ฅ ๏นข ๐Ÿท` |
55+
| Remove โ€” head | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
56+
| Remove โ€” tail | ๐‘‚โŸฎ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node before tail node,<br> in order to unlink it from original tail node |
57+
| Find node `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ` |
58+
59+
## Doubly linked list
60+
โ„น๏ธ For a DLL of `๐‘›` nodes, `๐‘ฅ` is the position of a given node in the list (from `๐Ÿถ` to `๐‘› โˆ’ ๐Ÿท`)
61+
62+
| Operation | TC | SC | Notes |
63+
|:---------------------------------|:-------|:-------|:----------------------------------------------------------------------------------------------------------------------------------------|
64+
| Add โ€” at position `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ โˆ’ ๐Ÿท`,<br> in order to retrieve node `๐‘ฅ โˆ’ ๐Ÿท` and be able to link it to new node |
65+
| Add โ€” **after** given node `๐‘ฅ` | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
66+
| Add โ€” **before** given node `๐‘ฅ` | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
67+
| Add โ€” as head | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
68+
| Add โ€” as tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | โš ๏ธ Assuming tail node is known !<br>Otherwise ๐‘‚โŸฎ๐‘›โŸฏ in order to retrieve original tail node |
69+
| Remove โ€” at position `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ`,<br> in order to retrieve node `๐‘ฅ` |
70+
| Remove โ€” given node `๐‘ฅ` | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
71+
| Remove โ€” head | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
72+
| Remove โ€” tail | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | โš ๏ธ Assuming tail node is known !<br>Otherwise ๐‘‚โŸฎ๐‘›โŸฏ, in order to retrieve node before tail node and unlink it from original tail node |
73+
| Find node `๐‘ฅ` | ๐‘‚โŸฎ๐‘ฅโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | Need to iterate from head to node `๐‘ฅ` |
74+
75+
## Heap
76+
| Operation | TC | SC | Notes |
77+
|:----------|:---------|:-------|--------------------------------------------------------------------|
78+
| Add | ๐‘‚โŸฎใ’ ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | In order to re-balance the tree after the new value has been added |
79+
| Pop | ๐‘‚โŸฎใ’ ๐‘›โŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | In order to re-balance the tree after the value has been popped |
80+
| Peek | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ | |
81+
82+
## Queue / Stack
83+
โ„น๏ธ Assuming implementation leveraging a circular array (finite-size) or Linked list.<br>
84+
_Using a simple array for instance would imply pushing/moving back values depending on the operation (see Array above)_
85+
86+
| Operation | TC | SC |
87+
|:---------------|:-------|:-------|
88+
| Enqueue / Push | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ |
89+
| Dequeue / Pop | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ |
90+
| Peek | ๐‘‚โŸฎ๐ŸทโŸฏ | ๐‘‚โŸฎ๐ŸทโŸฏ |
91+
92+
## Time Complexity
93+
94+
### Examples
95+
1496
- ๐‘ฅ the length of a 2D list
1597
- ๐‘ฆ the height of a 2D list
1698
- ๐‘› the number of item in a list (e.g: ๐‘› = ๐‘ฅ ร— ๐‘ฆ 2D list)
1799

18-
### ๐‘‚โŸฎ๐ŸทโŸฏ
100+
#### ๐‘‚โŸฎ๐ŸทโŸฏ
19101
```
20102
DoSomething();
21103
```
22104

23-
### ๐‘‚โŸฎ๐‘›โŸฏ
105+
#### ๐‘‚โŸฎ๐‘›โŸฏ
24106
```php
25107
for (i=0; i<n; i++) { // 1D list
26108
DoSomething();
@@ -32,15 +114,15 @@ for (i=0; i<x; i++) { // 2D list (๐‘› = ๐‘ฅ ร— ๐‘ฆ)
32114
}
33115
```
34116

35-
### ๐‘‚โŸฎใ’ ๐‘›โŸฏ
117+
#### ๐‘‚โŸฎใ’ ๐‘›โŸฏ
36118

37119
```php
38120
for ($i=0; $i<$n; $i=$i*2) { // Notice `$i=$i*2` !
39121
DoSomething();
40122
}
41123
```
42124

43-
### ๐‘‚โŸฎ๐‘› ใ’ ๐‘›โŸฏ
125+
#### ๐‘‚โŸฎ๐‘› ใ’ ๐‘›โŸฏ
44126

45127
```php
46128
for ($i=0; $i<n; $i++) {
@@ -50,7 +132,7 @@ for ($i=0; $i<n; $i++) {
50132
}
51133
```
52134

53-
### ๐‘‚โŸฎใ’ยฒ ๐‘›โŸฏ
135+
#### ๐‘‚โŸฎใ’ยฒ ๐‘›โŸฏ
54136
```php
55137
for ($i=0; $i<$n; $i=$i*2) {
56138
for ($j=0; $j<$n; $j=$j*2) {
@@ -59,7 +141,7 @@ for ($i=0; $i<$n; $i=$i*2) {
59141
}
60142
```
61143

62-
### ๐‘‚โŸฎ๐‘›ยฒโŸฏ
144+
#### ๐‘‚โŸฎ๐‘›ยฒโŸฏ
63145

64146
```php
65147
for ($i=0; $i<$n; $i=$i++) {
@@ -69,7 +151,7 @@ for ($i=0; $i<$n; $i=$i++) {
69151
}
70152
```
71153

72-
### ๐‘‚โŸฎ๐Ÿธ^๐‘›โŸฏ
154+
#### ๐‘‚โŸฎ๐Ÿธ^๐‘›โŸฏ
73155

74156
```php
75157
DoSomething(int $n): int
@@ -82,7 +164,7 @@ DoSomething(int $n): int
82164
}
83165
```
84166

85-
### ๐‘‚โŸฎยฝ ร— ๐‘ฅ ร— โŸฎ๐‘ฅ๏นข๐ŸทโŸฏโŸฏ versus ๐‘‚โŸฎ๐‘›โŸฏ
167+
#### ๐‘‚โŸฎยฝ ร— ๐‘ฅ ร— โŸฎ๐‘ฅ๏นข๐ŸทโŸฏโŸฏ versus ๐‘‚โŸฎ๐‘›โŸฏ
86168
Each cell is visited **only one time**, rather than two time if `$j=0` !
87169

88170
```php

0 commit comments

Comments
ย (0)