📜  到每个元素的最接近零的距离

📅  最后修改于: 2022-05-13 01:57:50.051000             🧑  作者: Mango

到每个元素的最接近零的距离

给定一个包含 n 个整数的数组,对于每个元素,打印到最接近零的距离。数组中至少有 1 个零。
例子:

Input: 5 6 0 1 -2 3 4
Output: 2 1 0 1 2 3 4 
Explanation : The nearest 0(indexed 2) to 
5(indexed 0) is at a distance of 2, so we 
print 2. Same is done for the rest of elements.

朴素的方法:一种朴素的方法是,对于每个元素,向左滑动并找出最接近的 0,然后再次向右滑动以找出最接近的零(如果有),并打印两个距离的最小值。它会节省空间,但时间复杂度会很高,因为我们必须对每个元素进行迭代,直到找到 0,在最坏的情况下,我们可能无法找到一个方向。
时间复杂度:O(n^2)
辅助空间: O(1)有效方法:一种有效的方法是两次使用滑动窗口技术。一种是从右向左遍历,另一种是从左向右遍历。
用最大值初始化 ans[0]。从左到右遍历数组。如果当前位置的值为0,则将距离设置为0,否则将距离增加1。在每一步中,将距离值写入答案数组。
做同样的事情,但从右到左。这将在右侧找到最接近的零。现在我们应该存储距离的当前值的最小值和已经在答案数组中的值。
下面是上述方法的实现。

C++
// CPP program to find closest 0 for every element
#include 
using namespace std;
 
// Print the distance with zeroes of every element
void print_distance(int arr[], int n)
{
    // initializes an array of size n with 0
    int ans[n];
    memset(arr, 0, sizeof(arr));
 
    // if first element is 0 then the distance
    // will be 0
    if (arr[0] == 0)
        ans[0] = 0;
    else
        ans[0] = INT_MAX; // if not 0 then initialize
                          // with a maximum value
 
    // traverse in loop from 1 to n and store
    // the distance from left
    for (int i = 1; i < n; ++i) {
 
        // add 1 to the distance from previous one
        ans[i] = ans[i - 1] + 1;
 
        // if the present element is 0 then distance
        // will be 0
        if (arr[i] == 0)
            ans[i] = 0;       
    }
 
    // if last element is zero then it will be 0 else
    // let the answer be what was found when traveled
    // from left to right
    if (arr[n - 1] == 0)
        ans[n - 1] = 0;
 
    // traverse from right to left and store the minimum
    // of distance if found from right to left or left
    // to right
    for (int i = n - 2; i >= 0; --i) {
 
        // store the minimum of distance from left to
        // right or right to left
        ans[i] = min(ans[i], ans[i + 1] + 1);
 
        // if it is 0 then minimum will always be 0
        if (arr[i] == 0)
            ans[i] = 0;
    }
 
    // print the answer array
    for (int i = 0; i < n; ++i)
        cout << ans[i] << " ";   
}
 
// driver program to test the above function
int main()
{
    int a[] = { 2, 1, 0, 3, 0, 0, 3, 2, 4 };
    int n = sizeof(a) / sizeof(a[0]);
    printDistances(a, n);
    return 0;
}


Java
// Java program to find closest
// 0 for every element
import java.util.Arrays;
 
