comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
简单 |
1408 |
第 35 场双周赛 Q1 |
|
给你一个正整数数组 arr
,请你计算所有可能的奇数长度子数组的和。
子数组 定义为原数组中的一个连续子序列。
请你返回 arr
中 所有奇数长度子数组的和 。
示例 1:
输入:arr = [1,4,2,5,3] 输出:58 解释:所有奇数长度子数组和它们的和为: [1] = 1 [4] = 4 [2] = 2 [5] = 5 [3] = 3 [1,4,2] = 7 [4,2,5] = 11 [2,5,3] = 10 [1,4,2,5,3] = 15 我们将所有值求和得到 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58
示例 2:
输入:arr = [1,2] 输出:3 解释:总共只有 2 个长度为奇数的子数组,[1] 和 [2]。它们的和为 3 。
示例 3:
输入:arr = [10,11,12] 输出:66
提示:
1 <= arr.length <= 100
1 <= arr[i] <= 1000
进阶:
你可以设计一个 O(n) 时间复杂度的算法解决此问题吗?
我们定义两个长度为
当
对于状态
对于状态
最终答案即为
时间复杂度
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
n = len(arr)
f = [0] * n
g = [0] * n
ans = f[0] = arr[0]
for i in range(1, n):
f[i] = g[i - 1] + arr[i] * (i // 2 + 1)
g[i] = f[i - 1] + arr[i] * ((i + 1) // 2)
ans += f[i]
return ans
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int n = arr.length;
int[] f = new int[n];
int[] g = new int[n];
int ans = f[0] = arr[0];
for (int i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * (i / 2 + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) / 2);
ans += f[i];
}
return ans;
}
}
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int n = arr.size();
vector<int> f(n, arr[0]);
vector<int> g(n);
int ans = f[0];
for (int i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * (i / 2 + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) / 2);
ans += f[i];
}
return ans;
}
};
func sumOddLengthSubarrays(arr []int) (ans int) {
n := len(arr)
f := make([]int, n)
g := make([]int, n)
f[0] = arr[0]
ans = f[0]
for i := 1; i < n; i++ {
f[i] = g[i-1] + arr[i]*(i/2+1)
g[i] = f[i-1] + arr[i]*((i+1)/2)
ans += f[i]
}
return
}
function sumOddLengthSubarrays(arr: number[]): number {
const n = arr.length;
const f: number[] = Array(n).fill(arr[0]);
const g: number[] = Array(n).fill(0);
let ans = f[0];
for (let i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * ((i >> 1) + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) >> 1);
ans += f[i];
}
return ans;
}
impl Solution {
pub fn sum_odd_length_subarrays(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut f = vec![0; n];
let mut g = vec![0; n];
let mut ans = arr[0];
f[0] = arr[0];
for i in 1..n {
f[i] = g[i - 1] + arr[i] * ((i as i32) / 2 + 1);
g[i] = f[i - 1] + arr[i] * (((i + 1) as i32) / 2);
ans += f[i];
}
ans
}
}
int sumOddLengthSubarrays(int* arr, int arrSize) {
int n = arrSize;
int f[n];
int g[n];
int ans = f[0] = arr[0];
g[0] = 0;
for (int i = 1; i < n; ++i) {
f[i] = g[i - 1] + arr[i] * (i / 2 + 1);
g[i] = f[i - 1] + arr[i] * ((i + 1) / 2);
ans += f[i];
}
return ans;
}
我们注意到,状态
时间复杂度
class Solution:
def sumOddLengthSubarrays(self, arr: List[int]) -> int:
ans, f, g = arr[0], arr[0], 0
for i in range(1, len(arr)):
ff = g + arr[i] * (i // 2 + 1)
gg = f + arr[i] * ((i + 1) // 2)
f, g = ff, gg
ans += f
return ans
class Solution {
public int sumOddLengthSubarrays(int[] arr) {
int ans = arr[0], f = arr[0], g = 0;
for (int i = 1; i < arr.length; ++i) {
int ff = g + arr[i] * (i / 2 + 1);
int gg = f + arr[i] * ((i + 1) / 2);
f = ff;
g = gg;
ans += f;
}
return ans;
}
}
class Solution {
public:
int sumOddLengthSubarrays(vector<int>& arr) {
int ans = arr[0], f = arr[0], g = 0;
for (int i = 1; i < arr.size(); ++i) {
int ff = g + arr[i] * (i / 2 + 1);
int gg = f + arr[i] * ((i + 1) / 2);
f = ff;
g = gg;
ans += f;
}
return ans;
}
};
func sumOddLengthSubarrays(arr []int) (ans int) {
f, g := arr[0], 0
ans = f
for i := 1; i < len(arr); i++ {
ff := g + arr[i]*(i/2+1)
gg := f + arr[i]*((i+1)/2)
f, g = ff, gg
ans += f
}
return
}
function sumOddLengthSubarrays(arr: number[]): number {
const n = arr.length;
let [ans, f, g] = [arr[0], arr[0], 0];
for (let i = 1; i < n; ++i) {
const ff = g + arr[i] * (Math.floor(i / 2) + 1);
const gg = f + arr[i] * Math.floor((i + 1) / 2);
[f, g] = [ff, gg];
ans += f;
}
return ans;
}
impl Solution {
pub fn sum_odd_length_subarrays(arr: Vec<i32>) -> i32 {
let mut ans = arr[0];
let mut f = arr[0];
let mut g = 0;
for i in 1..arr.len() {
let ff = g + arr[i] * ((i as i32) / 2 + 1);
let gg = f + arr[i] * (((i + 1) as i32) / 2);
f = ff;
g = gg;
ans += f;
}
ans
}
}
int sumOddLengthSubarrays(int* arr, int arrSize) {
int ans = arr[0], f = arr[0], g = 0;
for (int i = 1; i < arrSize; ++i) {
int ff = g + arr[i] * (i / 2 + 1);
int gg = f + arr[i] * ((i + 1) / 2);
f = ff;
g = gg;
ans += f;
}
return ans;
}