Skip to content

Commit c43bd39

Browse files
[N-0] add 724
1 parent 510b2c0 commit c43bd39

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_724.java) | O(n) | O(n) | Easy | Array
2526
|723|[Candy Crush](https://leetcode.com/problems/candy-crush/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_723.java) | O((r*c)^2) | O((r*c)) | Medium | Array, Two Pointers
2627
|721|[Accounts Merge](https://leetcode.com/problems/accounts-merge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_721.java) | | | Medium | DFS, Union Find
2728
|720|[Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_720.java) | O(∑wi) where wi is the length of words[i] | O(∑wi) where wi is the length of words[i] | Easy | Trie
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 724. Find Pivot Index
5+
*
6+
* Given an array of integers nums, write a method that returns the "pivot" index of this array.
7+
* We define the pivot index as the index where the sum of the numbers to the left of the index is equal
8+
* to the sum of the numbers to the right of the index.
9+
* If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
10+
11+
Example 1:
12+
Input:
13+
nums = [1, 7, 3, 6, 5, 6]
14+
Output: 3
15+
Explanation:
16+
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
17+
Also, 3 is the first index where this occurs.
18+
19+
Example 2:
20+
Input:
21+
nums = [1, 2, 3]
22+
Output: -1
23+
Explanation:
24+
There is no index that satisfies the conditions in the problem statement.
25+
26+
Note:
27+
The length of nums will be in the range [0, 10000].
28+
Each element nums[i] will be an integer in the range [-1000, 1000].
29+
*/
30+
public class _724 {
31+
public static class Solution1 {
32+
public int pivotIndex(int[] nums) {
33+
if (nums == null || nums.length == 0) {
34+
return -1;
35+
}
36+
int[] sums = new int[nums.length];
37+
sums[0] = nums[0];
38+
for (int i = 1; i < nums.length; i++) {
39+
sums[i] = sums[i - 1] + nums[i];
40+
}
41+
int result = -1;
42+
for (int i = 0; i < nums.length; i++) {
43+
if (i == 0 && 0 == sums[nums.length - 1] - sums[i] || (i > 0 && sums[i - 1] == sums[nums.length - 1] - sums[i])) {
44+
result = i;
45+
break;
46+
}
47+
}
48+
return result;
49+
}
50+
}
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._724;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _724Test {
10+
private static _724.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _724.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 7, 3, 6, 5, 6};
21+
assertEquals(3, solution1.pivotIndex(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[]{1, 2, 3};
27+
assertEquals(-1, solution1.pivotIndex(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[]{-1, -1, -1, 0, 1, 1};
33+
assertEquals(0, solution1.pivotIndex(nums));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
nums = new int[]{-1, -1, 0, 1, 1, 0};
39+
assertEquals(5, solution1.pivotIndex(nums));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)