class GFG
{
    // Print the distance with zeroes of every element
    static void print_distance(int arr[], int n)
    {
        // initializes an array of size n with 0
        int ans[]=new int[n];
        Arrays.fill(ans,0);
     
        // if first element is 0 then the distance
        // will be 0
        if (arr[0] == 0)
            ans[0] = 0;
         
        // if not 0 then initialize
        // with a maximum value   
        else
            ans[0] = +2147483647;
     
        // traverse in loop from 1 to n and store
        // the distance from left
        for (int i = 1; i < n; ++i)
        {
     
            // add 1 to the distance
            // from previous one
            ans[i] = ans[i - 1] + 1;
     
            // if the present element is
            // 0 then distance will be 0
            if (arr[i] == 0)
                ans[i] = 0;    
        }
     
        // if last element is zero
        // then it will be 0 else
        // let the answer be what was
        // found when traveled
        // from left to right
        if (arr[n - 1] == 0)
            ans[n - 1] = 0;
     
        // traverse from right to
        // left and store the minimum
        // of distance if found from
        // right to left or left
        // to right
        for (int i = n - 2; i >= 0; --i)
        {
     
            // store the minimum of distance
            // from left to right or right to left
            ans[i] = Math.min(ans[i], ans[i + 1] + 1);
     
            // if it is 0 then minimum
            // will always be 0
            if (arr[i] == 0)
                ans[i] = 0;
        }
     
        // print the answer array
        for (int i = 0; i < n; ++i)
            System.out.print(ans[i] + " ");
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int a[] = { 2, 1, 0, 3, 0, 0, 3, 2, 4 };
        int n = a.length;
        print_distance(a, n);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find closest 0
# for every element
 
# Print the distance with zeroes of
# every element
def print_distance(arr, n):
     
    # initializes an array of size n with 0
    ans = [0 for i in range(n)]
     
    # if first element is 0 then the
    # distance will be 0
    if (arr[0] == 0):
        ans[0] = 0
    else:
        ans[0] = 10**9  # if not 0 then initialize
                        # with a maximum value
 
    # traverse in loop from 1 to n and
    # store the distance from left
    for i in range(1, n):
 
        # add 1 to the distance from
        # previous one
        ans[i] = ans[i - 1] + 1
 
        # if the present element is 0 then
        # distance will be 0
        if (arr[i] == 0):
            ans[i] = 0
 
    # if last element is zero then it will be 0
    # else let the answer be what was found when
    # traveled from left to right
    if (arr[n - 1] == 0):
        ans[n - 1] = 0
 
    # traverse from right to left and store
    # the minimum of distance if found from
    # right to left or left to right
    for i in range(n - 2, -1, -1):
 
        # store the minimum of distance from
        # left to right or right to left
        ans[i] = min(ans[i], ans[i + 1] + 1)
 
        # if it is 0 then minimum will
        # always be 0
        if (arr[i] == 0):
            ans[i] = 0
     
    # print the answer array
    for i in ans:
        print(i, end = " ")
 
# Driver Code
a = [2, 1, 0, 3, 0, 0, 3, 2, 4]
n = len(a)
print_distance(a, n)
 
# This code is contributed
# by Mohit Kumar


C#
// C# program to find closest
// 0 for every element
using System;
class GFG
{
    // Print the distance with zeroes of every element
    static void print_distance(int []arr, int n)
    {
        // initializes an array of size n with 0
        int []ans=new int[n];
        for(int i = 0; i < n; i++)
            ans[i] = 0;
 
        // if first element is 0 then the distance
        // will be 0
        if (arr[0] == 0)
            ans[0] = 0;
         
        // if not 0 then initialize
        // with a maximum value
        else
            ans[0] = +2147483646;
     
        // traverse in loop from 1 to n and store
        // the distance from left
        for (int i = 1; i < n; ++i)
        {
     
            // add 1 to the distance
            // from previous one
            ans[i] = ans[i - 1] + 1;
     
            // if the present element is
            // 0 then distance will be 0
            if (arr[i] == 0)
                ans[i] = 0;    
        }
     
        // if last element is zero
        // then it will be 0 else
        // let the answer be what was
        // found when traveled
        // from left to right
        if (arr[n - 1] == 0)
            ans[n - 1] = 0;
     
        // traverse from right to
        // left and store the minimum
        // of distance if found from
        // right to left or left
        // to right
        for (int i = n - 2; i >= 0; --i)
        {
     
            // store the minimum of distance
            // from left to right or right to left
            ans[i] = Math.Min(ans[i], ans[i + 1] + 1);
     
            // if it is 0 then minimum
            // will always be 0
            if (arr[i] == 0)
                ans[i] = 0;
        }
     
        // print the answer array
        for (int i = 0; i < n; ++i)
            Console.Write(ans[i] + " ");
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []a = { 2, 1, 0, 3, 0, 0, 3, 2, 4 };
        int n = a.Length;
        print_distance(a, n);
    }
}
 
// This code is contributed by PrinciRaj1992


PHP
= 0; --$i)
    {
 
        // store the minimum of distance from
        // left to right or right to left
        $ans[$i] = min($ans[$i], $ans[$i + 1] + 1);
 
        // if it is 0 then minimum will
        // always be 0
        if ($arr[$i] == 0)
            $ans[$i] = 0;
    }
 
    // print the answer array
    for ($i = 0; $i < $n; ++$i)
        echo $ans[$i] , " ";
}
 
// Driver Code
$a = array( 2, 1, 0, 3, 0, 0, 3, 2, 4 );
$n = sizeof($a);
print_distance($a, $n);
 
// This code is contributed by Sachin
?>


Javascript


输出:

2 1 0 1 0 0 1 2 3