📌  相关文章
📜  检查数组中每对1是否彼此之间至少相距K个长度

📅  最后修改于: 2021-05-14 01:11:51             🧑  作者: Mango

给定一个二进制数组和一个整数K ,检查数组中的每对1是否彼此至少相距K个长度。如果条件成立,则返回true,否则返回false。

例子:

方法:

为了解决上述问题,我们必须检查每对相邻的1之间的距离。找到1的第一个位置,然后遍历数组的其余部分,如果距离为0,则递增距离,否则,如果距离小于k,则执行检查操作,然后将计数再次重置为0。

下面是上述方法的实现:

C++
// C++ implementation to Check if every pair of 1 in
// the array is at least K length from each other
  
#include 
using namespace std;
  
// Function to check distance
bool kLengthApart(vector& nums, int k)
{
    // Find first position of 1
    int pos = 0, count = 0;
  
    while (pos < nums.size() && nums[pos] == 0)
        pos++;
  
    // Iterate through the rest of array
    for (int i = pos + 1; i < nums.size(); i++) {
        // Increment distance if its 0
        if (nums[i] == 0)
            count++;
  
        // Check if the distance is less than k
        else {
            if (count < k)
                return false;
  
            // Reset count to 0
            count = 0;
        }
    }
  
    // Return the result
    return true;
}
  
// Driver code
int main()
{
    vector nums = { 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 };
    int k = 2;
  
    bool ans = kLengthApart(nums, k);
    if (ans == 1)
        cout << "True" << endl;
  
    else
        cout << "False" << endl;
  
    return 0;
}


Java
// Java implementation to check if 
// every pair of 1 in the array is 
// at least K length from each other 
class Main{
      
// Function to check distance 
public static boolean kLengthApart(int[] nums,
                                   int k) 
{ 
      
    // Find first position of 1 
    int pos = 0, count = 0; 
  
    while (pos < nums.length && nums[pos] == 0) 
        pos++; 
  
    // Iterate through the rest of array 
    for(int i = pos + 1; i < nums.length; i++)
    {
          
       // Increment distance if its 0 
       if (nums[i] == 0) 
           count++; 
             
       // Check if the distance is less than k 
       else
       { 
           if (count < k) 
               return false; 
                 
           // Reset count to 0 
           count = 0; 
       } 
    } 
      
    // Return the result 
    return true; 
} 
  
// Driver Code
public static void main(String[] args)
{
    int[] nums = { 1, 0, 0, 0, 1, 
                   0, 0, 1, 0, 0 }; 
    int k = 2; 
    boolean ans = kLengthApart(nums, k); 
      
    if (ans) 
        System.out.println("True");
  
    else
        System.out.println("False");
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to check if 
# every pair of 1 in the array is 
# at least K length from each other 
  
# Function to check distance
def kLengthApart(nums, k):
      
    # Find first position of 1 
    pos = 0
    count = 0
      
    while (pos < len(nums) and nums[pos] == 0):
        pos += 1
          
    # Iterate through the rest of list
    for i in range(pos + 1, len(nums)):
          
        # Increment distance if its 0 
        if nums[i] == 0:
            count += 1
              
        # Check if the distance is less than k 
        else :
            if count < k:
                return False
                  
            # Reset count to 0 
            count = 0
              
        # Return the result 
    return True
      
# Driver Code
if __name__ == "__main__":
       
    nums = [ 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 ]
    k = 2
      
    print(kLengthApart(nums, k))
  
# This code is contributed by rutvik_56


C#
// C# implementation to check if 
// every pair of 1 in the array is 
// at least K length from each other 
using System;
  
class GFG{
      
// Function to check distance 
public static bool kLengthApart(int[] nums,
                                int k) 
{ 
      
    // Find first position of 1 
    int pos = 0, count = 0; 
  
    while (pos < nums.Length && nums[pos] == 0) 
        pos++; 
  
    // Iterate through the rest of array 
    for(int i = pos + 1; i < nums.Length; i++)
    {
         
       // Increment distance if its 0 
       if (nums[i] == 0) 
           count++; 
             
       // Check if the distance is 
       // less than k 
       else
       { 
           if (count < k) 
               return false; 
             
           // Reset count to 0 
           count = 0; 
       } 
    } 
      
    // Return the result 
    return true; 
} 
  
// Driver Code
public static void Main()
{
    int[] nums = { 1, 0, 0, 0, 1, 
                   0, 0, 1, 0, 0 }; 
    int k = 2; 
    bool ans = kLengthApart(nums, k); 
      
    if (ans) 
        Console.Write("True");
    else
        Console.Write("False");
}
}
  
// This code is contributed by chitranayal


输出:
True

时间复杂度: O(n)
辅助空间: O(1)