Skip to content

Commit acf1db0

Browse files
authored
feat: add solutions to lc problems: No.2907,2921 (doocs#1920)
* No.2907.Maximum Profitable Triplets With Increasing Prices I * No.2921.Maximum Profitable Triplets With Increasing Prices II
1 parent 07d5320 commit acf1db0

File tree

12 files changed

+2433
-7
lines changed

12 files changed

+2433
-7
lines changed

solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959

6060
因此,对于当前节点,如果其左子节点不为空,我们找到左子树的最右节点,作为前驱节点,然后将当前节点的右子节点赋给前驱节点的右子节点。然后将当前节点的左子节点赋给当前节点的右子节点,并将当前节点的左子节点置为空。然后将当前节点的右子节点作为下一个节点,继续处理,直至所有节点处理完毕。
6161

62-
时间复杂度 $O(n)$,空间复杂度 O(1)$。其中 $n$ 是树中节点的个数。
62+
时间复杂度 $O(n)$,其中 $n$ 是树中节点的个数。空间复杂度 O(1)$
6363

6464
<!-- tabs:start -->
6565

solution/0100-0199/0115.Distinct Subsequences/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ As shown below, there are 5 ways you can generate &quot;bag&quot; from s.
4444

4545
## Solutions
4646

47+
**Solution 1: Dynamic Programming**
48+
49+
We define $f[i][j]$ as the number of schemes where the first $i$ characters of string $s$ form the first $j$ characters of string $t$. Initially, $f[i][0]=1$ for all $i \in [0,m]$.
50+
51+
When $i > 0$, we consider the calculation of $f[i][j]$:
52+
53+
- When $s[i-1] \ne t[j-1]$, we cannot select $s[i-1]$, so $f[i][j]=f[i-1][j]$;
54+
- Otherwise, we can select $s[i-1]$, so $f[i][j]=f[i-1][j-1]$.
55+
56+
Therefore, we have the following state transition equation:
57+
58+
$$
59+
f[i][j]=\left\{
60+
\begin{aligned}
61+
&f[i-1][j], &s[i-1] \ne t[j-1] \\
62+
&f[i-1][j-1]+f[i-1][j], &s[i-1]=t[j-1]
63+
\end{aligned}
64+
\right.
65+
$$
66+
67+
The final answer is $f[m][n]$, where $m$ and $n$ are the lengths of strings $s$ and $t$ respectively.
68+
69+
The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$.
70+
71+
We notice that the calculation of $f[i][j]$ is only related to $f[i-1][..]$. Therefore, we can optimize the first dimension, reducing the space complexity to $O(n)$.
72+
4773
<!-- tabs:start -->
4874

4975
### **Python3**

0 commit comments

Comments
 (0)