diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md index c86a9c60d77d3..de294a24867bc 100644 --- a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README.md @@ -182,6 +182,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool { } ``` +#### TypeScript + +```ts +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + +#### JavaScript + +```js +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md index dcc51a67c5cc1..4940fe3be8258 100644 --- a/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/README_EN.md @@ -178,6 +178,34 @@ func flipEquiv(root1 *TreeNode, root2 *TreeNode) bool { } ``` +#### TypeScript + +```ts +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + +#### JavaScript + +```js +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} +``` + diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js new file mode 100644 index 0000000000000..60af04dec4ae8 --- /dev/null +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.js @@ -0,0 +1,9 @@ +function flipEquiv(root1, root2) { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1; + const { left: l2, right: r2 } = root2; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} diff --git a/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts new file mode 100644 index 0000000000000..a3a67fb2335fe --- /dev/null +++ b/solution/0900-0999/0951.Flip Equivalent Binary Trees/Solution.ts @@ -0,0 +1,9 @@ +function flipEquiv(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === root2) return true; + if (!root1 || !root2 || root1?.val !== root2?.val) return false; + + const { left: l1, right: r1 } = root1!; + const { left: l2, right: r2 } = root2!; + + return (flipEquiv(l1, l2) && flipEquiv(r1, r2)) || (flipEquiv(l1, r2) && flipEquiv(r1, l2)); +} diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/README.md b/solution/2000-2099/2090.K Radius Subarray Averages/README.md index 9dc7e762f6bf6..e06fd1cc572ba 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/README.md +++ b/solution/2000-2099/2090.K Radius Subarray Averages/README.md @@ -84,21 +84,17 @@ tags: -### 方法一:滑动窗口(写法一) +### 方法一:滑动窗口 -半径为 $k$ 的子数组个数为 $k \times 2 + 1$,因此,我们不妨将 $k \times 2 + 1$ 记为 $k$。 +半径为 $k$ 的子数组的长度为 $k \times 2 + 1$,因此我们可以维护一个大小为 $k \times 2 + 1$ 的窗口,记窗口中的所有元素和为 $s$。 -我们创建一个长度为 $n$ 的答案数组 $ans$,初始时每项元素均为 $-1$。 +我们创建一个长度为 $n$ 的答案数组 $\textit{ans}$,初始时每个元素都为 $-1$。 -接下来,我们首先判断 $k$ 是否大于数组 `nums` 的长度 $n$,如果是,则直接返回答案数组。 - -否则,我们计算数组 `nums` 的前 $k$ 个元素的和 $s$,并将其除以 $k$ 得到的商赋值给答案数组 $ans$ 的第 $j$ 个元素,其中 $j = k / 2$。 - -然后,我们从 $k$ 开始遍历数组 `nums`,每次遍历时,我们将 $nums[i]$ 的值加到 $s$ 中,同时减去 $nums[i - k]$ 的值,并且更新 $j = j + 1$,那么我们就得到了以第 $j$ 个元素为中心,半径为 $k$ 的子数组的和 $s$,将其除以 $k$ 得到的商赋值给答案数组 $ans$ 的第 $j$ 个元素。 +接下来,我们遍历数组 $\textit{nums}$,将 $\textit{nums}[i]$ 的值加到窗口的和 $s$ 中,如果此时 $i \geq k \times 2$,说明此时窗口大小为 $k \times 2 + 1$,那么 $\textit{ans}[i-k] = \frac{s}{k \times 2 + 1}$,然后我们将 $\textit{nums}[i - k \times 2]$ 的值从窗口和 $s$ 中移出。继续遍历下个元素。 最后返回答案数组即可。 -时间复杂度 $O(n)$,其中 $n$ 为数组 `nums` 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 @@ -107,150 +103,11 @@ tags: ```python class Solution: def getAverages(self, nums: List[int], k: int) -> List[int]: - k = k << 1 | 1 n = len(nums) ans = [-1] * n - if k > n: - return ans - s = sum(nums[:k]) - j = k // 2 - ans[j] = s // k - for i in range(k, n): - j += 1 - s += nums[i] - nums[i - k] - ans[j] = s // k - return ans -``` - -#### Java - -```java -class Solution { - public int[] getAverages(int[] nums, int k) { - k = k << 1 | 1; - int n = nums.length; - int[] ans = new int[n]; - Arrays.fill(ans, -1); - if (k > n) { - return ans; - } - long s = 0; - for (int i = 0; i < k; ++i) { - s += nums[i]; - } - int j = k / 2; - ans[j] = (int) (s / k); - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = (int) (s / k); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector getAverages(vector& nums, int k) { - k = k << 1 | 1; - int n = nums.size(); - vector ans(n, -1); - if (k > n) { - return ans; - } - long long s = accumulate(nums.begin(), nums.begin() + k, 0LL); - int j = k / 2; - ans[j] = s / k; - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = s / k; - } - return ans; - } -}; -``` - -#### Go - -```go -func getAverages(nums []int, k int) []int { - k = k<<1 | 1 - n := len(nums) - ans := make([]int, n) - for i := range ans { - ans[i] = -1 - } - if k > n { - return ans - } - s := 0 - for _, x := range nums[:k] { - s += x - } - j := k >> 1 - ans[j] = s / k - for i := k; i < n; i++ { - s += nums[i] - nums[i-k] - j++ - ans[j] = s / k - } - return ans -} -``` - -#### TypeScript - -```ts -function getAverages(nums: number[], k: number): number[] { - k = (k << 1) | 1; - const n = nums.length; - const ans: number[] = Array(n).fill(-1); - if (k > n) { - return ans; - } - let s = nums.slice(0, k).reduce((acc, cur) => acc + cur, 0); - let j = k >> 1; - ans[j] = Math.floor(s / k); - for (let i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = Math.floor(s / k); - } - return ans; -} -``` - - - - - - - -### 方法二:滑动窗口的另一种写法 - -我们维护一个大小为 $k \times 2 + 1$ 的窗口,记窗口中的所有元素和为 $s$。 - -与方法一一样,我们创建一个长度为 $n$ 的答案数组 $ans$,初始时每项元素均为 $-1$。 - -接下来遍历数组 `nums`,将 $nums[i]$ 的值加到窗口的和 $s$ 中,如果此时 $i \geq k \times 2$,说明此时窗口大小为 $k \times 2 + 1$,那么 $ans[i-k] = \frac{s}{k \times 2 + 1}$,然后我们将 $nums[i - k \times 2]$ 的值从窗口和 $s$ 中移出。继续遍历下个元素。 - -最后返回答案数组即可。 - -时间复杂度 $O(n)$,其中 $n$ 为数组 `nums` 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。 - - - -#### Python3 - -```python -class Solution: - def getAverages(self, nums: List[int], k: int) -> List[int]: s = 0 - ans = [-1] * len(nums) - for i, v in enumerate(nums): - s += v + for i, x in enumerate(nums): + s += x if i >= k * 2: ans[i - k] = s // (k * 2 + 1) s -= nums[i - k * 2] @@ -286,7 +143,7 @@ public: vector getAverages(vector& nums, int k) { int n = nums.size(); vector ans(n, -1); - long s = 0; + long long s = 0; for (int i = 0; i < n; ++i) { s += nums[i]; if (i >= k * 2) { @@ -304,10 +161,12 @@ public: ```go func getAverages(nums []int, k int) []int { ans := make([]int, len(nums)) - s := 0 - for i, v := range nums { + for i := range ans { ans[i] = -1 - s += v + } + s := 0 + for i, x := range nums { + s += x if i >= k*2 { ans[i-k] = s / (k*2 + 1) s -= nums[i-k*2] @@ -322,7 +181,7 @@ func getAverages(nums []int, k int) []int { ```ts function getAverages(nums: number[], k: number): number[] { const n = nums.length; - const ans: number[] = new Array(n).fill(-1); + const ans: number[] = Array(n).fill(-1); let s = 0; for (let i = 0; i < n; ++i) { s += nums[i]; diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/README_EN.md b/solution/2000-2099/2090.K Radius Subarray Averages/README_EN.md index e4a843725e86d..441b3cb4da442 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/README_EN.md +++ b/solution/2000-2099/2090.K Radius Subarray Averages/README_EN.md @@ -61,7 +61,7 @@ tags:
 Input: nums = [8], k = 100000
 Output: [-1]
