Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 026157a

Browse files
1480: Running Sum of 1d Array
1 parent ef9f84e commit 026157a

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ My solutions to LeetCode problems in Kotlin.
2323
| [344](https://leetcode.com/problems/reverse-string/) | [Reverse String](src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt) | Easy |
2424
| [773](https://leetcode.com/problems/sliding-puzzle/) | [Sliding Puzzle](src/main/kotlin/com/schmoczer/leetcode/_0773/SlidingPuzzle.kt) | Hard |
2525
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
26+
| [1480](https://leetcode.com/problems/running-sum-of-1d-array/) | [Running Sum of 1d Array](src/main/kotlin/com/schmoczer/leetcode/_1480/RunningSumOf1dArray.kt) | Easy |
2627
| [1574](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/) | [Shortest Subarray to be Removed to Make Array Sorted](src/main/kotlin/com/schmoczer/leetcode/_1574/ShortestSubarray.kt) | Medium |
2728
| [1829](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Maximum XOR for Each Query](src/main/kotlin/com/schmoczer/leetcode/_1829/MaximumXorForEachQuery.kt) | Medium |
2829
| [1861](https://leetcode.com/problems/rotating-the-box/) | [Rotating the Box](src/main/kotlin/com/schmoczer/leetcode/_1861/RotatingTheBox.kt) | Medium |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Running Sum of 1d Array
2+
3+
Given an array `nums`. We define a running sum of an array as `runningSum[i] = sum(nums[0]...nums[i])`.
4+
5+
Return the running sum of `nums`.
6+
7+
Example 1:
8+
9+
> Input: nums = [1,2,3,4]
10+
>
11+
> Output: [1,3,6,10]
12+
13+
Example 2:
14+
15+
> Input: nums = [1,1,1,1,1]
16+
>
17+
> Output: [1,2,3,4,5]
18+
19+
Example 3:
20+
21+
> Input: nums = [3,1,2,10,1]
22+
>
23+
> Output: [3,4,6,16,17]
24+
25+
Constraints:
26+
27+
- `1 <= nums.length <= 1000`
28+
- `-10^6 <= nums[i] <= 10^6`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.schmoczer.leetcode._1480
2+
3+
class RunningSumOf1dArray {
4+
// Runtime 0ms Beats 100.00%
5+
fun runningSum(nums: IntArray): IntArray {
6+
val sums = IntArray(nums.size)
7+
var currentSum = 0
8+
for (index in 0..<nums.size) {
9+
currentSum += nums[index]
10+
sums[index] = currentSum
11+
}
12+
return sums
13+
}
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.schmoczer.leetcode._1480
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class RunningSumOf1dArrayTest {
8+
private lateinit var sut: RunningSumOf1dArray
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = RunningSumOf1dArray()
13+
}
14+
15+
@Test
16+
fun `given 1,2,3,4 then the running sum is 1,3,6,10`() {
17+
val input = intArrayOf(1, 2, 3, 4)
18+
val expected = intArrayOf(1, 3, 6, 10)
19+
20+
val result = sut.runningSum(input)
21+
22+
assertContentEquals(expected, result)
23+
}
24+
25+
@Test
26+
fun `given 1,1,1,1,1 then the running sum is 1,2,3,4,5`() {
27+
val input = intArrayOf(1, 1, 1, 1, 1)
28+
val expected = intArrayOf(1, 2, 3, 4, 5)
29+
30+
val result = sut.runningSum(input)
31+
32+
assertContentEquals(expected, result)
33+
}
34+
35+
@Test
36+
fun `given 3,1,2,10,1 then the running sum is 3,4,6,16,17`() {
37+
val input = intArrayOf(3, 1, 2, 10, 1)
38+
val expected = intArrayOf(3, 4, 6, 16, 17)
39+
40+
val result = sut.runningSum(input)
41+
42+
assertContentEquals(expected, result)
43+
}
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /