📜  朋友配对问题

📅  最后修改于: 2021-05-04 14:16:08             🧑  作者: Mango

给定n个朋友,每个人可以保持单身,也可以与其他朋友配对。每个朋友只能配对一次。找出使朋友保持单身或配对的方式的总数。
例子 :

Input  : n = 3
Output : 4
Explanation:
{1}, {2}, {3} : all single
{1}, {2, 3} : 2 and 3 paired but 1 is single.
{1, 2}, {3} : 1 and 2 are paired but 3 is single.
{1, 3}, {2} : 1 and 3 are paired but 2 is single.
Note that {1, 2} and {2, 1} are considered same.
f(n) = ways n people can remain single 
       or pair up.

For n-th person there are two choices:
1) n-th person remains single, we recur 
   for f(n - 1)
2) n-th person pairs up with any of the 
   remaining n - 1 persons. We get (n - 1) * f(n - 2)

Therefore we can recursively write f(n) as:
f(n) = f(n - 1) + (n - 1) * f(n - 2)

由于上述递归公式具有重叠的子问题,因此我们可以使用动态编程来解决。

C++
// C++ program for solution of
// friends pairing problem
#include 
using namespace std;
 
// Returns count of ways n people
// can remain single or paired up.
int countFriendsPairings(int n)
{
    int dp[n + 1];
 
    // Filling dp[] in bottom-up manner using
    // recursive formula explained above.
    for (int i = 0; i <= n; i++) {
        if (i <= 2)
            dp[i] = i;
        else
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
    }
 
    return dp[n];
}
 
// Driver code
int main()
{
    int n = 4;
    cout << countFriendsPairings(n) << endl;
    return 0;
}


Java
// Java program for solution of
// friends pairing problem
import java.io.*;
 
class GFG {
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int dp[] = new int[n + 1];
 
        // Filling dp[] in bottom-up manner using
        // recursive formula explained above.
        for (int i = 0; i <= n; i++) {
            if (i <= 2)
                dp[i] = i;
            else
                dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
        }
 
        return dp[n];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(countFriendsPairings(n));
    }
}
 
// This code is contributed by vt_m


Python3
# Python program solution of
# friends pairing problem
 
# Returns count of ways
# n people can remain
# single or paired up.
def countFriendsPairings(n):
 
    dp = [0 for i in range(n + 1)]
 
    # Filling dp[] in bottom-up manner using
    # recursive formula explained above.
    for i in range(n + 1):
 
        if(i <= 2):
            dp[i] = i
        else:
            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]
 
    return dp[n]
 
# Driver code
n = 4
print(countFriendsPairings(n))
 
# This code is contributed
# by Soumen Ghosh.


C#
// C# program solution for
// friends pairing problem
using System;
 
class GFG {
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int[] dp = new int[n + 1];
 
        // Filling dp[] in bottom-up manner using
        // recursive formula explained above.
        for (int i = 0; i <= n; i++) {
            if (i <= 2)
                dp[i] = i;
            else
                dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
        }
 
