C# 数组上的前向和后向搜索算法

C# 数组上的前向和后向搜索算法,c#,arrays,algorithm,search,iteration,C#,Arrays,Algorithm,Search,Iteration,我正在研究一个简单的算法问题以供实践,我试图找出为什么在大约20%的测试用例中它失败了。因此,问题是,给定一个整数数组,查找数组中所有有效整数的平均值 如果 它大于或等于-273 前两个或后两个整数中至少有一个距离当前整数有两点 如果整数无效,则不应将其包括在计算平均值中。此外,我认为问题不希望解决方案是循环的(不确定,尽管写这篇文章时只是考虑了一下,所以会尝试),也就是说,如果你在第一个int数组[0],那么就没有前两个int,而最后两个int是循环数组中的前两个 我的策略总结如下: publ

我正在研究一个简单的算法问题以供实践,我试图找出为什么在大约20%的测试用例中它失败了。因此,问题是,给定一个整数数组,查找数组中所有有效整数的平均值

如果

  • 它大于或等于-273
  • 前两个或后两个整数中至少有一个距离当前整数有两点
  • 如果整数无效,则不应将其包括在计算平均值中。此外,我认为问题不希望解决方案是循环的(不确定,尽管写这篇文章时只是考虑了一下,所以会尝试),也就是说,如果你在第一个int数组[0],那么就没有前两个int,而最后两个int是循环数组中的前两个

    我的策略总结如下:

    public double averageTemperature(int[] measuredValues)
    {
        Queue<int> qLeft = new Queue<int>(2);
        Queue<int> qRight = new Queue<int>(2);
    
        double sum = 0d;
        int cnt = 0;
    
        for (int i = 0; i < measuredValues.Length; i++)
        {
            if (measuredValues[i] < -273)
                continue;
            if (qLeft.Count == 3)
                qLeft.Dequeue();
            for (int j = i + 1; j < measuredValues.Length; j++)
            {
                if (qRight.Count == 2)
                {
                    break;
                }
                qRight.Enqueue(measuredValues[j]);
            }
    
            if (b(qLeft, qRight, measuredValues[i]) == true)
            {
                sum += measuredValues[i];
                cnt++;
                qLeft.Enqueue(measuredValues[i]);
            }
    
    
            qRight.Clear();
        }
    
        if (cnt > 0)
            return sum / cnt;
        return -300.0;
    }
    bool b(Queue<int> a, Queue<int> b, int c)
    {
        foreach (int q in a)
        {
            if (Math.Abs(q - c) <= 2)
                return true;
        }
        foreach (int w in b)
        {
            if (Math.Abs(w - c) <= 2)
                return true;
        }
        return false;
    }
    
    我不明白为什么。我错过了一些明显的东西?我知道它们可能是解决这个问题的另一种更有效的方法,但我不想尝试它们,直到我知道为什么我的“天真”方法不起作用

    我终于明白为什么要感谢你们了。以下是我修订后的代码,供那些可能觉得有用的人使用:

    public double averageTemperature(int[] measuredValues)
        {
            Queue<int> qLeft = new Queue<int>(2);
            Queue<int> qRight = new Queue<int>(2);
    
            double sum = 0d;
            int cnt = 0;
    
    
            for (int i = 0; i < measuredValues.Length; i++)
            {
                if (qLeft.Count == 3)
                    qLeft.Dequeue();
                for (int j = i + 1; j < measuredValues.Length; j++)
                  {
                    if (qRight.Count == 2)
                    {
                        break;
                    }
                    qRight.Enqueue(measuredValues[j]);
                }
    
                if (isValid(qLeft, qRight, measuredValues[i]) == true)
                {
                    sum += measuredValues[i];
                    cnt++;
    
                }
                qLeft.Enqueue(measuredValues[i]);
                qRight.Clear();
            }
    
            if (cnt > 0)
                return sum / cnt;
            return -300.0;
        }
        bool isValid(Queue<int> a, Queue<int> b, int c)
        {
    
            foreach (int q in a)
            {
                if (c >=-273 && Math.Abs(q - c) <= 2)
                    return true;
            }
            foreach (int w in b)
            {
                if (c >=-273 && Math.Abs(w - c) <= 2)
                    return true;
            }
            return false;
        }
    
    公共双平均温度(int[]测量值)
    {
    队列qLeft=新队列(2);
    Queue qRight=新队列(2);
    双和=0d;
    int-cnt=0;
    对于(int i=0;i0)
    返回和/cnt;
    回报率-300.0;
    }
    bool是有效的(队列a、队列b、整数c)
    {
    foreach(a中的int q)
    {
    
    如果(c>=-273&&Math.Abs(q-c)=-273&&Math.Abs(w-c)在比较时尝试从嵌套for()循环中的同一点开始。例如:运行它时会得到什么

    public double averageTemperature(int[] measuredValues)
    {
    Queue<int> qLeft = new Queue<int>(2);
    Queue<int> qRight = new Queue<int>(2);
    
    double sum = 0d;
    int cnt = 0;
    
    for (int i = 0; i < measuredValues.Length; i++)
    {
        if (measuredValues[i] < -273)
            continue;
        if (qLeft.Count == 3)
            qLeft.Dequeue();
        for (int j = 0; j < measuredValues.Length; j++)
        {
            if (qRight.Count == 2)
            {
                break;
            }
            qRight.Enqueue(measuredValues[j]);
        }
    
        if (b(qLeft, qRight, measuredValues[i]) == true)
        {
            sum += measuredValues[i];
            cnt++;
            qLeft.Enqueue(measuredValues[i]);
        }
    
    
        qRight.Clear();
    }
    
    if (cnt > 0)
        return sum / cnt;
    return -300.0;
    }
    bool b(Queue<int> a, Queue<int> b, int c)
    {
    foreach (int q in a)
    {
        if (Math.Abs(q - c) <= 2)
            return true;
    }
    foreach (int w in b)
    {
        if (Math.Abs(w - c) <= 2)
            return true;
    }
    return false;
    }
    
    公共双平均温度(int[]测量值)
    {
    队列qLeft=新队列(2);
    Queue qRight=新队列(2);
    双和=0d;
    int-cnt=0;
    对于(int i=0;i0)
    返回和/cnt;
    回报率-300.0;
    }
    布尔b(队列a、队列b、整数c)
    {
    foreach(a中的int q)
    {
    
    if(Math.Abs(q-c)仅当数组中的当前值有效时,您才进入qLeft队列,但这是不对的。您需要在由
    i
    控制的外部for循环的每次迭代时进入qLeft队列

    请参阅以下代码:

    for (int i = 0; i < measuredValues.Length; i++)
    {
        if (measuredValues[i] < -273)
            continue;
        if (qLeft.Count == 3)
            qLeft.Dequeue();
        for (int j = i + 1; j < measuredValues.Length; j++)
        {
            if (qRight.Count == 2)
            {
                break;
            }
            qRight.Enqueue(measuredValues[j]);
        }
    
        if (b(qLeft, qRight, measuredValues[i]) == true)
        {
            sum += measuredValues[i];
            cnt++;
        }
    
        qLeft.Enqueue(measuredValues[i]); // YOU NEED TO ENQUEUE INTO qLeft EACH TIME REGARDLESS OF IT IS VALID OR INVALID
        qRight.Clear();
    }
    
    for(int i=0;i
    是“273”还是“-273”?根据您的测试用例,我猜应该是“-273”。我不确定“两点”是什么意思,但您给出的数据集不包含任何元素,其中后面或前面的元素2==当前元素的值+/-2。@Ormaj让我解释一下,-13是有效的,因为至少有一个元素最多相隔2个元素,它最多大于或小于-13 2个点。因此,12显然不是距离-13 2个点,而是-14只是距离-13 1点。因此-13是有效的。如果-13前面的两个元素都大于+/-2点,则-13将无效。队列右侧的当前整数右侧最多应有两个整数。因此,从0开始肯定不起作用,然后从i+2开始,而不是从i+1开始,然后看看效果如何。同样尝试过,所有无效整数都应为di在计算平均值之前删除。将无效int添加到队列左侧将扭曲结果。在b()方法中,始终丢弃有效int。在条件1(<-273)中失败的无效int将在外循环中的第一个if()之后立即丢弃。在条件2中失败的无效int将在b()中丢弃那么,为什么需要限制qLeft只在中插入有效的int?
    for (int i = 0; i < measuredValues.Length; i++)
    {
        if (measuredValues[i] < -273)
            continue;
        if (qLeft.Count == 3)
            qLeft.Dequeue();
        for (int j = i + 1; j < measuredValues.Length; j++)
        {
            if (qRight.Count == 2)
            {
                break;
            }
            qRight.Enqueue(measuredValues[j]);
        }
    
        if (b(qLeft, qRight, measuredValues[i]) == true)
        {
            sum += measuredValues[i];
            cnt++;
        }
    
        qLeft.Enqueue(measuredValues[i]); // YOU NEED TO ENQUEUE INTO qLeft EACH TIME REGARDLESS OF IT IS VALID OR INVALID
        qRight.Clear();
    }