You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/Dynamic Programming/Longest Common Subseqence.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
#### Problem Statement
4
4
5
-
Given two strings `S` and `T`, find the length of the longest common subsequence (<b>LCS</b>).
5
+
Given two strings `S` and `T`, find the length of the longest common subsequence (<b>LCS</b>).
6
6
7
7
#### Approach
8
8
@@ -19,14 +19,14 @@ We could see that we can fill our `dp` table row by row, column by column. So ou
19
19
- Let's say that we have strings `S` of the length N and `T` of the length M (numbered from 1). Let's create the table `dp` of size `(N + 1) x (M + 1)` numbered from 0.
20
20
- Let's fill the 0th row and the 0th column of `dp` with 0.
21
21
- Then, we follow the algorithm:
22
-
```
23
-
for i in range(1..N):
24
-
for j in range(1..M):
25
-
if(S[i] == T[j])
26
-
dp[i][j] = dp[i - 1][j - 1] + 1
27
-
else
28
-
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
29
-
```
22
+
```
23
+
for i in range(1..N):
24
+
for j in range(1..M):
25
+
if(S[i] == T[j])
26
+
dp[i][j] = dp[i - 1][j - 1] + 1
27
+
else
28
+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
29
+
```
30
30
31
31
32
32
#### Time Complexity
@@ -35,8 +35,8 @@ We could see that we can fill our `dp` table row by row, column by column. So ou
35
35
36
36
#### Space Complexity
37
37
38
-
`O(N * M)` - simple implementation
39
-
`O(min {N, M})` - two-layers implementation (as `dp[i][j]` depends on only i-th and i-th layers, we coudld store only two layers).
38
+
`O(N * M)` - simple implementation
39
+
`O(min {N, M})` - two-layers implementation (as `dp[i][j]` depends on only i-th and i-th layers, we coudld store only two layers).
Copy file name to clipboardExpand all lines: en/Sorting Algorithms/Insertion Sort.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,19 +8,18 @@ Given an array of n elements, write a function to sort the array in increasing o
8
8
9
9
- Define a "key" index, the subarray to the left of which is sorted.
10
10
- Initiate "key" as 1, ie. the second element of array(as there is only one element to left of the second element, which can be considered as sorted array with one element).
11
-
12
11
- If value of element at (key - 1) position is less than value of element at (key) position; increment "key".
13
12
- Else move elements of sorted subarray that are greater than value of element at "key" to one position ahead of their current position. Put the value of element at "key" in the newly created void.
14
13
15
14
#### Time Complexity
16
15
17
-
О(n^2) comparisons, О(n^2) swaps -- Worst Case
16
+
-`О(n^2)` comparisons, `О(n^2)` swaps -- Worst Case
18
17
19
-
O(n) comparisons, O(1) swaps -- Best Case
18
+
-`O(n)` comparisons, `O(1)` swaps -- Best Case
20
19
21
20
#### Space Complexity
22
21
23
-
O(1) -- (No extra space needed, sorting done in place)
22
+
`O(1)` -- (No extra space needed, sorting done in place)
24
23
25
24
#### Example
26
25
@@ -47,7 +46,7 @@ i = 4.
47
46
6 will move to position after 5,
48
47
and elements from 11 to 13 will move one position ahead of their current position.
0 commit comments