Reputation: 7181
Hey guys I am learning data structure. I am trying to solve a problem of leetcode.
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].
I tried my solution and it working fine and I understand all stuff.
My solution
class Solution {
fun runningSum(nums: IntArray): IntArray {
if (nums.size == 1) {
return nums
}
for (index in 1..nums.size - 1) {
nums[index] += nums[index - 1]
}
return nums
}
}
Complexity Analysis
Time complexity: O(n)O(n) where nn is the length of the input array. This is because we use a single loop that iterates over the entire array to calculate the running sum.
Space complexity: O(1)O(1) since we don't use any additional space to find the running sum. Note that we do not take into consideration the space occupied by the output array.
Everything seems ok to me but I went to discuss section and I found one fold solution. So my question is there
Many Thanks.
Upvotes: 1
Views: 1362
Reputation: 11
You can follow anyone.
First Way:
fun runningSum(nums: IntArray): IntArray{
for ( index in 1 until nums.size){
nums[index]+=nums[index-1]
}
return nums
}
Second Way:
fun runningSum(nums: IntArray): IntArray{
var i = 0
nums.fold(0) {sum, element ->
nums[i++] = sum + element
nums[i-1]
}
return nums
}
Third Way
fun runningSum(numbers: IntArray): IntArray {
val numList = numbers.runningFold(0) { sum, num ->
sum + num
}.toMutableList()
numList.removeFirst()
return numList.toIntArray()
}
Upvotes: 0
Reputation: 141
You can do it in one line (line-broke for clarity):
return IntArray(nums.size).also {
// "it" is the IntArray just created
nums.forEachIndexed { i, v ->
it[i] = it.getOrElse(i - 1) { 0 } + v
}
// "it" will be returned here
}
Upvotes: 0
Reputation: 300
There should be no change to the Time Complexity or Space Complexity when going from your solution to the other solution. So foldIndexed
works the same way as fold
with the key difference being that it also has an index. The programmer here chose foldIndexed
because he was looking not for the sum at the end, but for the sum at each stage as it loops through the array, and he wanted to input them into the answers array. Knowing this, we can understand the essence of the code using fold
instead. That'd look something like this:
fun runningSum(nums: IntArray): IntArray {
val answers = IntArray(nums.size)
nums.fold(0) {sum, element ->
sum + element
}
return answers
}
So going piece by piece, 0
is saying we're starting from the index 0
in the nums
IntArray. sum
is what is called an accumulator, which is basically a temporary variable within .fold
which as the name suggests, accumulates as we continue to loop through the terms of the array. It accumulates based on the conditions we set for it within the function, which in this case is to increment by the value of the current element.
I hope that helped:)
Upvotes: 0