[LeetCode] Faulty Sensor

1826. Faulty Sensor

sensor1 and sensor2, where sensor1[i] and sensor2[i] are the ith data points collected by the two sensors.

However, this type of sensor has a chance of being defective, which causes exactly one data point to be dropped. After the data is dropped, all the data points to the right of the dropped data are shifted one place to the left, and the last data point is replaced with some random value. It is guaranteed that this random value will not be equal to the dropped value.

  • For example, if the correct data is [1,2,**3**,4,5] and 3 is dropped, the sensor could return [1,2,4,5,**7**]``7).

1 or 2) with the defect. If there is no defect in either sensor or if it is impossible to determine the defective sensor, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int badSensor(vector<int>& a, vector<int>& b) {
for(int i = 0; i < a.size() - 1; i++) {
if(a[i] == b[i]) continue;
int res = 3;
for(int x = i + 1, y = i; x < a.size() and y < b.size(); x++,y++) {
if(a[x] != b[y]) {
res ^= 2;
break;
}
}
for(int x = i, y = i+1; x < a.size() and y < b.size(); x++,y++) {
if(a[x] != b[y]) {
res ^= 1;
break;
}
}

return res == 0 or res == 3 ? -1 : res;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/05/16/PS/LeetCode/faulty-sensor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.