Sum of 1d Array
Running Sum of 1d Array
Given an array nums
We defined a running sum of an array as runningSum[i] = sum(nums[0]…nums[i])
.
Return the running sum of nums
.
My solution:
/**
* Overwritten Input Approach
*
* @param {number[]} nums
* @return {number[]}
*/
const runningSum = function (nums) {
for (let i = 1; i < nums.length; i++) {
nums[i] += nums[i - 1];
}
return nums;
};
- Time Complexity: O(n)
- Space Complexity: O(1)
Richest Customer Wealth
You are given an m x n
integer grid accounts where accounts[i][j]
is the amount of money the ith customer has in the jth
bank. Return the wealth that the richest customer has.
A customer’s wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
My solution:
/**
* Find wealthiest account.
*
* @param {number[][]} accounts
* @return {number}
*/
const maximumWealth = function (accounts) {
let max = 0;
accounts.forEach((account) => {
const sum = account.reduce((prev, curr) => prev + curr, 0);
if (sum > max) {
max = sum;
}
});
return max;
};