Skip to content

Commit 0fe2033

Browse files
authored
feat: add solutions to lc problems: No.1227+ (doocs#2152)
1 parent 71205c7 commit 0fe2033

File tree

61 files changed

+810
-152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+810
-152
lines changed

solution/1200-1299/1227.Airplane Seat Assignment Probability/README_EN.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,85 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Mathematics**
42+
43+
Let $f(n)$ represent the probability that the $n$th passenger will sit in their own seat when there are $n$ passengers boarding. Consider from the simplest case:
44+
45+
- When $n=1$, there is only 1 passenger and 1 seat, so the first passenger can only sit in the first seat, $f(1)=1$;
46+
47+
- When $n=2$, there are 2 seats, each seat has a probability of 0.5 to be chosen by the first passenger. After the first passenger chooses a seat, the second passenger can only choose the remaining seat, so the second passenger has a probability of 0.5 to sit in their own seat, $f(2)=0.5$.
48+
49+
When $n>2$, how to calculate the value of $f(n)$? Consider the seat chosen by the first passenger, there are three cases.
50+
51+
- The first passenger has a probability of $\frac{1}{n}$ to choose the first seat, then all passengers can sit in their own seats, so the probability of the $n$th passenger sitting in their own seat is 1.0.
52+
53+
- The first passenger has a probability of $\frac{1}{n}$ to choose the $n$th seat, then the second to the $(n-1)$th passengers can sit in their own seats, the $n$th passenger can only sit in the first seat, so the probability of the $n$th passenger sitting in their own seat is 0.0.
54+
55+
- The first passenger has a probability of $\frac{n-2}{n}$ to choose the remaining seats, each seat has a probability of $\frac{1}{n}$ to be chosen.
56+
Suppose the first passenger chooses the $i$th seat, where $2 \le i \le n-1$, then the second to the $(i-1)$th passengers can sit in their own seats, the seats of the $i$th to the $n$th passengers are uncertain, the $i$th passenger will randomly choose from the remaining $n-(i-1)=n-i+1$ seats (including the first seat and the $(i+1)$th to the $n$th seats). Since the number of remaining passengers and seats is $n-i+1$, and 1 passenger will randomly choose a seat, the problem size is reduced from $n$ to $n-i+1$.
57+
58+
Combining the above three cases, we can get the recursive formula of $f(n)$:
59+
60+
$$
61+
\begin{aligned}
62+
f(n) &= \frac{1}{n} \times 1.0 + \frac{1}{n} \times 0.0 + \frac{1}{n} \times \sum_{i=2}^{n-1} f(n-i+1) \\
63+
&= \frac{1}{n}(1.0+\sum_{i=2}^{n-1} f(n-i+1))
64+
\end{aligned}
65+
$$
66+
67+
In the above recursive formula, there are $n-2$ values of $i$, since the number of values of $i$ must be a non-negative integer, so the above recursive formula only holds when $n-2 \ge 0$ i.e., $n \ge 2$.
68+
69+
If you directly use the above recursive formula to calculate the value of $f(n)$, the time complexity is $O(n^2)$, which cannot pass all test cases, so it needs to be optimized.
70+
71+
Replace $n$ with $n-1$ in the above recursive formula, you can get the recursive formula:
72+
73+
$$
74+
f(n-1) = \frac{1}{n-1}(1.0+\sum_{i=2}^{n-2} f(n-i))
75+
$$
76+
77+
In the above recursive formula, there are $n-3$ values of $i$, and the above recursive formula only holds when $n-3 \ge 0$ i.e., $n \ge 3$.
78+
79+
When $n \ge 3$, the above two recursive formulas can be written as follows:
80+
81+
$$
82+
\begin{aligned}
83+
n \times f(n) &= 1.0+\sum_{i=2}^{n-1} f(n-i+1) \\
84+
(n-1) \times f(n-1) &= 1.0+\sum_{i=2}^{n-2} f(n-i)
85+
\end{aligned}
86+
$$
87+
88+
Subtract the above two formulas:
89+
90+
$$
91+
\begin{aligned}
92+
&~~~~~ n \times f(n) - (n-1) \times f(n-1) \\
93+
&= (1.0+\sum_{i=2}^{n-1} f(n-i+1)) - (1.0+\sum_{i=2}^{n-2} f(n-i)) \\
94+
&= \sum_{i=2}^{n-1} f(n-i+1) - \sum_{i=2}^{n-2} f(n-i) \\
95+
&= f(2)+f(3)+...+f(n-1) - (f(2)+f(3)+...+f(n-2)) \\
96+
&= f(n-1)
97+
\end{aligned}
98+
$$
99+
100+
After simplification, we get the simplified recursive formula:
101+
102+
$$
103+
\begin{aligned}
104+
n \times f(n) &= n \times f(n-1) \\
105+
f(n) &= f(n-1)
106+
\end{aligned}
107+
$$
108+
109+
Since we know that $f(1)=1$ and $f(2)=0.5$, therefore when $n \ge 3$, according to $f(n) = f(n-1)$, we know that for any positive integer $n$, $f(n)=0.5$. And since $f(2)=0.5$, therefore when $n \ge 2$, for any positive integer $n$, $f(n)=0.5$.
110+
111+
From this, we can get the result of $f(n)$:
112+
113+
$$
114+
f(n) = \begin{cases}
115+
1.0, & n = 1 \\
116+
0.5, & n \ge 2
117+
\end{cases}
118+
$$
119+
41120
<!-- tabs:start -->
42121

43122
### **Python3**

solution/1200-1299/1228.Missing Number In Arithmetic Progression/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
因此,缺失的数为 $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$。
5353

54-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度
54+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
5555

5656
<!-- tabs:start -->
5757

solution/1200-1299/1228.Missing Number In Arithmetic Progression/README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737

3838
## Solutions
3939

40+
**Solution 1: Arithmetic Series Sum Formula**
41+
42+
The sum formula for an arithmetic series is $\frac{n(a_1 + a_n)}{2}$, where $n$ is the number of terms in the arithmetic series, $a_1$ is the first term of the arithmetic series, and $a_n$ is the last term of the arithmetic series.
43+
44+
Since the array given in the problem is an arithmetic series and is missing a number, the number of terms in the array is $n + 1$, the first term is $a_1$, and the last term is $a_n$, so the sum of the array is $\frac{n + 1}{2}(a_1 + a_n)$.
45+
46+
Therefore, the missing number is $\frac{n + 1}{2}(a_1 + a_n) - \sum_{i = 0}^n a_i$.
47+
48+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array.
49+
4050
<!-- tabs:start -->
4151

4252
### **Python3**

solution/1200-1299/1229.Meeting Scheduler/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141

4242
## Solutions
4343

44+
**Solution 1: Sorting + Two Pointers**
45+
46+
We can sort the free time of the two people separately, then use two pointers to traverse the two arrays, find the intersection of the free time periods of the two people, and if the length of the intersection is greater than or equal to `duration`, then return the start time of the intersection and the start time plus `duration`.
47+
48+
The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(\log m + \log n)$. Where $m$ and $n$ are the lengths of the two arrays respectively.
49+
4450
<!-- tabs:start -->
4551

4652
### **Python3**

solution/1200-1299/1231.Divide Chocolate/README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Binary Search + Greedy**
51+
52+
We notice that if we can eat a piece of chocolate with sweetness $x$, then we can also eat all chocolates with sweetness less than or equal to $x$. This shows monotonicity, therefore, we can use binary search to find the maximum $x$ that satisfies the condition.
53+
54+
We define the left boundary of the binary search as $l=0$, and the right boundary as $r=\sum_{i=0}^{n-1} sweetness[i]$. Each time, we take the middle value $mid$ of $l$ and $r$, and then determine whether we can eat a piece of chocolate with sweetness $mid$. If we can, then we try to eat a piece of chocolate with greater sweetness, i.e., let $l=mid$; otherwise, we try to eat a piece of chocolate with smaller sweetness, i.e., let $r=mid-1$. After the binary search ends, we return $l$.
55+
56+
The key to the problem is how to determine whether we can eat a piece of chocolate with sweetness $x$. We can use a greedy approach, traverse the array from left to right, accumulate the current sweetness each time, when the accumulated sweetness is greater than or equal to $x$, the chocolate count $cnt$ is increased by $1$, and the accumulated sweetness is reset to zero. Finally, check whether $cnt$ is greater than $k$.
57+
58+
The time complexity is $O(n \times \log \sum_{i=0}^{n-1} sweetness[i])$, and the space complexity is $O(1)$. Where $n$ is the length of the array.
59+
5060
<!-- tabs:start -->
5161

5262
### **Python3**

solution/1200-1299/1232.Check If It Is a Straight Line/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
**方法一:数学**
4747

48-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 表示 `coordinates` 数组的长度。
48+
时间复杂度 $O(n)$,其中 $n$ 表示 `coordinates` 数组的长度。空间复杂度 $O(1)$
4949

5050
<!-- tabs:start -->
5151

solution/1200-1299/1232.Check If It Is a Straight Line/README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
## Solutions
4141

42+
**Solution 1: Mathematics**
43+
44+
The time complexity is $O(n)$, where $n$ is the length of the `coordinates` array. The space complexity is $O(1)$.
45+
4246
<!-- tabs:start -->
4347

4448
### **Python3**

solution/1200-1299/1233.Remove Sub-Folders from the Filesystem/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@
5151

5252
## Solutions
5353

54+
**Solution 1: Sorting**
55+
56+
First, we sort the array `folder` in lexicographical order, then traverse the array. For the current folder $f$ we are traversing, if its length is greater than or equal to the length of the last folder in the answer array, and its prefix includes the last folder in the answer array plus a `/`, then $f$ is a subfolder of the last folder in the answer array, and we don't need to add it to the answer array. Otherwise, we add $f$ to the answer array.
57+
58+
After the traversal ends, the folders in the answer array are the answer required by the problem.
59+
60+
The time complexity is $O(n \times \log n \times m)$, and the space complexity is $O(m)$. Where $n$ and $m$ are the length of the array `folder` and the maximum length of the strings in the array `folder`, respectively.
61+
62+
**Solution 2: Trie**
63+
64+
We can use a trie to store all the folders in the array `folder`. Each node of the trie contains a `children` field, used to store the child nodes of the current node, and a `fid` field, used to store the index of the folder corresponding to the current node in the array `folder`.
65+
66+
For each folder $f$ in the array `folder`, we first split $f$ into several substrings according to `/`, then start from the root node and add the substrings to the trie in turn. Next, we start from the root node to search the trie. If the `fid` field of the current node is not `-1`, it means that the folder corresponding to the current node is a folder in the answer array. We add it to the answer array and return. Otherwise, we recursively search all child nodes of the current node and finally return the answer array.
67+
68+
The time complexity is $O(n \times m)$, and the space complexity is $O(n \times m)$. Where $n$ and $m$ are the length of the array `folder` and the maximum length of the strings in the array `folder`, respectively.
69+
5470
<!-- tabs:start -->
5571

5672
### **Python3**

solution/1200-1299/1234.Replace the Substring for Balanced String/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747

4848
## Solutions
4949

50+
**Solution 1: Counting + Two Pointers**
51+
52+
First, we use a hash table or array `cnt` to count the number of each character in string $s$. If the count of all characters does not exceed $n/4$, then the string $s$ is balanced, and we directly return $0$.
53+
54+
Otherwise, we use two pointers $j$ and $i$ to maintain the left and right boundaries of the window, initially $j = 0$.
55+
56+
Next, we traverse the string $s$ from left to right. Each time we encounter a character, we decrease its count by $1$, then we check whether the current window meets the condition, that is, the count of characters outside the window does not exceed $n/4$. If the condition is met, we update the answer, then move the left boundary of the window to the right until the condition is not met.
57+
58+
Finally, we return the answer.
59+
60+
The time complexity is $O(n)$, and the space complexity is $O(C)$. Where $n$ is the length of the string $s$; and $C$ is the size of the character set, in this problem $C = 4$.
61+
5062
<!-- tabs:start -->
5163

5264
### **Python3**

solution/1200-1299/1235.Maximum Profit in Job Scheduling/README_EN.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,45 @@ Profit obtained 150 = 20 + 70 + 60.
5353

5454
## Solutions
5555

56+
**Solution 1: Memoization Search + Binary Search**
57+
58+
First, we sort the jobs by start time in ascending order, then design a function $dfs(i)$ to represent the maximum profit that can be obtained starting from the $i$-th job. The answer is $dfs(0)$.
59+
60+
The calculation process of function $dfs(i)$ is as follows:
61+
62+
For the $i$-th job, we can choose to do it or not. If we don't do it, the maximum profit is $dfs(i + 1)$; if we do it, we can use binary search to find the first job that starts after the end time of the $i$-th job, denoted as $j$, then the maximum profit is $profit[i] + dfs(j)$. We take the larger of the two. That is:
63+
64+
$$
65+
dfs(i)=\max(dfs(i+1),profit[i]+dfs(j))
66+
$$
67+
68+
Where $j$ is the smallest index that satisfies $startTime[j] \ge endTime[i]$.
69+
70+
In this process, we can use memoization search to save the answer of each state to avoid repeated calculations.
71+
72+
The time complexity is $O(n \times \log n)$, where $n$ is the number of jobs.
73+
74+
**Solution 2: Dynamic Programming + Binary Search**
75+
76+
We can also change the memoization search in Solution 1 to dynamic programming.
77+
78+
First, sort the jobs, this time we sort by end time in ascending order, then define $dp[i]$, which represents the maximum profit that can be obtained from the first $i$ jobs. The answer is $dp[n]$. Initialize $dp[0]=0$.
79+
80+
For the $i$-th job, we can choose to do it or not. If we don't do it, the maximum profit is $dp[i]$; if we do it, we can use binary search to find the last job that ends before the start time of the $i$-th job, denoted as $j$, then the maximum profit is $profit[i] + dp[j]$. We take the larger of the two. That is:
81+
82+
$$
83+
dp[i+1] = \max(dp[i], profit[i] + dp[j])
84+
$$
85+
86+
Where $j$ is the largest index that satisfies $endTime[j] \leq startTime[i]$.
87+
88+
The time complexity is $O(n \times \log n)$, where $n$ is the number of jobs.
89+
90+
Similar problems:
91+
92+
- [2008. Maximum Earnings From Taxi](/solution/2000-2099/2008.Maximum%20Earnings%20From%20Taxi/README.md)
93+
- [1751. Maximum Number of Events That Can Be Attended II](/solution/1700-1799/1751.Maximum%20Number%20of%20Events%20That%20Can%20Be%20Attended%20II/README.md)
94+
5695
<!-- tabs:start -->
5796

5897
### **Python3**

0 commit comments

Comments
 (0)