Movatterモバイル変換


[0]ホーム

URL:


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

Commitcf752f5

Browse files
add 1013
1 parent42d1ba6 commitcf752f5

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Your ideas/fixes/algorithms are more than welcome!
4242
|1020|[Number of Enclaves](https://leetcode.com/problems/number-of-enclaves/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1020.java)| O(mn)| O(mn)||Medium|Graph, DFS, BFS, recursion|
4343
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java)| O(n)| O(1)||Easy|
4444
|1014|[Best Sightseeing Pair](https://leetcode.com/problems/best-sightseeing-pair/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1014.java)| O(n)| O(1)||Medium|
45+
|1013|[Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java)| O(n)| O(1)||Easy|
4546
|1011|[Capacity To Ship Packages Within D Days](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1011.java)| O(nlogn)| O(1)||Medium|Binary Search|
4647
|1010|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1010.java)| O(n)| O(1)||Easy|
4748
|1009|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1009.java)| O(n)| O(1)||Easy|
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 1013. Partition Array Into Three Parts With Equal Sum
5+
*
6+
* Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.
7+
* Formally, we can partition the array if we can find indexes i+1 < j with
8+
* (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])
9+
*
10+
* Example 1:
11+
* Input: [0,2,1,-6,6,-7,9,1,2,0,1]
12+
* Output: true
13+
* Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
14+
*
15+
* Example 2:
16+
* Input: [0,2,1,-6,6,7,9,-1,2,0,1]
17+
* Output: false
18+
*
19+
* Example 3:
20+
* Input: [3,3,6,5,-2,2,5,1,-9,4]
21+
* Output: true
22+
* Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
23+
*
24+
* Note:
25+
*
26+
* 3 <= A.length <= 50000
27+
* -10000 <= A[i] <= 10000*/
28+
publicclass_1013 {
29+
publicstaticclassSolution1 {
30+
publicbooleancanThreePartsEqualSum(int[]A) {
31+
intsum =0;
32+
for (inti =0;i <A.length;i++) {
33+
sum +=A[i];
34+
}
35+
if (sum %3 !=0) {
36+
returnfalse;
37+
}
38+
intequalSum =sum /3;
39+
intleft =0;
40+
intleftSum =0;
41+
while (left <A.length -2 &&leftSum !=equalSum) {
42+
leftSum +=A[left++];
43+
}
44+
if (left >A.length -2 ||leftSum !=equalSum) {
45+
returnfalse;
46+
}
47+
48+
intright =A.length -1;
49+
intrightSum =0;
50+
while (right >left &&rightSum !=equalSum) {
51+
rightSum +=A[right--];
52+
}
53+
if (right <left ||rightSum !=equalSum) {
54+
returnfalse;
55+
}
56+
intmiddleSum =0;
57+
for (inti =left;i <=right;i++) {
58+
middleSum +=A[i];
59+
}
60+
returnmiddleSum ==equalSum;
61+
}
62+
}
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1013;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.Assert.assertEquals;
8+
9+
publicclass_1013Test {
10+
privatestatic_1013.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1013.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.canThreePartsEqualSum(newint[]{0,2,1, -6,6, -7,9,1,2,0,1}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(false,solution1.canThreePartsEqualSum(newint[]{0,2,1, -6,6,7,9, -1,2,0,1}));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(true,solution1.canThreePartsEqualSum(newint[]{3,3,6,5, -2,2,5,1, -9,4}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp