From f4bcddb7434e797dae9d8151745564e9adf177d0 Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 20 Feb 2025 03:33:18 +0300 Subject: [PATCH 1/3] feat: add solutions to lc problem: No.1415 (#4082) --- .../README.md | 48 +++++++++++++++++++ .../README_EN.md | 48 +++++++++++++++++++ .../Solution.js | 19 ++++++++ .../Solution.ts | 19 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js create mode 100644 solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md index 4c5f66ab3b634..254a012c014fd 100644 --- a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README.md @@ -159,6 +159,54 @@ public: }; ``` +#### TypeScript + +```ts +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + +#### JavaScript + +```js +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md index 058a45cf2e8bf..f1c0ddf2c09dd 100644 --- a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/README_EN.md @@ -146,6 +146,54 @@ public: }; ``` +#### TypeScript + +```ts +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + +#### JavaScript + +```js +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} +``` + diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js new file mode 100644 index 0000000000000..e349343e318ff --- /dev/null +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.js @@ -0,0 +1,19 @@ +function getHappyString(n, k) { + const ans = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts new file mode 100644 index 0000000000000..8f421ccaf92f4 --- /dev/null +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.ts @@ -0,0 +1,19 @@ +function getHappyString(n: number, k: number): string { + const ans: string[] = []; + + const dfs = (s = '') => { + if (s.length === n) { + ans.push(s); + return; + } + + for (const ch of 'abc') { + if (s.at(-1) === ch) continue; + dfs(s + ch); + } + }; + + dfs(); + + return ans[k - 1] ?? ''; +} From 0849f4fdfea6d48e164cb3fefc131abf887bdc42 Mon Sep 17 00:00:00 2001 From: fxrc Date: Wed, 19 Feb 2025 16:33:39 -0800 Subject: [PATCH 2/3] feat: add python3 solution to lc problem: No.0772 (#4083) --- .../0772.Basic Calculator III/README.md | 27 ++++++++++++++++++- .../0772.Basic Calculator III/README_EN.md | 27 ++++++++++++++++++- .../0772.Basic Calculator III/Solution.py | 26 ++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 solution/0700-0799/0772.Basic Calculator III/Solution.py diff --git a/solution/0700-0799/0772.Basic Calculator III/README.md b/solution/0700-0799/0772.Basic Calculator III/README.md index 3a7dd9aabbe76..4606d3882c084 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README.md +++ b/solution/0700-0799/0772.Basic Calculator III/README.md @@ -73,7 +73,32 @@ tags: #### Python3 ```python - +class Solution: + def calculate(self, s: str) -> int: + def dfs(q): + num, sign, stk = 0, "+", [] + while q: + c = q.popleft() + if c.isdigit(): + num = num * 10 + int(c) + if c == "(": + num = dfs(q) + if c in "+-*/)" or not q: + match sign: + case "+": + stk.append(num) + case "-": + stk.append(-num) + case "*": + stk.append(stk.pop() * num) + case "/": + stk.append(int(stk.pop() / num)) + num, sign = 0, c + if c == ")": + break + return sum(stk) + + return dfs(deque(s)) ``` #### Java diff --git a/solution/0700-0799/0772.Basic Calculator III/README_EN.md b/solution/0700-0799/0772.Basic Calculator III/README_EN.md index 82619b07a78c1..ea63a44f76157 100644 --- a/solution/0700-0799/0772.Basic Calculator III/README_EN.md +++ b/solution/0700-0799/0772.Basic Calculator III/README_EN.md @@ -71,7 +71,32 @@ tags: #### Python3 ```python - +class Solution: + def calculate(self, s: str) -> int: + def dfs(q): + num, sign, stk = 0, "+", [] + while q: + c = q.popleft() + if c.isdigit(): + num = num * 10 + int(c) + if c == "(": + num = dfs(q) + if c in "+-*/)" or not q: + match sign: + case "+": + stk.append(num) + case "-": + stk.append(-num) + case "*": + stk.append(stk.pop() * num) + case "/": + stk.append(int(stk.pop() / num)) + num, sign = 0, c + if c == ")": + break + return sum(stk) + + return dfs(deque(s)) ``` #### Java diff --git a/solution/0700-0799/0772.Basic Calculator III/Solution.py b/solution/0700-0799/0772.Basic Calculator III/Solution.py new file mode 100644 index 0000000000000..d2a854468a4e7 --- /dev/null +++ b/solution/0700-0799/0772.Basic Calculator III/Solution.py @@ -0,0 +1,26 @@ +class Solution: + def calculate(self, s: str) -> int: + def dfs(q): + num, sign, stk = 0, "+", [] + while q: + c = q.popleft() + if c.isdigit(): + num = num * 10 + int(c) + if c == "(": + num = dfs(q) + if c in "+-*/)" or not q: + match sign: + case "+": + stk.append(num) + case "-": + stk.append(-num) + case "*": + stk.append(stk.pop() * num) + case "/": + stk.append(int(stk.pop() / num)) + num, sign = 0, c + if c == ")": + break + return sum(stk) + + return dfs(deque(s)) \ No newline at end of file From 808fe995a8999b70dcd3dae9de5ff0312f788721 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 20 Feb 2025 08:33:48 +0800 Subject: [PATCH 3/3] feat: update solutions to lc problems: No.2595,2596 (#4084) --- .../2300-2399/2330.Valid Palindrome IV/README.md | 4 ++-- .../2330.Valid Palindrome IV/README_EN.md | 6 +++++- .../README_EN.md | 13 ++++++++++++- .../README_EN.md | 8 +++++++- .../README.md | 4 ++-- .../README_EN.md | 6 +++++- .../README.md | 4 ++-- .../README_EN.md | 8 +++++++- .../2595.Number of Even and Odd Bits/README.md | 16 ++++++++++------ .../README_EN.md | 8 ++++++-- .../2595.Number of Even and Odd Bits/Solution.ts | 2 +- .../README.md | 4 ++-- .../README_EN.md | 6 +++--- 13 files changed, 64 insertions(+), 25 deletions(-) diff --git a/solution/2300-2399/2330.Valid Palindrome IV/README.md b/solution/2300-2399/2330.Valid Palindrome IV/README.md index 0af8b1d403fde..4529628c3a1fc 100644 --- a/solution/2300-2399/2330.Valid Palindrome IV/README.md +++ b/solution/2300-2399/2330.Valid Palindrome IV/README.md @@ -41,7 +41,7 @@ tags: 解释: 能让 s 变成回文,且只用了两步操作的方案如下: - 将 s[0] 变成 'b' ,得到 s = "ba" 。 - 将 s[1] 变成 'b' ,得到s = "bb" 。 -执行两步操作让 s 变成一个回文,所以返回 true 。 +执行两步操作让 s 变成一个回文,所以返回 true 。

示例 3:

@@ -69,7 +69,7 @@ tags: ### 方法一:双指针 -我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 `false`,否则返回 `true`。 +我们可以使用双指针 $i$ 和 $j$,分别指向字符串的头尾,然后向中间移动,统计不同字符的个数,如果不同字符的个数大于 $2$,则返回 $\textit{false}$,否则返回 $\textit{true}$。 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。 diff --git a/solution/2300-2399/2330.Valid Palindrome IV/README_EN.md b/solution/2300-2399/2330.Valid Palindrome IV/README_EN.md index 1bf906064d8c1..893fb371eb668 100644 --- a/solution/2300-2399/2330.Valid Palindrome IV/README_EN.md +++ b/solution/2300-2399/2330.Valid Palindrome IV/README_EN.md @@ -65,7 +65,11 @@ Two operations could be performed to make s a palindrome so return true. -### Solution 1 +### Solution 1: Two Pointers + +We can use two pointers $i$ and $j$, pointing to the beginning and end of the string, respectively, and then move towards the center, counting the number of different characters. If the number of different characters is greater than $2$, return $\textit{false}$; otherwise, return $\textit{true}$. + +The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$. diff --git a/solution/2300-2399/2378.Choose Edges to Maximize Score in a Tree/README_EN.md b/solution/2300-2399/2378.Choose Edges to Maximize Score in a Tree/README_EN.md index 8d3a1943303c3..9480c13c214d7 100644 --- a/solution/2300-2399/2378.Choose Edges to Maximize Score in a Tree/README_EN.md +++ b/solution/2300-2399/2378.Choose Edges to Maximize Score in a Tree/README_EN.md @@ -77,7 +77,18 @@ Note that we cannot choose more than one edge because all edges are adjacent to -### Solution 1 +### Solution 1: Tree DP + +We design a function $dfs(i)$, which represents the maximum sum of the weights of selected edges in the subtree rooted at node $i$, such that no two selected edges are adjacent. This function returns two values $(a, b)$. The first value $a$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is selected. The second value $b$ represents the sum of the weights of selected edges when the edge between the current node $i$ and its parent node is not selected. + +We can observe the following for the current node $i$: + +- If the edge between $i$ and its parent node is selected, then none of the edges between $i$ and its child nodes can be selected. In this case, the value of $a$ for the current node is the sum of the $b$ values of all its child nodes. +- If the edge between $i$ and its parent node is not selected, then we can select at most one edge between $i$ and its child nodes. In this case, the value of $b$ for the current node is the sum of the $a$ values of the selected child nodes and the $b$ values of the unselected child nodes, plus the weight of the edge between $i$ and the selected child node. + +We call the function $dfs(0)$, and the second value returned is the answer, which is the sum of the weights of selected edges when the edge between the root node and its parent node is not selected. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes. diff --git a/solution/2500-2599/2510.Check if There is a Path With Equal Number of 0's And 1's/README_EN.md b/solution/2500-2599/2510.Check if There is a Path With Equal Number of 0's And 1's/README_EN.md index 1742ea5e83382..d8e14ffac7999 100644 --- a/solution/2500-2599/2510.Check if There is a Path With Equal Number of 0's And 1's/README_EN.md +++ b/solution/2500-2599/2510.Check if There is a Path With Equal Number of 0's And 1's/README_EN.md @@ -55,7 +55,13 @@ tags: -### Solution 1 +### Solution 1: Memoization Search + +According to the problem description, we know that the number of 0s and 1s on the path from the top-left corner to the bottom-right corner is equal, and the total number is $m + n - 1$, which means the number of 0s and 1s are both $(m + n - 1) / 2$. + +Therefore, we can use memoization search, starting from the top-left corner and moving right or down until reaching the bottom-right corner, to check if the number of 0s and 1s on the path is equal. + +The time complexity is $O(m \times n \times (m + n))$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively. diff --git a/solution/2500-2599/2523.Closest Prime Numbers in Range/README.md b/solution/2500-2599/2523.Closest Prime Numbers in Range/README.md index 8be5a8bce6aa5..13e6f99de4cf2 100644 --- a/solution/2500-2599/2523.Closest Prime Numbers in Range/README.md +++ b/solution/2500-2599/2523.Closest Prime Numbers in Range/README.md @@ -65,9 +65,9 @@ tags: ### 方法一:线性筛 -对于给定的范围 $[left, right]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。 +对于给定的范围 $[\textit{left}, \textit{right}]$,我们可以使用线性筛求出所有质数,然后从小到大遍历质数,找到相邻的两个质数,其差值最小的质数对即为答案。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = right$。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n = \textit{right}$。 diff --git a/solution/2500-2599/2523.Closest Prime Numbers in Range/README_EN.md b/solution/2500-2599/2523.Closest Prime Numbers in Range/README_EN.md index a5504e234b075..806182d3a0ed2 100644 --- a/solution/2500-2599/2523.Closest Prime Numbers in Range/README_EN.md +++ b/solution/2500-2599/2523.Closest Prime Numbers in Range/README_EN.md @@ -70,7 +70,11 @@ Since 11 is smaller than 17, we return the first pair. -### Solution 1 +### Solution 1: Linear Sieve + +For the given range $[\textit{left}, \textit{right}]$, we can use the linear sieve method to find all prime numbers. Then, we traverse the prime numbers in ascending order to find the pair of adjacent prime numbers with the smallest difference, which will be the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n = \textit{right}$. diff --git a/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README.md b/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README.md index cdc97bf308dff..963e5c93365fa 100644 --- a/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README.md +++ b/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README.md @@ -65,11 +65,11 @@ tags: ### 方法一:哈希表 + 滑动窗口 + 快速幂 -我们用哈希表 `cnt` 维护窗口大小为 $k$ 的元素及其出现的次数。 +我们用哈希表 $\textit{cnt}$ 维护窗口大小为 $k$ 的元素及其出现的次数。 先算出初始窗口为 $k$ 的所有元素的分数。然后利用滑动窗口,每次加入一个元素,并移除最左边的元素,同时利用快速幂更新分数。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `nums` 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。 diff --git a/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README_EN.md b/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README_EN.md index 0310abfee45d6..e5c63120ca5e2 100644 --- a/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README_EN.md +++ b/solution/2500-2599/2524.Maximum Frequency Score of a Subarray/README_EN.md @@ -63,7 +63,13 @@ tags: -### Solution 1 +### Solution 1: Hash Table + Sliding Window + Fast Power + +We use a hash table $\textit{cnt}$ to maintain the elements of the window of size $k$ and their frequencies. + +First, calculate the score of all elements in the initial window of size $k$. Then, use a sliding window to add one element at a time and remove the leftmost element, while updating the score using fast power. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$. diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/README.md b/solution/2500-2599/2595.Number of Even and Odd Bits/README.md index 0edd41cc945d3..eade6dc3ce73a 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/README.md +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/README.md @@ -32,8 +32,8 @@ tags:
输入:n = 17
 输出:[2,0]
-解释:17 的二进制形式是 10001 。 
-下标 0 和 下标 4 对应的值为 1 。 
+解释:17 的二进制形式是 10001 。
+下标 0 和 下标 4 对应的值为 1 。
 共有 2 个偶数下标,0 个奇数下标。
 
@@ -41,8 +41,8 @@ tags:
输入:n = 2
 输出:[0,1]
-解释:2 的二进制形式是 10 。 
-下标 1 对应的值为 1 。 
+解释:2 的二进制形式是 10 。
+下标 1 对应的值为 1 。
 共有 0 个偶数下标,1 个奇数下标。
 
@@ -127,7 +127,7 @@ func evenOddBit(n int) []int { ```ts function evenOddBit(n: number): number[] { - const ans = new Array(2).fill(0); + const ans = Array(2).fill(0); for (let i = 0; n > 0; n >>= 1, i ^= 1) { ans[i] += n & 1; } @@ -161,7 +161,11 @@ impl Solution { -### 方法二 +### 方法二:位运算 + +我们可以定义一个掩码 $\textit{mask} = \text{0x5555}$,它的二进制表示为 $\text{0101 0101 0101 0101}_2$。那么 $n$ 与 $\textit{mask}$ 进行按位与运算,就可以得到 $n$ 的二进制表示中偶数下标的位,而 $n$ 与 $\textit{mask}$ 取反后再进行按位与运算,就可以得到 $n$ 的二进制表示中奇数下标的位。统计这两个结果中 $1$ 的个数即可。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。其中 $n$ 为给定的整数。 diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md b/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md index 388f02fb909d7..4f9d0c5c07c9e 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md @@ -137,7 +137,7 @@ func evenOddBit(n int) []int { ```ts function evenOddBit(n: number): number[] { - const ans = new Array(2).fill(0); + const ans = Array(2).fill(0); for (let i = 0; n > 0; n >>= 1, i ^= 1) { ans[i] += n & 1; } @@ -171,7 +171,11 @@ impl Solution { -### Solution 2 +### Solution 2: Bit Manipulation + +We can define a mask $\textit{mask} = \text{0x5555}$, which is represented in binary as $\text{0101 0101 0101 0101}_2$. Then, performing a bitwise AND operation between $n$ and $\textit{mask}$ will give us the bits at even indices in the binary representation of $n$. Performing a bitwise AND operation between $n$ and the complement of $\textit{mask}$ will give us the bits at odd indices in the binary representation of $n$. We can count the number of 1s in these two results. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. Here, $n$ is the given integer. diff --git a/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts index dd201acad879e..5ed6fc69ccdc5 100644 --- a/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts +++ b/solution/2500-2599/2595.Number of Even and Odd Bits/Solution.ts @@ -1,5 +1,5 @@ function evenOddBit(n: number): number[] { - const ans = new Array(2).fill(0); + const ans = Array(2).fill(0); for (let i = 0; n > 0; n >>= 1, i ^= 1) { ans[i] += n & 1; } diff --git a/solution/2500-2599/2596.Check Knight Tour Configuration/README.md b/solution/2500-2599/2596.Check Knight Tour Configuration/README.md index f4786763e53ef..a62ce3088fd5e 100644 --- a/solution/2500-2599/2596.Check Knight Tour Configuration/README.md +++ b/solution/2500-2599/2596.Check Knight Tour Configuration/README.md @@ -68,9 +68,9 @@ tags: ### 方法一:模拟 -我们先用数组 $pos$ 记录骑士访问的每个格子的坐标,然后遍历 $pos$ 数组,检查相邻两个格子的坐标差是否为 $(1, 2)$ 或 $(2, 1)$ 即可。若不满足,则返回 `false`。 +我们先用数组 $\textit{pos}$ 记录骑士访问的每个格子的坐标,然后遍历 $\textit{pos}$ 数组,检查相邻两个格子的坐标差是否为 $(1, 2)$ 或 $(2, 1)$ 即可。若不满足,则返回 $\textit{false}$。 -否则遍历结束后,返回 `true`。 +否则遍历结束后,返回 $\textit{true}$ 时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为棋盘的边长。 diff --git a/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md b/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md index 832c843a9eb15..adf3259d207c8 100644 --- a/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md +++ b/solution/2500-2599/2596.Check Knight Tour Configuration/README_EN.md @@ -65,11 +65,11 @@ tags: ### Solution 1: Simulation -We first use the array $pos$ to record the coordinates of the grid visited by the knight, and then traverse the $pos$ array to check whether the difference between the adjacent two grid coordinates is $(1, 2)$ or $(2, 1)$. If not, return `false`. +We first use an array $\textit{pos}$ to record the coordinates of each cell visited by the knight, then traverse the $\textit{pos}$ array and check if the coordinate difference between two adjacent cells is $(1, 2)$ or $(2, 1)$. If not, return $\textit{false}$. -Otherwise, return `true` after the traversal ends. +Otherwise, after the traversal, return $\textit{true}$. -The time complexity is $O(n^2)$ and the space complexity is $O(n^2)$, where $n$ is the length of the chessboard. +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the side length of the chessboard.