📜  电影院大厅中可能的座位安排计数以保持社交距离

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

电影院大厅中可能的座位安排计数以保持社交距离

给定一个包含N个非负整数的列表,其中list[k]第 k 个座位和第 (k + 1) 个座位之间的距离,并且单排有(N+1)个座位。找出有效座位安排的数量。没有任何人就座的安排也是有效的。

例子:

天真的方法:天真的方法是使用回溯。在每个给定的座位上,有两种选择,坐或不坐。访问所有可能的安排并找到有效的解决方案。请按照以下步骤操作:

  • 从第一个座位开始。
  • 对于每个座位,检查与前一个座位的距离是否有效。
  • 如果无效,请移至下一个座位。
  • 如果有效,则有两种可能性,坐或不坐。使用这两个选项并移动到下一个座位。
  • 对所有席位递归地使用这些标准。
  • 最后找到的排列总数是所需的答案。

下面是上述方法的实现。

Python3
# Python program to implement the approach
 
# Function to count all valid arrangements
def get_possible_seatings(seats):
     
    # Account for the last seat
    seats.append(0)
    arrangement = []
    total_seatings = 0
    dist = 6
 
    # Function for backtracking
    def dfs(curr, prev_dist):
        nonlocal total_seatings
        if curr > len(seats):
            return
 
        if curr == len(seats):
             
            # This arrangement possible
            total_seatings += 1
            return
 
        # Have only one choice, don't sit
        if prev_dist < dist:
            dfs(curr+1, prev_dist+seats[curr])
        else:
             
            # Have 2 choices here
            arrangement.append(curr)
             
            # Sit here
            dfs(curr+1, seats[curr])
            arrangement.pop(-1)
             
            # Don't sit here
            dfs(curr+1, prev_dist+seats[curr])
        return
 
    # Loop to implement backtracking
    # and call the dfs function
    for index in range(len(seats)):
        arrangement.clear()
        arrangement.append(index)
        dfs(index + 1, seats[index])
 
    # Account for no seats occupied
    return total_seatings + 1
 
# Driver code
if __name__ == "__main__":
    list = [5, 2, 4, 1, 2]
    ans = get_possible_seatings(list)
    print(ans)


Javascript


C++
// C++ program to implement the approach
#include 
using namespace std;
 
// sum function
int sum(vector& v)
{
    int res = 0;
    for (auto dt : v)
        res += dt;
    return res;
}
 
