So I have been learning Dynamic Programming (DP) recently and when I came upon the following problem, I decided to use DP but since I'm a beginner in algorithms, I am not sure if this is a valid example of DP or not.
Problem:
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.
Example 1:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
This is my "DP" solution:
class Solution {
public int[] runningSum(int[] nums) {
int[] arr = new int[nums.length];
int sum = 0;
for(int i = 0; i < nums.length; i++){
arr[i] = nums[i] + sum;
sum += nums[i];
}
return arr;
}
}