Skip to content

Commit 37b806b

Browse files
add 1826
1 parent 8a4b97d commit 37b806b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ _If you like this project, please leave me a star._ ★
3636
|1829|[Maximum XOR for Each Query](https://leetcode.com/problems/maximum-xor-for-each-query/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1829.java) ||Medium|Bit Manipulation|
3737
|1828|[Queries on Number of Points Inside a Circle](https://leetcode.com/problems/queries-on-number-of-points-inside-a-circle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1828.java) |[:tv:](https://youtu.be/fU4oOBSsVMg)|Medium|Math|
3838
|1827|[Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1827.java) ||Easy|Array, Greedy|
39+
|1826|[Faulty Sensor](https://leetcode.com/problems/faulty-sensor/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1826.java) ||Easy|Array, Two Pointers|
3940
|1823|[Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1823.java) ||Medium|Array|
4041
|1822|[Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1822.java) ||Easy|Math|
4142
|1817|[Finding the Users Active Minutes](https://leetcode.com/problems/finding-the-users-active-minutes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1817.java) ||Medium|HashTable|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1826 {
4+
public static class Solution1 {
5+
public int badSensor(int[] sensor1, int[] sensor2) {
6+
//check if sensor2 is faulty
7+
int i = 0, j = 0;
8+
for (; i < sensor1.length && j < sensor2.length - 1; ) {
9+
if (sensor1[i] != sensor2[j]) {
10+
i++;
11+
} else {
12+
i++;
13+
j++;
14+
}
15+
}
16+
boolean sensor2Faulty = false;
17+
if (j == sensor2.length - 1 && i == sensor1.length) {
18+
sensor2Faulty = true;
19+
}
20+
//check sensor1
21+
i = 0;
22+
j = 0;
23+
for (; i < sensor1.length - 1 && j < sensor2.length; ) {
24+
if (sensor1[i] != sensor2[j]) {
25+
j++;
26+
} else {
27+
i++;
28+
j++;
29+
}
30+
}
31+
boolean sensor1Faulty = false;
32+
if (i == sensor1.length - 1 && j == sensor2.length) {
33+
sensor1Faulty = true;
34+
}
35+
if (sensor1Faulty && sensor2Faulty) {
36+
return -1;
37+
} else if (sensor1Faulty) {
38+
return 1;
39+
} else if (sensor2Faulty) {
40+
return 2;
41+
}
42+
return -1;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1826;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1826Test {
10+
private static _1826.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1826.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.badSensor(new int[]{2, 3, 4, 5}, new int[]{2, 1, 3, 4}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.badSensor(new int[]{2, 2, 2, 2, 2}, new int[]{2, 2, 2, 2, 5}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.badSensor(new int[]{2, 3, 2, 2, 3, 2}, new int[]{2, 3, 2, 3, 2, 7}));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(-1, solution1.badSensor(new int[]{1, 2, 3, 2, 3, 2}, new int[]{1, 2, 3, 3, 2, 3}));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)