Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b3b4c2e

Browse files
Merge pull request SharingSource#117 from SharingSource/ac_oier
✨feat: Add 1588
2 parents 0ade64b + 16d6f99 commit b3b4c2e

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

‎Index/前缀和.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 |
1414
| [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩🤩 |
1515
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
16+
| [1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/) | 简单 | 🤩🤩🤩🤩🤩 |
1617
| [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 |
1718
| [1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?](https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/solution/gong-shui-san-xie-qian-zhui-he-qiu-jie-c-b38y/) | 中等 | 🤩🤩🤩🤩🤩 |
1819
| [1749. 任意子数组和的绝对值的最大值](https://leetcode-cn.com/problems/maximum-absolute-sum-of-any-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-absolute-sum-of-any-subarray/solution/xiang-jie-qian-zhui-he-jie-fa-fen-xi-si-yibby/) | 中等 | 🤩🤩🤩 |

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩🤩 |
3232
| [1049. 最后一块石头的重量 II](https://leetcode-cn.com/problems/last-stone-weight-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/last-stone-weight-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-neng-jgxik/) | 中等 | 🤩🤩🤩🤩 |
3333
| [1486. 数组异或操作](https://leetcode-cn.com/problems/xor-operation-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-operation-in-an-array/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-dggg/) | 简单 | 🤩🤩🤩 |
34+
| [1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/) | 简单 | 🤩🤩🤩🤩 |
3435
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
3536
| [1734. 解码异或后的排列](https://leetcode-cn.com/problems/decode-xored-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-permutation/solution/gong-shui-san-xie-note-bie-pian-li-yong-zeh6o/) | 中等 | 🤩🤩🤩🤩 |
3637
| [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 |
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/)** ,难度为 **简单**
4+
5+
Tag : 「前缀和」、「数学」
6+
7+
8+
9+
给你一个正整数数组 `arr` ,请你计算所有可能的奇数长度子数组的和。
10+
11+
子数组 定义为原数组中的一个连续子序列。
12+
13+
请你返回 `arr` 中 所有奇数长度子数组的和 。
14+
15+
示例 1:
16+
```
17+
输入:arr = [1,4,2,5,3]
18+
19+
输出:58
20+
21+
解释:所有奇数长度子数组和它们的和为:
22+
[1] = 1
23+
[4] = 4
24+
[2] = 2
25+
[5] = 5
26+
[3] = 3
27+
[1,4,2] = 7
28+
[4,2,5] = 11
29+
[2,5,3] = 10
30+
[1,4,2,5,3] = 15
31+
我们将所有值求和得到 1 +たす 4 +たす 2 +たす 5 +たす 3 +たす 7 +たす 11 +たす 10 +たす 15 = 58
32+
```
33+
示例 2:
34+
```
35+
输入:arr = [1,2]
36+
37+
输出:3
38+
39+
解释:总共只有 2 个长度为奇数的子数组,[1] 和 [2]。它们的和为 3 。
40+
```
41+
示例 3:
42+
```
43+
输入:arr = [10,11,12]
44+
45+
输出:66
46+
```
47+
48+
提示:
49+
* 1 <= arr.length <= 100
50+
* 1 <= arr[i] <= 1000
51+
52+
---
53+
54+
### 前缀和
55+
56+
枚举所有长度为奇数的子数组,我们可以通过「枚举长度 - 枚举左端点,并计算右端点」的两层循环来做。
57+
58+
而对于区间 $[l, r]$ 的和问题,可以直接再加一层循环来做,这样复杂度来到了 $O(n^3),ドル但本题数据范围只有 100ドル,ドル也是可以过的。
59+
60+
对于此类区间求和问题,我们应当想到使用「前缀和」进行优化:使用 $O(n)$ 的复杂度预处理出前缀和数组,每次查询 $[l, r]$ 区间和可以在 $O(1)$ 返回。
61+
62+
代码:
63+
```Java
64+
class Solution {
65+
public int sumOddLengthSubarrays(int[] arr) {
66+
int n = arr.length;
67+
int[] sum = new int[n + 1];
68+
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + arr[i - 1];
69+
int ans = 0;
70+
for (int len = 1; len <= n; len += 2) {
71+
for (int l = 0; l + len - 1 < n; l++) {
72+
int r = l + len - 1;
73+
ans += sum[r + 1] - sum[l];
74+
}
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
* 时间复杂度:$O(n^2)$
81+
* 空间复杂度:$O(n)$
82+
83+
----
84+
85+
### 数学
86+
87+
事实上,我们可以统计任意只 $arr[i]$ 在奇数子数组的出现次数。
88+
89+
对于原数组的任意位置 $i$ 而言,其左边共有 $i$ 个数,右边共有 $n - i - 1$ 个数。
90+
91+
**$arr[i]$ 作为某个奇数子数组的成员的充要条件为:其所在奇数子数组左右两边元素个数奇偶性相同。**
92+
93+
于是问题转换为如何求得「$arr[i]$ 在原数组中左边连续一段元素个为奇数的方案数」和「$arr[i]$ 在原数组右边连续一段元素个数为偶数的方案数」。
94+
95+
由于我们已经知道 $arr[i]$ 左边共有 $i$ 个数,右边共有 $n - i - 1$ 个数,因此可以算得组合数:
96+
97+
* 位置 $i$ 左边奇数个数的方案数为 $(i + 1) / 2,ドル右边奇数个数的方案数为 $(n - i) / 2$;
98+
* 位置 $i$ 右边偶数(非零)个数的方案数为 $i / 2,ドル右边偶数(非零)个数的方案数为 $(n - i - 1) / 2$;
99+
* 考虑左右两边不选也属于合法的偶数个数方案数,因此在上述分析基础上对偶数方案数自增 1ドル$。
100+
101+
至此,我们得到了位置 $i$ 左右奇数和偶数的方案数个数,根据「如果 $arr[i]$ 位于奇数子数组中,其左右两边元素个数奇偶性相同」以及「乘法原理」,我们知道 $arr[i]$ 同出现在多少个奇数子数组中,再乘上 $arr[i]$ 即是 $arr[i]$ 对答案的贡献。
102+
103+
代码:
104+
```Java
105+
class Solution {
106+
public int sumOddLengthSubarrays(int[] arr) {
107+
int n = arr.length;
108+
int ans = 0;
109+
for (int i = 0; i < n; i++) {
110+
int l1 = (i + 1) / 2, r1 = (n - i) / 2; // 奇数
111+
int l2 = i / 2, r2 = (n - i - 1) / 2; // 偶数
112+
l2++; r2++;
113+
ans += (l1 * r1 + l2 * r2) * arr[i];
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
* 时间复杂度:$O(n)$
120+
* 空间复杂度:$O(1)$
121+
122+
---
123+
124+
### 最后
125+
126+
这是我们「刷穿 LeetCode」系列文章的第 `No.1588` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
127+
128+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
129+
130+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
131+
132+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
133+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /