Skip to content

Commit 0fe2033

Browse files
authored
feat: add solutions to lc problems: No.1227+ (#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

+79
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

+1-1
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

+10
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

+6
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

+10
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

+1-1
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

+4
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

+16
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

+12
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

+39
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**

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
7777

7878
根据题目我们可以知道,函数 $f(x, y)$ 是单调递增函数,因此,我们可以枚举 $x$,然后在 $[1,...z]$ 中二分查找 $y$,使得 $f(x, y) = z$。如果找到了,就将 $(x, y)$ 加入答案中。
7979

80-
时间复杂度 $(n \log n)$,空间复杂度 $O(1)$。
80+
时间复杂度 $(n \log n)$,其中 $n$ 是 $z$ 的值,空间复杂度 $O(1)$。
8181

8282
**方法二:双指针**
8383

@@ -89,7 +89,7 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
8989

9090
循环结束后,返回答案。
9191

92-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
92+
时间复杂度 $O(n)$,其中 $n$ 是 $z$ 的值,空间复杂度 $O(1)$。
9393

9494
<!-- tabs:start -->
9595

solution/1200-1299/1237.Find Positive Integer Solution for a Given Equation/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,23 @@ x=5, y=1 -&gt; f(5, 1) = 5 * 1 = 5.
6969

7070
## Solutions
7171

72-
Binary search.
72+
**Solution 1: Enumeration + Binary Search**
73+
74+
According to the problem, we know that the function $f(x, y)$ is a monotonically increasing function. Therefore, we can enumerate $x$, and then binary search $y$ in $[1,...z]$ to make $f(x, y) = z$. If found, add $(x, y)$ to the answer.
75+
76+
The time complexity is $O(n \log n)$, where $n$ is the value of $z$, and the space complexity is $O(1)$.
77+
78+
**Solution 2: Two Pointers**
79+
80+
We can define two pointers $x$ and $y$, initially $x = 1$, $y = z$.
81+
82+
- If $f(x, y) = z$, we add $(x, y)$ to the answer, then $x \leftarrow x + 1$, $y \leftarrow y - 1$;
83+
- If $f(x, y) \lt z$, at this time for any $y' \lt y$, we have $f(x, y') \lt f(x, y) \lt z$, so we cannot decrease $y$, we can only increase $x$, so $x \leftarrow x + 1$;
84+
- If $f(x, y) \gt z$, at this time for any $x' \gt x$, we have $f(x', y) \gt f(x, y) \gt z$, so we cannot increase $x$, we can only decrease $y$, so $y \leftarrow y - 1$.
85+
86+
After the loop ends, return the answer.
87+
88+
The time complexity is $O(n)$, where $n$ is the value of $z$, and the space complexity is $O(1)$.
7389

7490
<!-- tabs:start -->
7591

solution/1200-1299/1238.Circular Permutation in Binary Representation/README_EN.md

