返回介绍

solution / 1800-1899 / 1826.Faulty Sensor / README_EN

发布于 2024-06-17 01:03:14 字数 5704 浏览 0 评论 0 收藏 0

1826. Faulty Sensor

中文文档

Description

An experiment is being conducted in a lab. To ensure accuracy, there are two sensors collecting data simultaneously. You are given two arrays 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] (the last position can be any value, not just 7).

We know that there is a defect in at most one of the sensors. Return _the sensor number (_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_._

 

Example 1:

Input: sensor1 = [2,3,4,5], sensor2 = [2,1,3,4]
Output: 1
Explanation: Sensor 2 has the correct values.
The second data point from sensor 2 is dropped, and the last value of sensor 1 is replaced by a 5.

Example 2:

Input: sensor1 = [2,2,2,2,2], sensor2 = [2,2,2,2,5]
Output: -1
Explanation: It is impossible to determine which sensor has a defect.
Dropping the last value for either sensor could produce the output for the other sensor.

Example 3:

Input: sensor1 = [2,3,2,2,3,2], sensor2 = [2,3,2,3,2,7]
Output: 2
Explanation: Sensor 1 has the correct values.
The fourth data point from sensor 1 is dropped, and the last value of sensor 1 is replaced by a 7.

 

Constraints:

  • sensor1.length == sensor2.length
  • 1 <= sensor1.length <= 100
  • 1 <= sensor1[i], sensor2[i] <= 100

Solutions

Solution 1: Traversal

Traverse both arrays, find the first unequal position $i$. If $i \lt n - 1$, loop to compare $sensor1[i + 1]$ and $sensor2[i]$, if they are not equal, it indicates that sensor $1$ is defective, return $1$; otherwise compare $sensor1[i]$ and $sensor2[i + 1]$, if they are not equal, it indicates that sensor $2$ is defective, return $2$.

If the traversal ends, it means that the defective sensor cannot be determined, return $-1$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def badSensor(self, sensor1: List[int], sensor2: List[int]) -> int:
    i, n = 0, len(sensor1)
    while i < n - 1:
      if sensor1[i] != sensor2[i]:
        break
      i += 1
    while i < n - 1:
      if sensor1[i + 1] != sensor2[i]:
        return 1
      if sensor1[i] != sensor2[i + 1]:
        return 2
      i += 1
    return -1
class Solution {
  public int badSensor(int[] sensor1, int[] sensor2) {
    int i = 0;
    int n = sensor1.length;
    for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {
    }
    for (; i < n - 1; ++i) {
      if (sensor1[i + 1] != sensor2[i]) {
        return 1;
      }
      if (sensor1[i] != sensor2[i + 1]) {
        return 2;
      }
    }
    return -1;
  }
}
class Solution {
public:
  int badSensor(vector<int>& sensor1, vector<int>& sensor2) {
    int i = 0;
    int n = sensor1.size();
    for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {}
    for (; i < n - 1; ++i) {
      if (sensor1[i + 1] != sensor2[i]) return 1;
      if (sensor1[i] != sensor2[i + 1]) return 2;
    }
    return -1;
  }
};
func badSensor(sensor1 []int, sensor2 []int) int {
  i, n := 0, len(sensor1)
  for ; i < n-1 && sensor1[i] == sensor2[i]; i++ {
  }
  for ; i < n-1; i++ {
    if sensor1[i+1] != sensor2[i] {
      return 1
    }
    if sensor1[i] != sensor2[i+1] {
      return 2
    }
  }
  return -1
}
function badSensor(sensor1: number[], sensor2: number[]): number {
  let i = 0;
  const n = sensor1.length;
  while (i < n - 1) {
    if (sensor1[i] !== sensor2[i]) {
      break;
    }
    ++i;
  }
  while (i < n - 1) {
    if (sensor1[i + 1] !== sensor2[i]) {
      return 1;
    }
    if (sensor1[i] !== sensor2[i + 1]) {
      return 2;
    }
    ++i;
  }
  return -1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文