// Function to count all valid arrangements
int get_possible_seatings(vector& seats)
{
    int dist = 6;
 
    // Account for the last seat
    seats.push_back(0);
 
    // Each seat can be occupied individually
    vector dp(seats.size(), 1);
 
    // Keep track of total distance
    // from first seat
    vector total_distance(seats.size(), 0);
    int prefix_sum = seats[0];
    for (int index = 1; index < seats.size(); ++index) {
        total_distance[index] = prefix_sum;
        prefix_sum += seats[index];
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.size(); ++i) {
        for (int j = 0; j < i; ++j) {
            if (total_distance[i] - total_distance[j]
                >= dist)
                dp[i] += dp[j];
        }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
}
 
// Driver code
int main()
{
    vector list{ 5, 2, 4, 1, 2 };
    int ans = get_possible_seatings(list);
    cout << (ans);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // sum function
  static int sum(int[] v)
  {
    int res = 0;
    for (int i = 0; i < v.length; i++)
      res += v[i];
    return res;
  }
 
  // Function to count all valid arrangements
  static int get_possible_seatings(ArrayList seats)
  {
    int dist = 6;
 
    // Account for the last seat
    seats.add(0);
 
    // Each seat can be occupied individually
    int[] dp = new int[seats.size()];
    for (int i = 0; i < seats.size(); i++) {
      dp[i] = 1;
    }
 
    // Keep track of total distance
    // from first seat
    int[] total_distance = new int[seats.size()];
    for (int i = 0; i < seats.size(); i++) {
      total_distance[i] = 0;
    }
 
    int prefix_sum = (int)seats.get(0);
    for (int index = 1; index < seats.size(); ++index) {
      total_distance[index] = prefix_sum;
      prefix_sum += (int)seats.get(index) ;
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.size(); ++i) {
      for (int j = 0; j < i; ++j) {
        if (total_distance[i] - total_distance[j]
            >= dist)
          dp[i] += dp[j];
      }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    ArrayList list = new ArrayList();
 
    list.add(5);
    list.add(2);
    list.add(4);
    list.add(1);
    list.add(2);
 
    int ans = get_possible_seatings(list);
    System.out.print(ans);
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program to implement the approach
 
# Function to count all valid arrangements
def get_possible_seatings(seats):  
    dist = 6
     
    # Account for the last seat
    seats.append(0)
 
    # Each seat can be occupied individually
    dp = [1] * len(seats)
 
    # Keep track of total distance
    # from first seat
    total_distance = [0] * len(seats)
    prefix_sum = seats[0]
    for index, i in enumerate(seats[1:], 1):
        total_distance[index] = prefix_sum
        prefix_sum += i
 
    # Start from second seat onwards,
    # this is the curr seat 'i'
    for i in range(1, len(seats)):
        for j in range(i):
            if total_distance[i] \
            - total_distance[j] >= dist:
                dp[i] += dp[j]
 
    # Account for no seat occupied
    return sum(dp) + 1
 
# Driver code
if __name__ == "__main__":
    list = [5, 2, 4, 1, 2]
    ans = get_possible_seatings(list)
    print(ans)


C#
// C# program to implement the approach
using System;
using System.Collections;
 
class GFG {
 
  // sum function
  static int sum(int[] v)
  {
    int res = 0;
    for (int i = 0; i < v.Length; i++)
      res += v[i];
    return res;
  }
 
  // Function to count all valid arrangements
  static int get_possible_seatings(ArrayList seats)
  {
    int dist = 6;
 
    // Account for the last seat
    seats.Add(0);
 
    // Each seat can be occupied individually
    int[] dp = new int[seats.Count];
    for (int i = 0; i < seats.Count; i++) {
      dp[i] = 1;
    }
 
    // Keep track of total distance
    // from first seat
    int[] total_distance = new int[seats.Count];
    for (int i = 0; i < seats.Count; i++) {
      total_distance[i] = 0;
    }
 
    int prefix_sum = (int)seats[0];
    for (int index = 1; index < seats.Count; ++index) {
      total_distance[index] = prefix_sum;
      prefix_sum += (int)seats[index];
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.Count; ++i) {
      for (int j = 0; j < i; ++j) {
        if (total_distance[i] - total_distance[j]
            >= dist)
          dp[i] += dp[j];
      }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
  }
 
  // Driver code
  public static void Main()
  {
    ArrayList list = new ArrayList();
 
    list.Add(5);
    list.Add(2);
    list.Add(4);
    list.Add(1);
    list.Add(2);
 
    int ans = get_possible_seatings(list);
    Console.Write(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
16

时间复杂度: O(2 N )
辅助空间: O(N)

有效的方法:一种有效的方法是使用动态规划。使用dp[]数组将可能的座位安排存储到第i 个座位。从第一个开始检查每个座位。如果当前座位'i'和前一个座位'j'之间的距离有效(>= 6),则将 dp[j] 添加到 dp[i] 。这基本上意味着以前的座位和现在的座位可以一起被占用。就座方式的总数将增加前一个座位的就座方式数量。请按照以下步骤操作:

  • 从第一个座位开始迭代。
  • 对于每个座位,检查与先前占用的座位的距离是否有效。
  • 如果有效,则通过先前占用座位的安排增加可能安排的数量。
  • 如果没有,请保持安排不变并移动到下一个座位。
  • 最后一个座位的最终值是所需的答案。

下面是上述方法的实现。

C++

// C++ program to implement the approach
#include 
using namespace std;
 
// sum function
int sum(vector& v)
{
    int res = 0;
    for (auto dt : v)
        res += dt;
    return res;
}
 
// Function to count all valid arrangements
int get_possible_seatings(vector& seats)
{
    int dist = 6;
 
    // Account for the last seat
    seats.push_back(0);
 
    // Each seat can be occupied individually
    vector dp(seats.size(), 1);
 
    // Keep track of total distance
    // from first seat
    vector total_distance(seats.size(), 0);
    int prefix_sum = seats[0];
    for (int index = 1; index < seats.size(); ++index) {
        total_distance[index] = prefix_sum;
        prefix_sum += seats[index];
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.size(); ++i) {
        for (int j = 0; j < i; ++j) {
            if (total_distance[i] - total_distance[j]
                >= dist)
                dp[i] += dp[j];
        }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
}
 
// Driver code
int main()
{
    vector list{ 5, 2, 4, 1, 2 };
    int ans = get_possible_seatings(list);
    cout << (ans);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
  // sum function
  static int sum(int[] v)
  {
    int res = 0;
    for (int i = 0; i < v.length; i++)
      res += v[i];
    return res;
  }
 
  // Function to count all valid arrangements
  static int get_possible_seatings(ArrayList seats)
  {
    int dist = 6;
 
    // Account for the last seat
    seats.add(0);
 
    // Each seat can be occupied individually
    int[] dp = new int[seats.size()];
    for (int i = 0; i < seats.size(); i++) {
      dp[i] = 1;
    }
 
    // Keep track of total distance
    // from first seat
    int[] total_distance = new int[seats.size()];
    for (int i = 0; i < seats.size(); i++) {
      total_distance[i] = 0;
    }
 
    int prefix_sum = (int)seats.get(0);
    for (int index = 1; index < seats.size(); ++index) {
      total_distance[index] = prefix_sum;
      prefix_sum += (int)seats.get(index) ;
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.size(); ++i) {
      for (int j = 0; j < i; ++j) {
        if (total_distance[i] - total_distance[j]
            >= dist)
          dp[i] += dp[j];
      }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    ArrayList list = new ArrayList();
 
    list.add(5);
    list.add(2);
    list.add(4);
    list.add(1);
    list.add(2);
 
    int ans = get_possible_seatings(list);
    System.out.print(ans);
  }
}
 
// This code is contributed by sanjoy_62.

Python3

# Python program to implement the approach
 
# Function to count all valid arrangements
def get_possible_seatings(seats):  
    dist = 6
     
    # Account for the last seat
    seats.append(0)
 
    # Each seat can be occupied individually
    dp = [1] * len(seats)
 
    # Keep track of total distance
    # from first seat
    total_distance = [0] * len(seats)
    prefix_sum = seats[0]
    for index, i in enumerate(seats[1:], 1):
        total_distance[index] = prefix_sum
        prefix_sum += i
 
    # Start from second seat onwards,
    # this is the curr seat 'i'
    for i in range(1, len(seats)):
        for j in range(i):
            if total_distance[i] \
            - total_distance[j] >= dist:
                dp[i] += dp[j]
 
    # Account for no seat occupied
    return sum(dp) + 1
 
# Driver code
if __name__ == "__main__":
    list = [5, 2, 4, 1, 2]
    ans = get_possible_seatings(list)
    print(ans)

C#

// C# program to implement the approach
using System;
using System.Collections;
 
class GFG {
 
  // sum function
  static int sum(int[] v)
  {
    int res = 0;
    for (int i = 0; i < v.Length; i++)
      res += v[i];
    return res;
  }
 
  // Function to count all valid arrangements
  static int get_possible_seatings(ArrayList seats)
  {
    int dist = 6;
 
    // Account for the last seat
    seats.Add(0);
 
    // Each seat can be occupied individually
    int[] dp = new int[seats.Count];
    for (int i = 0; i < seats.Count; i++) {
      dp[i] = 1;
    }
 
    // Keep track of total distance
    // from first seat
    int[] total_distance = new int[seats.Count];
    for (int i = 0; i < seats.Count; i++) {
      total_distance[i] = 0;
    }
 
    int prefix_sum = (int)seats[0];
    for (int index = 1; index < seats.Count; ++index) {
      total_distance[index] = prefix_sum;
      prefix_sum += (int)seats[index];
    }
 
    // Start from second seat onwards,
    // this is the curr seat 'i'
    for (int i = 1; i < seats.Count; ++i) {
      for (int j = 0; j < i; ++j) {
        if (total_distance[i] - total_distance[j]
            >= dist)
          dp[i] += dp[j];
      }
    }
 
    // Account for no seat occupied
    return sum(dp) + 1;
  }
 
  // Driver code
  public static void Main()
  {
    ArrayList list = new ArrayList();
 
    list.Add(5);
    list.Add(2);
    list.Add(4);
    list.Add(1);
    list.Add(2);
 
    int ans = get_possible_seatings(list);
    Console.Write(ans);
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript



输出
16

时间复杂度: O(N 2 )
辅助空间: O(N)