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: solution/0800-0899/0840.Magic Squares In Grid/README_EN.md
+4-2
Original file line number
Diff line number
Diff line change
@@ -19,9 +19,11 @@ tags:
19
19
20
20
<!-- description:start -->
21
21
22
-
<p>A <code>3 x 3</code> magic square is a <code>3 x 3</code> grid filled with distinct numbers <strong>from </strong><code>1</code><strong> to </strong><code>9</code> such that each row, column, and both diagonals all have the same sum.</p>
22
+
<p>A <code>3 x 3</code> <strong>magic square</strong> is a <code>3 x 3</code> grid filled with distinct numbers <strong>from </strong>1<strong> to </strong>9 such that each row, column, and both diagonals all have the same sum.</p>
23
23
24
-
<p>Given a <code>row x col</code> <code>grid</code> of integers, how many <code>3 x 3</code> "magic square" subgrids are there? (Each subgrid is contiguous).</p>
24
+
<p>Given a <code>row x col</code> <code>grid</code> of integers, how many <code>3 x 3</code> contiguous magic square subgrids are there?</p>
25
+
26
+
<p>Note: while a magic square can only contain numbers from 1 to 9, <code>grid</code> may contain numbers up to 15.</p>
Copy file name to clipboardexpand all lines: solution/1400-1499/1444.Number of Ways of Cutting a Pizza/README_EN.md
+24-1
Original file line number
Diff line number
Diff line change
@@ -69,7 +69,30 @@ tags:
69
69
70
70
<!-- solution:start -->
71
71
72
-
### Solution 1
72
+
### Solution 1: 2D Prefix Sum + Memoized Search
73
+
74
+
We can use a 2D prefix sum to quickly calculate the number of apples in each sub-rectangle. Define $s[i][j]$ to represent the number of apples in the sub-rectangle that includes the first $i$ rows and the first $j$ columns. Then $s[i][j]$ can be derived from the number of apples in the three sub-rectangles $s[i-1][j]$, $s[i][j-1]$, and $s[i-1][j-1]$. The specific calculation method is as follows:
Here, $pizza[i-1][j-1]$ represents the character at the $i$-th row and $j$-th column in the rectangle. If it is an apple, it is $1$; otherwise, it is $0$.
81
+
82
+
Next, we design a function $dfs(i, j, k)$, which represents the number of ways to cut the rectangle $(i, j, m-1, n-1)$ with $k$ cuts to get $k+1$ pieces of pizza. Here, $(i, j)$ and $(m-1, n-1)$ are the coordinates of the top-left and bottom-right corners of the rectangle, respectively. The calculation method of the function $dfs(i, j, k)$ is as follows:
83
+
84
+
- If $k = 0$, it means no more cuts can be made. We need to check if there are any apples in the rectangle. If there are apples, return $1$; otherwise, return $0$.
85
+
- If $k \gt 0$, we need to enumerate the position of the last cut. If the last cut is horizontal, we need to enumerate the cutting position $x$, where $i \lt x \lt m$. If $s[x][n] - s[i][n] - s[x][j] + s[i][j] \gt 0$, it means there are apples in the upper piece of pizza, and we add the value of $dfs(x, j, k-1)$ to the answer. If the last cut is vertical, we need to enumerate the cutting position $y$, where $j \lt y \lt n$. If $s[m][y] - s[i][y] - s[m][j] + s[i][j] \gt 0$, it means there are apples in the left piece of pizza, and we add the value of $dfs(i, y, k-1)$ to the answer.
86
+
87
+
The final answer is the value of $dfs(0, 0, k-1)$.
88
+
89
+
To avoid repeated calculations, we can use memoized search. We use a 3D array $f$ to record the value of $dfs(i, j, k)$. When we need to calculate the value of $dfs(i, j, k)$, if $f[i][j][k]$ is not $-1$, it means we have already calculated it before, and we can directly return $f[i][j][k]$. Otherwise, we calculate the value of $dfs(i, j, k)$ according to the above method and save the result in $f[i][j][k]$.
90
+
91
+
The time complexity is $O(m \times n \times k \times (m + n))$, and the space complexity is $O(m \times n \times k)$. Here, $m$ and $n$ are the number of rows and columns of the rectangle, respectively.
92
+
93
+
Similar problems:
94
+
95
+
-[2312. Selling Pieces of Wood](https://github.com/doocs/leetcode/blob/main/solution/2300-2399/2312.Selling%20Pieces%20of%20Wood/README_EN.md)
Copy file name to clipboardexpand all lines: solution/1400-1499/1446.Consecutive Characters/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,15 @@ tags:
53
53
54
54
<!-- solution:start -->
55
55
56
-
### Solution 1
56
+
### Solution 1: Traversal and Counting
57
+
58
+
We define a variable $\textit{t}$ to represent the length of the current consecutive characters, initially $\textit{t}=1$.
59
+
60
+
Next, we traverse the string $s$ starting from the second character. If the current character is the same as the previous character, then $\textit{t} = \textit{t} + 1$, and update the answer $\textit{ans} = \max(\textit{ans}, \textit{t})$; otherwise, set $\textit{t} = 1$.
61
+
62
+
Finally, return the answer $\textit{ans}$.
63
+
64
+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
0 commit comments