zoukankan      html  css  js  c++  java
  • [GeeksForGeeks] Friends Pairing Problem

    Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up.

    Examples:

    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.

     The problem itself is pretty straightforward with the following optimal substructure:

    f(n) = f(n - 1) + (n - 1) * f(n - 2);

    To make it more challenging, write a method that generates all possible pairing ways and save them in a list.

    This is a dfs + backtracking question.

    Provided the optimal substructure, we know that for a given person, either he is single or he pairs with one other person.

    If he is single, we simply reduce the problem to be one fewer person smaller;

    If he is paired with another person, we pick one from all the available persons pool.

    To avoid duplicated answers, we only pick person with a bigger number as the pair of the current person.   

    We also need a global flag for each person to indicate if a person has been picked or not. This is needed because when picking 

    a pair, there will be persons left in between these 2 picked persons and these left persons have not been picked yet. The remaining 

    subproblem should include these unpicked persons. By only advancing the current index does not address this issue. 

     1 import java.util.List;
     2 import java.util.ArrayList;
     3 
     4 public class FriendsPairing {
     5     //f(n) = f(n - 1) + (n - 1) * f(n - 2);
     6     //Recursive solution
     7     public static int pairingWaysRecursion(int n) {
     8         if(n <= 1) {
     9             return 1;
    10         }
    11         return pairingWaysRecursion(n - 1) + (n - 1) * pairingWaysRecursion(n - 2);
    12     }
    13     //Dynamic Programming 
    14     public static int pairingWaysDp(int n) {
    15         int[] T = new int[n + 1];
    16         T[0] = 1;
    17         T[1] = 1;
    18         for(int i = 2; i <= n; i++) {
    19             T[i] = T[i - 1] + (i - 1) * T[i - 2];
    20         }
    21         return T[n];
    22     }
    23     //Dfs + backtracking to get all pairing ways
    24     public static List<List<List<Integer>>> getAllPairingWays(int n) {
    25         boolean[] available = new boolean[n + 1];
    26         for(int i = 1; i <= n; i++) {
    27             available[i] = true;
    28         }
    29         List<List<List<Integer>>> ways = new ArrayList<>();
    30         getAllWaysDfs(ways, new ArrayList<>(), available, n, 1, 0);
    31         return ways;
    32     }
    33     private static void getAllWaysDfs(List<List<List<Integer>>> ways, 
    34                                 List<List<Integer>> way,
    35                                 boolean[] available, int n, int currIdx, int addedCount) {
    36         if(addedCount == n) {
    37             ways.add(new ArrayList<List<Integer>>(way));
    38             return;
    39         }
    40         for(int i = currIdx; i <= n; i++) {
    41             if(available[i]) {
    42                 ArrayList<Integer> group1 = new ArrayList<Integer>();
    43                 group1.add(i);
    44                 way.add(group1);
    45                 available[i] = false;
    46                 getAllWaysDfs(ways, way, available, n, i + 1, addedCount + 1);            
    47                 way.remove(way.size() - 1);    
    48                 
    49                 int j = i + 1;
    50                 for(; j <= n; j++) {
    51                     if(available[j]) {
    52                         ArrayList<Integer> group2 = new ArrayList<Integer>();
    53                         group2.add(i);
    54                         group2.add(j);
    55                         way.add(group2);
    56                         available[j] = false;
    57                         getAllWaysDfs(ways, way, available, n, i + 1, addedCount + 2);        
    58                         way.remove(way.size() - 1);
    59                         available[j] = true;
    60                     }
    61                 }
    62                 available[i] = true;
    63             }
    64         }
    65     }
    66     public static void main(String[] args) {
    67         System.out.println(pairingWaysDp(3));
    68         System.out.println(pairingWaysDp(6));
    69         List<List<List<Integer>>> ways = getAllPairingWays(3);
    70         for(int i = 0; i < ways.size(); i++) {
    71             for(int j = 0; j < ways.get(i).size(); j++) {
    72                 for(int k = 0; k < ways.get(i).get(j).size(); k++) {
    73                     System.out.print(ways.get(i).get(j).get(k) + ",");
    74                 }
    75                 System.out.print("  ");
    76             }
    77             System.out.println();
    78         }
    79     }
    80 }

    Related Problems 

    Permutations 

    Permutations II





  • 相关阅读:
    [Bzoj2152]聪聪可可
    [2019杭电多校第七场][hdu6655]Just Repeat
    [2019杭电多校第七场][hdu6651]Final Exam
    [2019杭电多校第七场][hdu6646]A + B = C(hash)
    [2019杭电多校第六场][hdu6641]TDL
    [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)
    abc179f
    Codeforces Round #680A
    Codeforces Round #680B
    Codeforces Round #681 D
  • 原文地址:https://www.cnblogs.com/lz87/p/7594929.html
Copyright © 2011-2022 走看看