Skip to content

Commit d79e8df

Browse files
committed
Add Find Pivot Index & Running Sum of 1d Array
1 parent 6a61fcf commit d79e8df

File tree

7 files changed

+105
-0
lines changed

7 files changed

+105
-0
lines changed

README.MD

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetc
2121
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
2222
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
2323
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
24+
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
25+
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |

solutions/pivotIndex/README.MD

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [724. Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)
2+
3+
Given an array of integers `nums`, calculate the pivot index of this array.
4+
5+
The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.
6+
7+
If the index is on the left edge of the array, then the left sum is `0` because there are no elements to the left. This also applies to the right edge of the array.
8+
9+
Return the leftmost pivot index. If no such index exists, return `-1`.

solutions/pivotIndex/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
function pivotIndex(nums) {
6+
const totalSum = nums.reduce((acc, val) => acc + val, 0);
7+
let leftSum = 0;
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (leftSum === totalSum - leftSum - nums[i]) {
11+
return i;
12+
}
13+
14+
leftSum += nums[i];
15+
}
16+
17+
return -1;
18+
}
19+
20+
module.exports = pivotIndex;

solutions/pivotIndex/index.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const pivotIndex = require('.');
2+
3+
describe("Find Pivot Index", () => {
4+
it("should return -1 if the input array is empty", () => {
5+
const result = pivotIndex([]);
6+
expect(result).toEqual(-1);
7+
});
8+
9+
it("should return -1 if there is no pivot index", () => {
10+
const input = [1, 2, 3, 4, 5];
11+
const result = pivotIndex(input);
12+
expect(result).toEqual(-1);
13+
});
14+
15+
it("should return the correct pivot index if there is one", () => {
16+
const input = [1, 7, 3, 6, 5, 6];
17+
const result = pivotIndex(input);
18+
expect(result).toEqual(3);
19+
});
20+
21+
it("should work with arrays containing only one element", () => {
22+
const input = [1];
23+
const result = pivotIndex(input);
24+
expect(result).toEqual(0);
25+
});
26+
});

solutions/runningSum/README.MD

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [1480. Running Sum of 1d Array](https://leetcode.com/problems/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`.

solutions/runningSum/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
function runningSum(nums) {
6+
let sum = 0;
7+
const result = [];
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
sum += nums[i];
11+
result.push(sum);
12+
}
13+
14+
return result;
15+
}
16+
17+
module.exports = runningSum;

solutions/runningSum/index.spec.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const runningSum = require('.');
2+
3+
describe('Running Sum of 1d Array', () => {
4+
it('should return an empty array if given an empty array', () => {
5+
const result = runningSum([]);
6+
expect(result).toEqual([]);
7+
});
8+
9+
it('should return an array with the same length as the input array', () => {
10+
const input = [1, 2, 3, 4, 5];
11+
const result = runningSum(input);
12+
expect(result.length).toEqual(input.length);
13+
});
14+
15+
it('should correctly calculate the running sum of the input array', () => {
16+
const input = [1, 2, 3, 4, 5];
17+
const result = runningSum(input);
18+
expect(result).toEqual([1, 3, 6, 10, 15]);
19+
});
20+
21+
it('should work with arrays containing negative numbers', () => {
22+
const input = [-1, 2, -3, 4, -5];
23+
const result = runningSum(input);
24+
expect(result).toEqual([-1, 1, -2, 2, -3]);
25+
});
26+
});

0 commit comments

Comments
 (0)