        return dp[n];
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.Write(countFriendsPairings(n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program for solution of friends
// pairing problem Using Recursion
#include 
using namespace std;
 
int dp[1000];
 
// Returns count of ways n people
// can remain single or paired up.
int countFriendsPairings(int n)
{
    if (dp[n] != -1)
        return dp[n];
 
    if (n > 2)
        return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
    else
        return dp[n] = n;
}
 
// Driver code
int main()
{
    memset(dp, -1, sizeof(dp));
    int n = 4;
    cout << countFriendsPairings(n) << endl;
    // this code is contributed by Kushdeep Mittal
}


Java
// Java program for solution of friends
// pairing problem Using Recursion
 
class GFG {
    static int[] dp = new int[1000];
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        if (dp[n] != -1)
            return dp[n];
 
        if (n > 2)
            return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
        else
            return dp[n] = n;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        for (int i = 0; i < 1000; i++)
            dp[i] = -1;
        int n = 4;
        System.out.println(countFriendsPairings(n));
    }
}
 
// This code is contributed by Ita_c.


Python3
# Python3 program for solution of friends
# pairing problem Using Recursion
dp = [-1] * 1000
# Returns count of ways n people
# can remain single or paired up.
def countFriendsPairings(n):
    global dp
     
    if(dp[n] != -1):
        return dp[n]
 
    if(n > 2):
 
        dp[n] = (countFriendsPairings(n - 1) +
                (n - 1) * countFriendsPairings(n - 2))
        return dp[n]
 
    else:
        dp[n] = n
        return dp[n]
     
# Driver Code
n = 4
print(countFriendsPairings(n))
 
# This code contributed by PrinciRaj1992


C#
// C# program for solution of friends
// pairing problem Using Recursion
using System;
 
class GFG {
    static int[] dp = new int[1000];
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        if (dp[n] != -1)
            return dp[n];
 
        if (n > 2)
            return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
        else
            return dp[n] = n;
    }
 
    // Driver code
    static void Main()
    {
        for (int i = 0; i < 1000; i++)
            dp[i] = -1;
        int n = 4;
        Console.Write(countFriendsPairings(n));
    }
}
 
// This code is contributed by DrRoot_


PHP
 2)
    {
        $dp[$n] = countFriendsPairings($n - 1) + ($n - 1) *
                  countFriendsPairings($n - 2);
        return $dp[$n];
    }
    else
    {
        $dp[$n] = $n;
        return $dp[$n];
    }
}
     
// Driver Code
$n = 4;
echo countFriendsPairings($n)
 
// This code is contributed by Ryuga
?>


Javascript


C++
#include 
using namespace std;
 
// Returns count of ways n people
// can remain single or paired up.
int countFriendsPairings(int n)
{
    int a = 1, b = 2, c = 0;
    if (n <= 2) {
        return n;
    }
    for (int i = 3; i <= n; i++) {
        c = b + (i - 1) * a;
        a = b;
        b = c;
    }
    return c;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << countFriendsPairings(n);
    return 0;
}
 
// This code is contributed by mits


Java
class GFG {
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int a = 1, b = 2, c = 0;
        if (n <= 2) {
            return n;
        }
        for (int i = 3; i <= n; i++) {
            c = b + (i - 1) * a;
            a = b;
            b = c;
        }
        return c;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(countFriendsPairings(n));
    }
}
 
// This code is contributed by Ravi Kasha.


Python3
# Returns count of ways n people
# can remain single or paired up.
def countFriendsPairings(n):
    a, b, c = 1, 2, 0;
    if (n <= 2):
        return n;
    for i in range(3, n + 1):
        c = b + (i - 1) * a;
        a = b;
        b = c;
    return c;
 
# Driver code
n = 4;
print(countFriendsPairings(n));
 
# This code contributed by Rajput-Ji


C#
using System;
 
class GFG {
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int a = 1, b = 2, c = 0;
        if (n <= 2) {
            return n;
        }
        for (int i = 3; i <= n; i++) {
            c = b + (i - 1) * a;
            a = b;
            b = c;
        }
        return c;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 4;
        Console.WriteLine(countFriendsPairings(n));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP


输出 :

10

时间复杂度: O(n)
辅助空间: O(n)
另一种方法:(使用递归)

C++

// C++ program for solution of friends
// pairing problem Using Recursion
#include 
using namespace std;
 
int dp[1000];
 
// Returns count of ways n people
// can remain single or paired up.
int countFriendsPairings(int n)
{
    if (dp[n] != -1)
        return dp[n];
 
    if (n > 2)
        return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
    else
        return dp[n] = n;
}
 
// Driver code
int main()
{
    memset(dp, -1, sizeof(dp));
    int n = 4;
    cout << countFriendsPairings(n) << endl;
    // this code is contributed by Kushdeep Mittal
}

Java

// Java program for solution of friends
// pairing problem Using Recursion
 
class GFG {
    static int[] dp = new int[1000];
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        if (dp[n] != -1)
            return dp[n];
 