+28
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,34 @@ All the adjacent element differ by one bit. Another valid permutation is [3,1,0,
4040

4141
## Solutions
4242

43+
**Solution 1: Binary Code to Gray Code**
44+
45+
We observe the arrangement in the problem, and find that in its binary representation, only one bit is different between any two (including the first and last) adjacent numbers. This kind of coding method is Gray code, which is a coding method we will encounter in engineering.
46+
47+
The rule for converting binary code to binary Gray code is to keep the highest bit of the binary code as the highest bit of the Gray code, and the second highest bit of the Gray code is the XOR of the highest bit and the second highest bit of the binary code. The rest of the Gray code is similar to the second highest bit.
48+
49+
Assume a binary number is represented as $B_{n-1}B_{n-2}...B_2B_1B_0$, its Gray code representation is $G_{n-1}G_{n-2}...G_2G_1G_0$. The highest bit is kept, so $G_{n-1} = B_{n-1}$; and the other bits $G_i = B_{i+1} \oplus B_{i}$, where $i=0,1,2..,n-2$.
50+
51+
Therefore, for an integer $x$, we can use the function $gray(x)$ to get its Gray code:
52+
53+
```java
54+
int gray(x) {
55+
return x ^ (x >> 1);
56+
}
57+
```
58+
59+
We can directly convert the integers $[0,..2^n - 1]$ into the corresponding Gray code array, then find the position of $start$ in the Gray code array, cut the Gray code array from this position, and then append the cut part to the front of the Gray code array to get the arrangement required by the problem.
60+
61+
The time complexity is $O(2^n)$, and the space complexity is $O(2^n)$. Where $n$ is the integer given in the problem.
62+
63+
**Solution 2: Conversion Optimization**
64+
65+
Since $gray(0) = 0$, then $gray(0) \oplus start = start$, and $gray(i)$ is only one binary bit different from $gray(i-1)$, so $gray(i) \oplus start$ is also only one binary bit different from $gray(i-1) \oplus start$.
66+
67+
Therefore, we can also directly convert the integers $[0,..2^n - 1]$ into the corresponding $gray(i) \oplus start$ to get the Gray code arrangement with $start$ as the first term.
68+
69+
The time complexity is $O(2^n)$, where $n$ is the integer given in the problem. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
70+
4371
<!-- tabs:start -->
4472

4573
### **Python3**

solution/1200-1299/1239.Maximum Length of a Concatenated String with Unique Characters/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
**方法一:位运算 + 状态压缩**
6262

63-
状态压缩,用一个 32 位数记录字母的出现情况,`masks` 存储之前枚举的字符串。
63+
状态压缩,用一个 $32$ 位数记录字母的出现情况,`masks` 存储之前枚举的字符串。
6464

6565
时间复杂度 $O(2^n + L)$,空间复杂度 $O(2^n)$。其中 $n$ 和 $L$ 分别是字符串数组的长度和字符串数组中字符串的长度之和。
6666

solution/1200-1299/1239.Maximum Length of a Concatenated String with Unique Characters/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ Maximum length is 4.
5353

5454
## Solutions
5555

56-
State compression uses a 32-bit number to record the appearance of letters, and `masks` stores the previously enumerated strings.
56+
**Solution 1: Bit Manipulation + State Compression**
57+
58+
State compression is used, with a 32-bit number recording the occurrence of letters, and `masks` storing the strings enumerated before.
59+
60+
The time complexity is $O(2^n + L)$, and the space complexity is $O(2^n)$. Where $n$ and $L$ are the length of the string array and the sum of the lengths of the strings in the array, respectively.
5761

5862
<!-- tabs:start -->
5963

solution/1200-1299/1240.Tiling a Rectangle with the Fewest Squares/README_EN.md

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@
4545

4646
## Solutions
4747

48+
**Solution 1: Recursive Backtracking + State Compression**
49+
50+
We can perform recursive backtracking by position, during which we use a variable $t$ to record the current number of tiles used.
51+
52+
- If $j = m$, i.e., the $i$-th row has been completely filled, then we recurse to the next row, i.e., $(i + 1, 0)$.
53+
- If $i = n$, it means that all positions have been filled, we update the answer and return.
54+
- If the current position $(i, j)$ has been filled, then directly recurse to the next position $(i, j + 1)$.
55+
- Otherwise, we enumerate the maximum square side length $w$ that the current position $(i, j)$ can fill, and fill all positions from $(i, j)$ to $(i + w - 1, j + w - 1)$, then recurse to the next position $(i, j + w)$. When backtracking, we need to clear all positions from $(i, j)$ to $(i + w - 1, j + w - 1)$.
56+
57+
Since each position only has two states: filled or not filled, we can use an integer to represent the current state. We use an integer array $filled$ of length $n$, where $filled[i]$ represents the state of the $i$-th row. If the $j$-th bit of $filled[i]$ is $1$, it means that the $i$-th row and the $j$-th column have been filled, otherwise it means not filled.
58+
4859
<!-- tabs:start -->
4960

5061
### **Python3**

solution/1200-1299/1243.Array Transformation/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ No more operations can be done to this array.
4848

4949
## Solutions
5050

51+
**Solution 1: Simulation**
52+
53+
Simulate each day. For each element, if it is greater than its left and right neighbors, it decreases by 1, otherwise, it increases by 1. If the array no longer changes on a certain day, return that array.
54+
55+
The time complexity is $O(n \times m)$, and the space complexity is $O(n)$. Where $n$ is the length of the array, and $m$ is the maximum value in the array.
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**

0 commit comments

Comments
 (0)