diff --git a/solution/1900-1999/1927.Sum Game/README.md b/solution/1900-1999/1927.Sum Game/README.md index f898f811c61a9..b5e4f82974c76 100644 --- a/solution/1900-1999/1927.Sum Game/README.md +++ b/solution/1900-1999/1927.Sum Game/README.md @@ -71,6 +71,24 @@ Bob 获胜,因为 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7 。 +**方法一:分类讨论** + +如果 `'?'` 的个数为奇数,那么 Alice 一定会获胜,因为她可以选择将最后一个 `'?'` 替换为任何一个数字,使得前一半的和与后一半的和不相等。 + +如果 `'?'` 的个数为偶数,Alice 为了使得前一半的和与后一半的和不相等,那么会选择在当前和较大的一半数字中放置 $9$,在当前和较小的一半数字中放置 $0$,而 Bob 为了使得前后两半的和相等,那么会选择在 Alice 替换数字的另一半放置一个与 Alice 替换数字相同的数字。 + +因此,最终会使得剩下的所有偶数个 `'?'` 集中在其中一半。假设当前两半的数字差值为 $d$。 + +我们先考虑,如果剩下两个 `'?'`,差值为 $x$,此时: + +- 如果 $x \lt 9$,那么 Alice 必胜,因为 Alice 可以将其中一个 `'?'` 替换为 $9$,使得前一半的和与后一半的和不相等; +- 如果 $x \gt 9$,那么 Alice 必胜,因为 Alice 可以将其中一个 `'?'` 替换为 $0$,使得前一半的和与后一半的和不相等; +- 如果 $x = 9$,那么 Bob 必胜,假设 Alice 替换的数字为 $a$,那么 Bob 可以将另一个 `'?'` 替换为 $9 - a$,使得前后两半的和相等。 + +因此,如果两半数字差值为 $d= \frac{9 \times cnt}{2}$,其中 $cnt$ 为剩下的 `'?'` 的个数,那么 Bob 必胜,否则 Alice 必胜。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -78,7 +96,14 @@ Bob 获胜,因为 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7 。 ```python - +class Solution: + def sumGame(self, num: str) -> bool: + n = len(num) + cnt1 = num[: n // 2].count("?") + cnt2 = num[n // 2 :].count("?") + s1 = sum(int(x) for x in num[: n // 2] if x != "?") + s2 = sum(int(x) for x in num[n // 2 :] if x != "?") + return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2 ``` ### **Java** @@ -86,7 +111,104 @@ Bob 获胜,因为 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7 。 ```java +class Solution { + public boolean sumGame(String num) { + int n = num.length(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num.charAt(i) == '?') { + cnt1++; + } else { + s1 += num.charAt(i) - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num.charAt(i) == '?') { + cnt2++; + } else { + s2 += num.charAt(i) - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool sumGame(string num) { + int n = num.size(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num[i] == '?') { + cnt1++; + } else { + s1 += num[i] - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num[i] == '?') { + cnt2++; + } else { + s2 += num[i] - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2; + } +}; +``` + +### **Go** + +```go +func sumGame(num string) bool { + n := len(num) + var cnt1, cnt2, s1, s2 int + for i := 0; i < n/2; i++ { + if num[i] == '?' { + cnt1++ + } else { + s1 += int(num[i] - '0') + } + } + for i := n / 2; i < n; i++ { + if num[i] == '?' { + cnt2++ + } else { + s2 += int(num[i] - '0') + } + } + return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2 +} +``` +### **TypeScript** + +```ts +function sumGame(num: string): boolean { + const n = num.length; + let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0]; + for (let i = 0; i < n >> 1; ++i) { + if (num[i] === '?') { + ++cnt1; + } else { + s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + for (let i = n >> 1; i < n; ++i) { + if (num[i] === '?') { + ++cnt2; + } else { + s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1); +} ``` ### **...** diff --git a/solution/1900-1999/1927.Sum Game/README_EN.md b/solution/1900-1999/1927.Sum Game/README_EN.md index ac21f84d3ccc4..4e4a5b8d267ba 100644 --- a/solution/1900-1999/1927.Sum Game/README_EN.md +++ b/solution/1900-1999/1927.Sum Game/README_EN.md @@ -70,13 +70,117 @@ Bob wins because 9 + 3 + 2 + 9 = 5 + 9 + 2 + 7. ### **Python3** ```python - +class Solution: + def sumGame(self, num: str) -> bool: + n = len(num) + cnt1 = num[: n // 2].count("?") + cnt2 = num[n // 2 :].count("?") + s1 = sum(int(x) for x in num[: n // 2] if x != "?") + s2 = sum(int(x) for x in num[n // 2 :] if x != "?") + return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2 ``` ### **Java** ```java +class Solution { + public boolean sumGame(String num) { + int n = num.length(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num.charAt(i) == '?') { + cnt1++; + } else { + s1 += num.charAt(i) - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num.charAt(i) == '?') { + cnt2++; + } else { + s2 += num.charAt(i) - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool sumGame(string num) { + int n = num.size(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num[i] == '?') { + cnt1++; + } else { + s1 += num[i] - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num[i] == '?') { + cnt2++; + } else { + s2 += num[i] - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2; + } +}; +``` + +### **Go** + +```go +func sumGame(num string) bool { + n := len(num) + var cnt1, cnt2, s1, s2 int + for i := 0; i < n/2; i++ { + if num[i] == '?' { + cnt1++ + } else { + s1 += int(num[i] - '0') + } + } + for i := n / 2; i < n; i++ { + if num[i] == '?' { + cnt2++ + } else { + s2 += int(num[i] - '0') + } + } + return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2 +} +``` +### **TypeScript** + +```ts +function sumGame(num: string): boolean { + const n = num.length; + let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0]; + for (let i = 0; i < n >> 1; ++i) { + if (num[i] === '?') { + ++cnt1; + } else { + s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + for (let i = n >> 1; i < n; ++i) { + if (num[i] === '?') { + ++cnt2; + } else { + s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1); +} ``` ### **...** diff --git a/solution/1900-1999/1927.Sum Game/Solution.cpp b/solution/1900-1999/1927.Sum Game/Solution.cpp new file mode 100644 index 0000000000000..c987b30f2dfbb --- /dev/null +++ b/solution/1900-1999/1927.Sum Game/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + bool sumGame(string num) { + int n = num.size(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num[i] == '?') { + cnt1++; + } else { + s1 += num[i] - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num[i] == '?') { + cnt2++; + } else { + s2 += num[i] - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || (s1 - s2) != 9 * (cnt2 - cnt1) / 2; + } +}; \ No newline at end of file diff --git a/solution/1900-1999/1927.Sum Game/Solution.go b/solution/1900-1999/1927.Sum Game/Solution.go new file mode 100644 index 0000000000000..f55de12dbe220 --- /dev/null +++ b/solution/1900-1999/1927.Sum Game/Solution.go @@ -0,0 +1,19 @@ +func sumGame(num string) bool { + n := len(num) + var cnt1, cnt2, s1, s2 int + for i := 0; i < n/2; i++ { + if num[i] == '?' { + cnt1++ + } else { + s1 += int(num[i] - '0') + } + } + for i := n / 2; i < n; i++ { + if num[i] == '?' { + cnt2++ + } else { + s2 += int(num[i] - '0') + } + } + return (cnt1+cnt2)%2 == 1 || s1-s2 != (cnt2-cnt1)*9/2 +} \ No newline at end of file diff --git a/solution/1900-1999/1927.Sum Game/Solution.java b/solution/1900-1999/1927.Sum Game/Solution.java new file mode 100644 index 0000000000000..86104bf359d74 --- /dev/null +++ b/solution/1900-1999/1927.Sum Game/Solution.java @@ -0,0 +1,22 @@ +class Solution { + public boolean sumGame(String num) { + int n = num.length(); + int cnt1 = 0, cnt2 = 0; + int s1 = 0, s2 = 0; + for (int i = 0; i < n / 2; ++i) { + if (num.charAt(i) == '?') { + cnt1++; + } else { + s1 += num.charAt(i) - '0'; + } + } + for (int i = n / 2; i < n; ++i) { + if (num.charAt(i) == '?') { + cnt2++; + } else { + s2 += num.charAt(i) - '0'; + } + } + return (cnt1 + cnt2) % 2 == 1 || s1 - s2 != 9 * (cnt2 - cnt1) / 2; + } +} \ No newline at end of file diff --git a/solution/1900-1999/1927.Sum Game/Solution.py b/solution/1900-1999/1927.Sum Game/Solution.py new file mode 100644 index 0000000000000..4832d1ba733f5 --- /dev/null +++ b/solution/1900-1999/1927.Sum Game/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def sumGame(self, num: str) -> bool: + n = len(num) + cnt1 = num[: n // 2].count("?") + cnt2 = num[n // 2 :].count("?") + s1 = sum(int(x) for x in num[: n // 2] if x != "?") + s2 = sum(int(x) for x in num[n // 2 :] if x != "?") + return (cnt1 + cnt2) % 2 == 1 or s1 - s2 != 9 * (cnt2 - cnt1) // 2 diff --git a/solution/1900-1999/1927.Sum Game/Solution.ts b/solution/1900-1999/1927.Sum Game/Solution.ts new file mode 100644 index 0000000000000..3c412b01ff948 --- /dev/null +++ b/solution/1900-1999/1927.Sum Game/Solution.ts @@ -0,0 +1,19 @@ +function sumGame(num: string): boolean { + const n = num.length; + let [cnt1, cnt2, s1, s2] = [0, 0, 0, 0]; + for (let i = 0; i < n >> 1; ++i) { + if (num[i] === '?') { + ++cnt1; + } else { + s1 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + for (let i = n >> 1; i < n; ++i) { + if (num[i] === '?') { + ++cnt2; + } else { + s2 += num[i].charCodeAt(0) - '0'.charCodeAt(0); + } + } + return (cnt1 + cnt2) % 2 === 1 || 2 * (s1 - s2) !== 9 * (cnt2 - cnt1); +} diff --git a/solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md b/solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md index 30b9b94808675..b1310eb046150 100644 --- a/solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md +++ b/solution/2700-2799/2761.Prime Pairs With Target Sum/README_EN.md @@ -43,6 +43,16 @@ These pairs are [3,7] and [5,5], and we return them in the sorted order as descr ## Solutions +**Solution 1: Preprocessing + Enumeration** + +First, we pre-process all the prime numbers within the range of $n$, and record them in the array $primes$, where $primes[i]$ is `true` if $i$ is a prime number. + +Next, we enumerate $x$ in the range of $[2, \frac{n}{2}]$. In this case, $y = n - x$. If both $primes[x]$ and $primes[y]$ are `true`, then $(x, y)$ is a pair of prime numbers, which is added to the answer. + +After the enumeration is complete, we return the answer. + +The time complexity is $O(n \log \log n)$ and the space complexity is $O(n)$, where $n$ is the number given in the problem. + ### **Python3** diff --git a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md index 338310737be32..2737ff350f0b2 100644 --- a/solution/2700-2799/2762.Continuous Subarrays/README_EN.md +++ b/solution/2700-2799/2762.Continuous Subarrays/README_EN.md @@ -53,6 +53,16 @@ Total continuous subarrays = 3 + 2 + 1 = 6. ## Solutions +**Solution 1: Ordered List + Two Pointers** + +We can use two pointers, $i$ and $j$, to maintain the left and right endpoints of the current subarray, and use an ordered list to maintain all elements in the current subarray. + +Iterate through the array $nums$. For the current number $nums[i]$ we're iterating over, we add it to the ordered list. If the difference between the maximum and minimum values in the ordered list is greater than $2$, we then loop to move the pointer $i$ to the right, continuously removing $nums[i]$ from the ordered list, until the list is empty or the maximum difference between elements in the ordered list is not greater than $2$. At this point, the number of uninterrupted subarrays is $j - i + 1$, which we add to the answer. + +After the iteration, return the answer. + +The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$. + ### **Python3** diff --git a/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md b/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md index ddb5cb07a2590..e2ae6dd171e09 100644 --- a/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md +++ b/solution/2700-2799/2763.Sum of Imbalance Numbers of All Subarrays/README_EN.md @@ -55,6 +55,20 @@ The imbalance number of all other subarrays is 0. Hence, the sum of imbalance nu ## Solutions +**Solution 1: Enumeration + Ordered Set** + +We can first enumerate the left endpoint $i$ of the subarray. For each $i$, we enumerate the right endpoint $j$ of the subarray from small to large, and maintain all the elements in the current subarray with an ordered list. We also use a variable $cnt$ to maintain the unbalanced number of the current subarray. + +For each number $nums[j]$, we find the first element $nums[k]$ in the ordered list that is greater than or equal to $nums[j]$, and the last element $nums[h]$ that is less than $nums[j]$: + +- If $nums[k]$ exists, and the difference between $nums[k]$ and $nums[j]$ is more than $1$, the unbalanced number increases by $1$; +- If $nums[h]$ exists, and the difference between $nums[j]$ and $nums[h]$ is more than $1$, the unbalanced number increases by $1$; +- If both $nums[k]$ and $nums[h]$ exist, then inserting the element $nums[j]$ between $nums[h]$ and $nums[k]$ will reduce the unbalanced number by $1$. + +Then, we add the unbalanced number of the current subarray to the answer, and continue the iteration until we finish iterating over all subarrays. + +The time complexity is $O(n^2 \times \log n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $nums$. + ### **Python3** diff --git "a/solution/2700-2799/2764.is Array a Preorder of Some \342\200\214Binary Tree/README_EN.md" "b/solution/2700-2799/2764.is Array a Preorder of Some \342\200\214Binary Tree/README_EN.md" index e974150c24f7f..09610786d812e 100644 --- "a/solution/2700-2799/2764.is Array a Preorder of Some \342\200\214Binary Tree/README_EN.md" +++ "b/solution/2700-2799/2764.is Array a Preorder of Some \342\200\214Binary Tree/README_EN.md" @@ -48,6 +48,20 @@ For the preorder traversal, first we visit node 0, then we do the preorder trave ## Solutions +**Solution 1:Depth-First Search** + +First, we construct a graph $g$ based on the $nodes$ data, where $g[i]$ represents all the child nodes of node $i$. + +Next, we design a function $dfs(i)$, which represents a pre-order traversal starting from node $i$. We use a variable $k$ to represent the $k$-th node in the $nodes$ list that we have currently traversed, with an initial value of $k = 0$. + +The execution logic of the function $dfs(i)$ is as follows: + +If $i \neq nodes[k][0]$, it indicates that the current sequence is not a pre-order traversal sequence of a binary tree, and returns false. +Otherwise, we increment $k$ by $1$, and then recursively search all child nodes of $i$. If a false is found during the search, we return false immediately. Otherwise, when the search is finished, we return true. +In the main function, we call $dfs(nodes[0][0])$. If the return value is true and $k = |nodes|$, then the $nodes$ sequence is a pre-order traversal sequence of a binary tree, and we return true; otherwise, we return false. + +The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the number of nodes in $nodes$. + ### **Python3** diff --git a/solution/2700-2799/2771.Longest Non-decreasing Subarray From Two Arrays/README_EN.md b/solution/2700-2799/2771.Longest Non-decreasing Subarray From Two Arrays/README_EN.md index 0d84a441a1e30..da9d299b986ee 100644 --- a/solution/2700-2799/2771.Longest Non-decreasing Subarray From Two Arrays/README_EN.md +++ b/solution/2700-2799/2771.Longest Non-decreasing Subarray From Two Arrays/README_EN.md @@ -72,7 +72,7 @@ Then, we update $f = ff$ and $g = gg$, and update $ans$ to $max(ans, f, g)$. After the traversal is over, we return $ans$. -Time complexity $O(n)$, where $n$ is the length of the array. Space complexity $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.