Code Logo

Running Sum of Purchases

Published atDate not available
Easy 0 views
Like0

This problem feels like a little puzzle you can solve one step at a time. In Running Sum of Purchases, you are trying to work toward the right list by following one clear idea.

This one is about building the best total or working out a final amount. You may need to choose which values should be included and which ones should be skipped. Sometimes the biggest number is not the smartest first choice if it hurts the rest of the plan. The goal is to finish with the best overall total, not just one good moment.

For example, if the input is nums = [1,2,3,4], the answer is [1,3,6,10]. The running totals after each purchase are 1, 3, 6, and 10. Another example is nums = [1,1,1,1,1], which gives [1,2,3,4,5]. Each purchase adds 1, so the running sum increments steadily.

This is a friendly practice problem, but it still rewards careful reading. The key is thinking about the whole plan, not only one choice at a time.

Example Input & Output

Example 1
Input
nums = [1,2,3,4]
Output
[1,3,6,10]
Explanation

The running totals after each purchase are 1, 3, 6, and 10.

Example 2
Input
nums = [1,1,1,1,1]
Output
[1,2,3,4,5]
Explanation

Each purchase adds 1, so the running sum increments steadily.

Example 3
Input
nums = [3,1,2,10,1]
Output
[3,4,6,16,17]
Explanation

The cumulative totals reflect how each purchase increases the balance.

Algorithm Flow

Recommendation Algorithm Flow for Running Sum of Purchases
Recommendation Algorithm Flow for Running Sum of Purchases

Best Answers

java
import java.util.*;
class Solution {
    public Object running_sum(Object nums) {
        int[] arr = (int[]) nums;
        List<Integer> r = new ArrayList<>(); int t = 0;
        for (int n : arr) { t += n; r.add(t); }
        return r;
    }
}