-Explanation: 
+Explanation:
 - avg[0] is -1 because there are less than k elements before and after index 0.
 
@@ -82,19 +82,15 @@ tags: ### Solution 1: Sliding Window -The number of elements in a subarray with radius $k$ is $k \times 2 + 1$. Therefore, we can redefine $k$ as $k \times 2 + 1$. - -We create an answer array $ans$ of length $n$, initially each element is $-1$. - -Next, we first check whether $k$ is greater than the length $n$ of the array `nums`. If it is, we directly return the answer array. +The length of a subarray with radius $k$ is $k \times 2 + 1$, so we can maintain a window of size $k \times 2 + 1$ and denote the sum of all elements in the window as $s$. -Otherwise, we calculate the sum $s$ of the first $k$ elements of the array `nums`, and assign the quotient of $s$ divided by $k$ to the $j$-th element of the answer array $ans$, where $j = k / 2$. +We create an answer array $\textit{ans}$ of length $n$, initially setting each element to $-1$. -Then, we start traversing the array `nums` from $k$. For each iteration, we add the value of $nums[i]$ to $s$ and subtract the value of $nums[i - k]$, and update $j = j + 1$. Then we get the sum $s$ of the subarray with the $j$-th element as the center and radius $k$, and assign the quotient of $s$ divided by $k$ to the $j$-th element of the answer array $ans$. +Next, we traverse the array $\textit{nums}$, adding the value of $\textit{nums}[i]$ to the window sum $s$. If $i \geq k \times 2$, it means the window size is $k \times 2 + 1$, so we set $\textit{ans}[i-k] = \frac{s}{k \times 2 + 1}$. Then, we remove the value of $\textit{nums}[i - k \times 2]$ from the window sum $s$. Continue traversing the next element. -Finally, we return the answer array. +Finally, return the answer array. -The time complexity is $O(n)$, where $n$ is the length of the array `nums`. Ignoring the space consumption of the answer, the space complexity is $O(1)$. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. @@ -103,150 +99,11 @@ The time complexity is $O(n)$, where $n$ is the length of the array `nums`. Igno ```python class Solution: def getAverages(self, nums: List[int], k: int) -> List[int]: - k = k << 1 | 1 n = len(nums) ans = [-1] * n - if k > n: - return ans - s = sum(nums[:k]) - j = k // 2 - ans[j] = s // k - for i in range(k, n): - j += 1 - s += nums[i] - nums[i - k] - ans[j] = s // k - return ans -``` - -#### Java - -```java -class Solution { - public int[] getAverages(int[] nums, int k) { - k = k << 1 | 1; - int n = nums.length; - int[] ans = new int[n]; - Arrays.fill(ans, -1); - if (k > n) { - return ans; - } - long s = 0; - for (int i = 0; i < k; ++i) { - s += nums[i]; - } - int j = k / 2; - ans[j] = (int) (s / k); - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = (int) (s / k); - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - vector getAverages(vector& nums, int k) { - k = k << 1 | 1; - int n = nums.size(); - vector ans(n, -1); - if (k > n) { - return ans; - } - long long s = accumulate(nums.begin(), nums.begin() + k, 0LL); - int j = k / 2; - ans[j] = s / k; - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = s / k; - } - return ans; - } -}; -``` - -#### Go - -```go -func getAverages(nums []int, k int) []int { - k = k<<1 | 1 - n := len(nums) - ans := make([]int, n) - for i := range ans { - ans[i] = -1 - } - if k > n { - return ans - } - s := 0 - for _, x := range nums[:k] { - s += x - } - j := k >> 1 - ans[j] = s / k - for i := k; i < n; i++ { - s += nums[i] - nums[i-k] - j++ - ans[j] = s / k - } - return ans -} -``` - -#### TypeScript - -```ts -function getAverages(nums: number[], k: number): number[] { - k = (k << 1) | 1; - const n = nums.length; - const ans: number[] = Array(n).fill(-1); - if (k > n) { - return ans; - } - let s = nums.slice(0, k).reduce((acc, cur) => acc + cur, 0); - let j = k >> 1; - ans[j] = Math.floor(s / k); - for (let i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = Math.floor(s / k); - } - return ans; -} -``` - - - - - - - -### Solution 2: Another Way of Sliding Window - -We maintain a window of size $k \times 2 + 1$, and let the sum of all elements in the window be $s$. - -Like Solution 1, we create an answer array $ans$ of length $n$, initially each element is $-1$. - -Next, we traverse the array `nums`, add the value of $nums[i]$ to the sum $s$. If $i \geq k \times 2$, it means the window size is $k \times 2 + 1$ now, so we set $ans[i-k] = \frac{s}{k \times 2 + 1}$, then we subtract the value of $nums[i - k \times 2]$ from the sum $s$. Continue to the next element. - -Finally, we return the answer array. - -The time complexity is $O(n)$, where $n$ is the length of the array `nums`. Ignoring the space consumption of the answer, the space complexity is $O(1)$. - - - -#### Python3 - -```python -class Solution: - def getAverages(self, nums: List[int], k: int) -> List[int]: s = 0 - ans = [-1] * len(nums) - for i, v in enumerate(nums): - s += v + for i, x in enumerate(nums): + s += x if i >= k * 2: ans[i - k] = s // (k * 2 + 1) s -= nums[i - k * 2] @@ -282,7 +139,7 @@ public: vector getAverages(vector& nums, int k) { int n = nums.size(); vector ans(n, -1); - long s = 0; + long long s = 0; for (int i = 0; i < n; ++i) { s += nums[i]; if (i >= k * 2) { @@ -300,10 +157,12 @@ public: ```go func getAverages(nums []int, k int) []int { ans := make([]int, len(nums)) - s := 0 - for i, v := range nums { + for i := range ans { ans[i] = -1 - s += v + } + s := 0 + for i, x := range nums { + s += x if i >= k*2 { ans[i-k] = s / (k*2 + 1) s -= nums[i-k*2] @@ -318,7 +177,7 @@ func getAverages(nums []int, k int) []int { ```ts function getAverages(nums: number[], k: number): number[] { const n = nums.length; - const ans: number[] = new Array(n).fill(-1); + const ans: number[] = Array(n).fill(-1); let s = 0; for (let i = 0; i < n; ++i) { s += nums[i]; diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.cpp b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.cpp index 0c64049ffec87..5ca8849427d36 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.cpp +++ b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.cpp @@ -1,19 +1,16 @@ class Solution { public: vector getAverages(vector& nums, int k) { - k = k << 1 | 1; int n = nums.size(); vector ans(n, -1); - if (k > n) { - return ans; - } - long long s = accumulate(nums.begin(), nums.begin() + k, 0LL); - int j = k / 2; - ans[j] = s / k; - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = s / k; + long long s = 0; + for (int i = 0; i < n; ++i) { + s += nums[i]; + if (i >= k * 2) { + ans[i - k] = s / (k * 2 + 1); + s -= nums[i - k * 2]; + } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.go b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.go index d5b5ffbb07574..6bd1b5aa02d5f 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.go +++ b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.go @@ -1,23 +1,15 @@ func getAverages(nums []int, k int) []int { - k = k<<1 | 1 - n := len(nums) - ans := make([]int, n) + ans := make([]int, len(nums)) for i := range ans { ans[i] = -1 } - if k > n { - return ans - } s := 0 - for _, x := range nums[:k] { + for i, x := range nums { s += x - } - j := k >> 1 - ans[j] = s / k - for i := k; i < n; i++ { - s += nums[i] - nums[i-k] - j++ - ans[j] = s / k + if i >= k*2 { + ans[i-k] = s / (k*2 + 1) + s -= nums[i-k*2] + } } return ans -} \ No newline at end of file +} diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.java b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.java index 2bc02c7104dab..0e168990c44da 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.java +++ b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.java @@ -1,22 +1,16 @@ class Solution { public int[] getAverages(int[] nums, int k) { - k = k << 1 | 1; int n = nums.length; int[] ans = new int[n]; Arrays.fill(ans, -1); - if (k > n) { - return ans; - } long s = 0; - for (int i = 0; i < k; ++i) { + for (int i = 0; i < n; ++i) { s += nums[i]; - } - int j = k / 2; - ans[j] = (int) (s / k); - for (int i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = (int) (s / k); + if (i >= k * 2) { + ans[i - k] = (int) (s / (k * 2 + 1)); + s -= nums[i - k * 2]; + } } return ans; } -} \ No newline at end of file +} diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.py b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.py index 50bfa412a2382..90c07ba287b82 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.py +++ b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.py @@ -1,15 +1,11 @@ class Solution: def getAverages(self, nums: List[int], k: int) -> List[int]: - k = k << 1 | 1 n = len(nums) ans = [-1] * n - if k > n: - return ans - s = sum(nums[:k]) - j = k // 2 - ans[j] = s // k - for i in range(k, n): - j += 1 - s += nums[i] - nums[i - k] - ans[j] = s // k + s = 0 + for i, x in enumerate(nums): + s += x + if i >= k * 2: + ans[i - k] = s // (k * 2 + 1) + s -= nums[i - k * 2] return ans diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.ts b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.ts index a5e96935e6088..fc11116e21f14 100644 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution.ts +++ b/solution/2000-2099/2090.K Radius Subarray Averages/Solution.ts @@ -1,16 +1,13 @@ function getAverages(nums: number[], k: number): number[] { - k = (k << 1) | 1; const n = nums.length; const ans: number[] = Array(n).fill(-1); - if (k > n) { - return ans; - } - let s = nums.slice(0, k).reduce((acc, cur) => acc + cur, 0); - let j = k >> 1; - ans[j] = Math.floor(s / k); - for (let i = k; i < n; ++i) { - s += nums[i] - nums[i - k]; - ans[++j] = Math.floor(s / k); + let s = 0; + for (let i = 0; i < n; ++i) { + s += nums[i]; + if (i >= k * 2) { + ans[i - k] = Math.floor(s / (k * 2 + 1)); + s -= nums[i - k * 2]; + } } return ans; } diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.cpp b/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.cpp deleted file mode 100644 index f5a1b6e64079e..0000000000000 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.cpp +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - vector getAverages(vector& nums, int k) { - int n = nums.size(); - vector ans(n, -1); - long s = 0; - for (int i = 0; i < n; ++i) { - s += nums[i]; - if (i >= k * 2) { - ans[i - k] = s / (k * 2 + 1); - s -= nums[i - k * 2]; - } - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.go b/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.go deleted file mode 100644 index 2a261c950e403..0000000000000 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.go +++ /dev/null @@ -1,13 +0,0 @@ -func getAverages(nums []int, k int) []int { - ans := make([]int, len(nums)) - s := 0 - for i, v := range nums { - ans[i] = -1 - s += v - if i >= k*2 { - ans[i-k] = s / (k*2 + 1) - s -= nums[i-k*2] - } - } - return ans -} \ No newline at end of file diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.java b/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.java deleted file mode 100644 index a4111a38ce296..0000000000000 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.java +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { - public int[] getAverages(int[] nums, int k) { - int n = nums.length; - int[] ans = new int[n]; - Arrays.fill(ans, -1); - long s = 0; - for (int i = 0; i < n; ++i) { - s += nums[i]; - if (i >= k * 2) { - ans[i - k] = (int) (s / (k * 2 + 1)); - s -= nums[i - k * 2]; - } - } - return ans; - } -} \ No newline at end of file diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.py b/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.py deleted file mode 100644 index 74e99367c92b2..0000000000000 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.py +++ /dev/null @@ -1,10 +0,0 @@ -class Solution: - def getAverages(self, nums: List[int], k: int) -> List[int]: - s = 0 - ans = [-1] * len(nums) - for i, v in enumerate(nums): - s += v - if i >= k * 2: - ans[i - k] = s // (k * 2 + 1) - s -= nums[i - k * 2] - return ans diff --git a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.ts b/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.ts deleted file mode 100644 index 95708268e942c..0000000000000 --- a/solution/2000-2099/2090.K Radius Subarray Averages/Solution2.ts +++ /dev/null @@ -1,13 +0,0 @@ -function getAverages(nums: number[], k: number): number[] { - const n = nums.length; - const ans: number[] = new Array(n).fill(-1); - let s = 0; - for (let i = 0; i < n; ++i) { - s += nums[i]; - if (i >= k * 2) { - ans[i - k] = Math.floor(s / (k * 2 + 1)); - s -= nums[i - k * 2]; - } - } - return ans; -} diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/README.md b/solution/2600-2699/2641.Cousins in Binary Tree II/README.md index c47f0afe07bc7..1fa10800f4826 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/README.md +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/README.md @@ -554,32 +554,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode { */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - t.push(right); - s += right.val; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + + q = qNext; + } + + return root; +} +``` + +#### JavaScript + +```js +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; } ``` diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md b/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md index 2b0f516247a68..3c29eff4e22eb 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/README_EN.md @@ -548,32 +548,66 @@ func replaceValueInTree(root *TreeNode) *TreeNode { */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - t.push(right); - s += right.val; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + + q = qNext; + } + + return root; +} +``` + +#### JavaScript + +```js +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; } ``` diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js new file mode 100644 index 0000000000000..48d76e08e06ac --- /dev/null +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.js @@ -0,0 +1,29 @@ +function replaceValueInTree(root) { + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); + } + + if (node.right) { + node.right.val = x; + qNext.push(node.right); + } + } + + q = qNext; + } + + return root; +} diff --git a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts index e110cb32b2db3..4191d2783fa74 100644 --- a/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts +++ b/solution/2600-2699/2641.Cousins in Binary Tree II/Solution2.ts @@ -13,31 +13,31 @@ */ function replaceValueInTree(root: TreeNode | null): TreeNode | null { - root.val = 0; - const q: TreeNode[] = [root]; - while (q.length > 0) { - const t: TreeNode[] = []; - let s = 0; - for (const { left, right } of q) { - if (left) { - t.push(left); - s += left.val; - } - if (right) { - t.push(right); - s += right.val; - } - } - for (const { left, right } of q) { - const sub = (left?.val || 0) + (right?.val || 0); - if (left) { - left.val = s - sub; + let q = [root]; + let [sum, nextSum] = [0, root.val]; + + while (q.length) { + const qNext: TreeNode[] = []; + [sum, nextSum] = [nextSum, 0]; + + for (const node of q) { + const x = (node.left?.val ?? 0) + (node.right?.val ?? 0); + node.val = sum - node.val; + nextSum += x; + + if (node.left) { + node.left.val = x; + qNext.push(node.left); } - if (right) { - right.val = s - sub; + + if (node.right) { + node.right.val = x; + qNext.push(node.right); } } - q.splice(0, q.length, ...t); + + q = qNext; } + return root; }