Array Reduce Transformation — LeetCode 2626 Python Solution

EasyJavaScript
Problem
#2626
Reading time
2 min

The problem

Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element. This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ...

Example

Input
nums = [1,2,3,4]
Output
10
Explanation
initially, the value is init=0.

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2626. Array Reduce Transformation?
LeetCode 2626. Array Reduce Transformation is rated Easy on LeetCode.
What topics does LeetCode 2626. Array Reduce Transformation cover?
LeetCode 2626. Array Reduce Transformation is tagged JavaScript on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview