Skip to content

Commit 7ce4daf

Browse files
committed
update DP Min Deletions to Make a Sequence Sorted
1 parent cea0f58 commit 7ce4daf

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

✅ Pattern 15: 0-1 Knapsack (Dynamic Programming).md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5062,7 +5062,7 @@ console.log(
50625062
## Shortest Common Super-sequence
50635063
https://leetcode.com/problems/shortest-common-supersequence/
50645064
5065-
> Given two </b>sequences `s1` and `s2`, write a method to find the length of the shortest sequence which has `s1` and `s2` as <b>subsequences</b>.
5065+
> Given two <b>sequences</b> `s1` and `s2`, write a method to find the length of the shortest sequence which has `s1` and `s2` as <b>subsequences</b>.
50665066
50675067
#### Example 1:
50685068
```js
@@ -5110,9 +5110,6 @@ function findSCSLength(s1, s2) {
51105110
return Math.min(length1, length2)
51115111
}
51125112

5113-
5114-
5115-
51165113
return findSCSLengthRecursive(s1, s2, 0, 0) ;
51175114
};
51185115

@@ -5196,12 +5193,12 @@ console.log(
51965193
### Bottom-up Dynamic Programming
51975194
Since we want to match all the <b>subsequences</b> of the given <b>sequences</b>, we can use a two-dimensional array to store our results. The lengths of the two strings will define the size of the array’s dimensions. So for every index `i` in sequence `s1` and `j` in sequence `s2`, we will choose one of the following two options:
51985195
5199-
1. If the character `s1[i]` matches `s2[j]`, the length of the <b>SCS<b> would be the one plus the length of the <b>SCS<b> until `i-1` and `j-1` indexes in the two strings.
5200-
2. If the character `s1[i]` does not match `s2[j]`, we will consider two <b>SCS<b>:
5196+
1. If the character `s1[i]` matches `s2[j]`, the length of the <b>SCS</b> would be the one plus the length of the <b>SCS</b> until `i-1` and `j-1` indexes in the two strings.
5197+
2. If the character `s1[i]` does not match `s2[j]`, we will consider two <b>SCS</b>:
52015198
- one without `s1[i]` and one without `s2[j]`.
5202-
- Our required <b>SCS<b> length will be the shortest of these two super-sequences plus one.
5199+
- Our required <b>SCS</b> length will be the shortest of these two super-sequences plus one.
52035200
5204-
So our <b>recursive formula<b> would be:
5201+
So our <b>recursive formula</b> would be:
52055202
52065203
```js
52075204
if s1[i] == s2[j]
@@ -5264,6 +5261,27 @@ console.log(
52645261
## Minimum Deletions to Make a Sequence Sorted
52655262
https://www.geeksforgeeks.org/minimum-number-deletions-make-sorted-sequence/
52665263
5264+
> Given a number <b>sequence</b>, find the minimum number of elements that should be deleted to make the remaining <b>sequence</b> sorted.
5265+
5266+
#### Example 1:
5267+
```js
5268+
Input: {4,2,3,6,10,1,12}
5269+
Output: 2
5270+
Explanation: We need to delete {4,1} to make the remaing sequence sorted {2,3,6,10,12}.
5271+
```
5272+
#### Example 2:
5273+
```js
5274+
Input: {-4,10,3,7,15}
5275+
Output: 1
5276+
Explanation: We need to delete {10} to make the remaing sequence sorted {-4,3,7,15}.
5277+
```
5278+
#### Example 3:
5279+
```js
5280+
Input: {3,2,1,0}
5281+
Output: 3
5282+
Explanation: Since the elements are in reverse order, we have to delete all except one to get a
5283+
sorted sequence. Sorted sequences are {3}, {2}, {1}, and {0}
5284+
```
52675285
## Longest Repeating Subsequence
52685286
https://www.geeksforgeeks.org/longest-repeating-subsequence/
52695287

0 commit comments

Comments
 (0)