I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1588. Sum of All Odd Length Subarrays, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- time: O(N^3)
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are sumOddLengthSubarrays.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 20 ms, faster than 20.00% of C++ online submissions for Sum of All Odd Length Subarrays.
02//Memory Usage: 8.5 MB, less than 40.00% of C++ online submissions for Sum of All Odd Length Subarrays.
03//time: O(N^3)
04class Solution {
05public:
06 int sumOddLengthSubarrays(vector<int>& arr) {
07 int n = arr.size();
08 int ans = 0;
09
10 for(int len = 1; len <= n; len += 2){
11 for(int start = 0; start+len-1 < n; ++start){
12 ans += accumulate(arr.begin()+start, arr.begin()+start+len, 0);
13 }
14 }
15
16 return ans;
17 }
18};
19
20//https://leetcode.com/problems/sum-of-all-odd-length-subarrays/discuss/854184/JavaC%2B%2BPython-O(N)-Time-O(1)-Space
21//Runtime: 4 ms, faster than 89.52% of C++ online submissions for Sum of All Odd Length Subarrays.
22//Memory Usage: 8.3 MB, less than 90.10% of C++ online submissions for Sum of All Odd Length Subarrays.
23//time: O(N), space: O(1)
24class Solution {
25public:
26 int sumOddLengthSubarrays(vector<int>& arr) {
27 int n = arr.size();
28 int ans = 0;
29
30 for(int i = 0; i < n; ++i){
31 //i+1: count of subarrays starting at [0...i]
32 //n-i: count of subarrays ending at [i...n-1]
33 //(i+1)*(n-i): number of subarrays containing i
34 /*
35 in the subarrays containing i,
36 there are odd-length ones and even-length ones,
37 and it's obvious that either:
38 1. number of odd-length ones = number of even-length ones
39 2. number of odd-length ones = number of even-length ones+1,
40 so number of odd-length ones = ((i+1)*(n-i)+1)/2
41 */
42 ans += ((i+1)*(n-i)+1)/2*arr[i];
43 }
44
45 return ans;
46 }
47};
Cost