Skip to content

Commit c431845

Browse files
add 1018
1 parent 4f21ae0 commit c431845

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

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

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|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|
3031
|1013|[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/_1013.java) | O(n) | O(1) | |Easy|
3132
|1002|[Find Common Characters](https://leetcode.com/problems/find-common-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1002.java) | O(n) | O(1) | |Easy|
3233
|999|[Available Captures for Rook](https://leetcode.com/problems/available-captures-for-rook/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_999.java) | O(1) | O(1) | |Easy|
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.math.BigInteger;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 1018. Binary Prefix Divisible By 5
9+
*
10+
* Given an array A of 0s and 1s, consider N_i: the i-th subarray from A[0] to A[i] interpreted as a binary number (from most-significant-bit to least-significant-bit.)
11+
*
12+
* Return a list of booleans answer, where answer[i] is true if and only if N_i is divisible by 5.
13+
*
14+
* Example 1:
15+
*
16+
* Input: [0,1,1]
17+
* Output: [true,false,false]
18+
* Explanation:
19+
* The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
20+
* Example 2:
21+
*
22+
* Input: [1,1,1]
23+
* Output: [false,false,false]
24+
* Example 3:
25+
*
26+
* Input: [0,1,1,1,1,1]
27+
* Output: [true,false,false,false,true,false]
28+
* Example 4:
29+
*
30+
* Input: [1,1,1,0,1]
31+
* Output: [false,false,false,false,false]
32+
*
33+
*
34+
* Note:
35+
*
36+
* 1 <= A.length <= 30000
37+
* A[i] is 0 or 1
38+
* */
39+
public class _1018 {
40+
public static class Solution1 {
41+
/**credit: https://leetcode.com/problems/binary-prefix-divisible-by-5/discuss/266051/Java-beats-100*/
42+
public List<Boolean> prefixesDivBy5(int[] A) {
43+
List<Boolean> result = new ArrayList<>(A.length);
44+
int remainder = 0;
45+
for (int a : A) {
46+
remainder = ((remainder << 1) + a) % 5;
47+
result.add(remainder == 0);
48+
}
49+
return result;
50+
}
51+
}
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1018;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static junit.framework.Assert.assertEquals;
10+
11+
public class _1018Test {
12+
private static _1018.Solution1 solution1;
13+
private static int[] A;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _1018.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
A = new int[]{0, 1, 1};
23+
assertEquals(Arrays.asList(true, false, false), solution1.prefixesDivBy5(A));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
A = new int[]{1, 1, 1};
29+
assertEquals(Arrays.asList(false, false, false), solution1.prefixesDivBy5(A));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
A = new int[]{0, 1, 1, 1, 1, 1};
35+
assertEquals(Arrays.asList(true, false, false, false, true, false), solution1.prefixesDivBy5(A));
36+
}
37+
38+
@Test
39+
public void test4() {
40+
A = new int[]{1, 1, 1, 0, 1};
41+
assertEquals(Arrays.asList(false, false, false, false, false), solution1.prefixesDivBy5(A));
42+
}
43+
44+
@Test
45+
public void test5() {
46+
A = new int[]{1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1};
47+
assertEquals(Arrays.asList(false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, true, true, true, true, false), solution1.prefixesDivBy5(A));
48+
}
49+
50+
}

0 commit comments

Comments
 (0)