        if (n > 2)
            return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
        else
            return dp[n] = n;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        for (int i = 0; i < 1000; i++)
            dp[i] = -1;
        int n = 4;
        System.out.println(countFriendsPairings(n));
    }
}
 
// This code is contributed by Ita_c.

Python3

# Python3 program for solution of friends
# pairing problem Using Recursion
dp = [-1] * 1000
# Returns count of ways n people
# can remain single or paired up.
def countFriendsPairings(n):
    global dp
     
    if(dp[n] != -1):
        return dp[n]
 
    if(n > 2):
 
        dp[n] = (countFriendsPairings(n - 1) +
                (n - 1) * countFriendsPairings(n - 2))
        return dp[n]
 
    else:
        dp[n] = n
        return dp[n]
     
# Driver Code
n = 4
print(countFriendsPairings(n))
 
# This code contributed by PrinciRaj1992

C#

// C# program for solution of friends
// pairing problem Using Recursion
using System;
 
class GFG {
    static int[] dp = new int[1000];
 
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        if (dp[n] != -1)
            return dp[n];
 
        if (n > 2)
            return dp[n] = countFriendsPairings(n - 1) + (n - 1) * countFriendsPairings(n - 2);
        else
            return dp[n] = n;
    }
 
    // Driver code
    static void Main()
    {
        for (int i = 0; i < 1000; i++)
            dp[i] = -1;
        int n = 4;
        Console.Write(countFriendsPairings(n));
    }
}
 
// This code is contributed by DrRoot_

的PHP

 2)
    {
        $dp[$n] = countFriendsPairings($n - 1) + ($n - 1) *
                  countFriendsPairings($n - 2);
        return $dp[$n];
    }
    else
    {
        $dp[$n] = $n;
        return $dp[$n];
    }
}
     
// Driver Code
$n = 4;
echo countFriendsPairings($n)
 
// This code is contributed by Ryuga
?>

Java脚本


输出 :

10

时间复杂度: O(n)
辅助空间: O(n)
由于上述公式类似于斐波那契数,因此我们可以使用迭代解决方案来优化空间。

C++

#include 
using namespace std;
 
// Returns count of ways n people
// can remain single or paired up.
int countFriendsPairings(int n)
{
    int a = 1, b = 2, c = 0;
    if (n <= 2) {
        return n;
    }
    for (int i = 3; i <= n; i++) {
        c = b + (i - 1) * a;
        a = b;
        b = c;
    }
    return c;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << countFriendsPairings(n);
    return 0;
}
 
// This code is contributed by mits

Java

class GFG {
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int a = 1, b = 2, c = 0;
        if (n <= 2) {
            return n;
        }
        for (int i = 3; i <= n; i++) {
            c = b + (i - 1) * a;
            a = b;
            b = c;
        }
        return c;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(countFriendsPairings(n));
    }
}
 
// This code is contributed by Ravi Kasha.

Python3

# Returns count of ways n people
# can remain single or paired up.
def countFriendsPairings(n):
    a, b, c = 1, 2, 0;
    if (n <= 2):
        return n;
    for i in range(3, n + 1):
        c = b + (i - 1) * a;
        a = b;
        b = c;
    return c;
 
# Driver code
n = 4;
print(countFriendsPairings(n));
 
# This code contributed by Rajput-Ji

C#

using System;
 
class GFG {
    // Returns count of ways n people
    // can remain single or paired up.
    static int countFriendsPairings(int n)
    {
        int a = 1, b = 2, c = 0;
        if (n <= 2) {
            return n;
        }
        for (int i = 3; i <= n; i++) {
            c = b + (i - 1) * a;
            a = b;
            b = c;
        }
        return c;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 4;
        Console.WriteLine(countFriendsPairings(n));
    }
}
 
// This code has been contributed by 29AjayKumar

的PHP


输出 :

10

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