-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.ts
More file actions
76 lines (69 loc) · 2.14 KB
/
solution.ts
File metadata and controls
76 lines (69 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* LeetCode Problem #1480 - Running Sum of 1d Array
*
* Problem: Given an array nums, return the running sum of nums.
* The running sum of an array is defined as runningSum[i] = sum(nums[0]…nums[i]).
*
* @see https://leetcode.com/problems/running-sum-of-1d-array/
*/
/**
* Approach 1: In-Place Prefix Sum
*
* This solution modifies the input array in-place by adding the previous
* element's value to the current element, building up the running sum.
*
* Time Complexity: O(n) - We iterate through the array once
* Space Complexity: O(1) - We modify the array in-place
*
* @param nums - The input array of integers
* @returns The running sum array
*
* @example
* // Returns [1, 3, 6, 10]
* runningSum([1, 2, 3, 4]);
*
* @example
* // Returns [1, 2, 3, 4, 5]
* runningSum([1, 1, 1, 1, 1]);
*/
export function runningSum(nums: number[]): number[] {
// Handle edge case: empty array (though constraints guarantee length >= 1)
if (!nums || nums.length === 0) {
return [];
}
// Start from index 1 since the first element's running sum is itself
for (let i = 1; i < nums.length; i++) {
// Add the previous running sum to the current element
nums[i] += nums[i - 1];
}
return nums;
}
/**
* Approach 2: New Array (Non-destructive)
*
* This solution creates a new array to store the running sum,
* preserving the original input array.
*
* Time Complexity: O(n) - We iterate through the array once
* Space Complexity: O(n) - We create a new array of the same size
*
* @param nums - The input array of integers
* @returns A new array containing the running sum
*
* @example
* // Returns [3, 4, 6, 16, 17]
* runningSumNonDestructive([3, 1, 2, 10, 1]);
*/
export function runningSumNonDestructive(nums: number[]): number[] {
// Handle edge case: empty array (though constraints guarantee length >= 1)
if (!nums || nums.length === 0) {
return [];
}
// Initialize result array with the first element
const result: number[] = [nums[0]];
// Build the running sum by adding current element to previous sum
for (let i = 1; i < nums.length; i++) {
result[i] = result[i - 1] + nums[i];
}
return result;
}