1588. Sum of All Odd Length Subarrays Easy
1/**
2 * [1588] Sum of All Odd Length Subarrays
3 *
4 * Given an array of positive integers arr, return the sum of all possible odd-length subarrays of arr.
5 * A subarray is a contiguous subsequence of the array.
6 *
7 * Example 1:
8 *
9 * Input: arr = [1,4,2,5,3]
10 * Output: 58
11 * Explanation: The odd-length subarrays of arr and their sums are:
12 * [1] = 1
13 * [4] = 4
14 * [2] = 2
15 * [5] = 5
16 * [3] = 3
17 * [1,4,2] = 7
18 * [4,2,5] = 11
19 * [2,5,3] = 10
20 * [1,4,2,5,3] = 15
21 * If we add all these together we get 1 + 4 + 2 + 5 + 3 + 7 + 11 + 10 + 15 = 58
22 * Example 2:
23 *
24 * Input: arr = [1,2]
25 * Output: 3
26 * Explanation: There are only 2 subarrays of odd length, [1] and [2]. Their sum is 3.
27 * Example 3:
28 *
29 * Input: arr = [10,11,12]
30 * Output: 66
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= arr.length <= 100
36 * 1 <= arr[i] <= 1000
37 *
38 *
39 * Follow up:
40 * Could you solve this problem in O(n) time complexity?
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/sum-of-all-odd-length-subarrays/
46// discuss: https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn sum_odd_length_subarrays(arr: Vec<i32>) -> i32 {
52 0
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_1588() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.