📌  相关文章
📜  数组可以用0和1填充的方式数,以使连续的元素都不是1

📅  最后修改于: 2021-05-06 19:13:23             🧑  作者: Mango

给定数字N,请找到构造大小为N的数组的方法,以使它仅包含1和0,但没有两个连续的索引中包含值1。

例子:

Input  : 2
Output : 3
Explanation:
For n=2, the possible arrays are:
{0, 1} {1, 0} {0, 0}

Input  : 3
Output : 5
Explanation:
For n=3, the possible arrays are:
{0, 0, 0} {1, 0, 0} {0, 1, 0} {0, 0, 1} {1, 0, 1} 

天真的方法:
基本的暴力破解方法是构造可以用1和0填充数组的所有可能方式,然后检查数组中是否有两个连续的1(如果有),不计算这些数组。

由于每个元素都有2个可能的值1和0,并且共有n个元素,因此不受任何限制的数组总数将是指数级的,即2 n

高效方法:
如果我们仔细观察,会发现在输入和输出中形成了一个模式。

对于n = 1 ,路数为2,即{0},{1}
对于n = 2 ,路数为3
相似地,

对于n = 3的方式是5
对于n = 4的方式数是8

等等…
令T()为给出大小为n的数组可被填充的方式的函数,则我们得到以下递归关系

这是斐波那契数列<的递归关系。
因此,任何n的输出都等于从1开始的斐波那契数列的(n + 2)
即1 1 2 3 5 8 11…

所以现在我们只需要计算第(n + 2)元素的斐波那契数列即可,这就是答案。
时间复杂度为O(n)

C++
// C++ implementation of the 
// above approach
#include 
using namespace std;
  
    // The total number of ways 
    // is equal to the (n+2)th 
    // Fibonacci term, hence we 
    // only need to find that.
    int nth_term(int n)
    {
        int a = 1, b = 1, c = 1;
          
        // Construct fibonacci upto 
        // (n+2)th term the first
        // two terms being 1 and 1
        for (int i = 0; i < n; i++) 
        {
            c = a + b;
            a = b;
            b = c;
        }
          
        return c;
    }
      
    // Driver Code
    int main()
    {
          
        // Take input n
        int n = 10;
        int c = nth_term(n);
          
        // printing output
        cout << c;
    }
  
// This code is contributed by Sumit Sudhakar.


Java
// Java implementation of the 
// above approach
class Main 
{
   
    // The total number of ways 
    // is equal to the (n+2)th 
    // Fibonacci term, hence we 
    // only need to find that.
    public static int nth_term(int n)
    {
        int a = 1, b = 1, c = 1;
           
        // Construct fibonacci upto 
        // (n+2)th term the first
        // two terms being 1 and 1
        for (int i = 0; i < n; i++) 
        {
            c = a + b;
            a = b;
            b = c;
        }
           
        return c;
    }
       
    // Driver program
    public static void main(String[] args)
    {
        // Take input n
        int n = 10;
        int c = nth_term(n);
           
        // printing output
        System.out.println(c);
    }
}


Python3
# Python3 implementation of 
# the above approach
  
# The total number of ways 
# is equal to the (n+2)th 
# Fibonacci term, hence we 
# only need to find that.
def nth_term(n) :
      
    a = 1
    b = 1
    c = 1
      
    # Construct fibonacci upto 
    # (n+2)th term the first
    # two terms being 1 and 1
    for i in range(0, n) :
        c = a + b
        a = b
        b = c
    return c
  
# Driver Code
  
# Take input n
n = 10
c = nth_term(n)
  
# printing output
print (c)
# This code is contributed by 
# Manish Shaw (manishshaw1)


C#
// C# implementation of the 
// above approach
using System;
  
class GFG {
      
    // The total number of ways 
    // is equal to the (n+2)th 
    // Fibonacci term, hence we 
    // only need to find that.
    static int nth_term(int n)
    {
        int a = 1, b = 1, c = 1;
          
        // Construct fibonacci upto 
        // (n+2)th term the first
        // two terms being 1 and 1
        for (int i = 0; i < n; i++) 
        {
            c = a + b;
            a = b;
            b = c;
        }
          
        return c;
    }
      
    // Driver Code
    public static void Main()
    {
          
        // Take input n
        int n = 10;
        int c = nth_term(n);
          
        // printing output
        Console.WriteLine(c);
      
    }
}
      
// This code is contributed by Sam007


PHP


输出:

144

我们可以进一步优化上述解决方案,以便使用矩阵幂解法在O(Log n)中工作,以找到第n个斐波那契数(请参阅斐波那契数数程序的方法4、5和6)。