📜  计算灯泡切换状态的次数

📅  最后修改于: 2021-04-17 15:19:57             🧑  作者: Mango

给定两个数组switch [] (由表示开关为ON(0)OFF(1)的二进制整数)query []组成,其中query [i]表示要切换的开关。完成所有开关切换后的任务是打印灯泡改变其状态的次数,即从ON变为OFF ,反之亦然。

例子 :

方法:按照以下步骤解决问题:

  1. 遍历数组arr []。
  2. 计算1 s的数量,以跟踪灯泡的初始状态。
  3. 遍历数组query []。
  4. 对于每个query [i] ,更新arr []1 s的计数。相应地检查灯泡的当前状态。
  5. 如果发现先前状态与当前状态不同,则增加计数。
  6. 最后,打印计数值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the number of
// times a bulb switches its state
int solve(int A[], int n,
          int Q[], int q)
{
   
  // Count of 1s
  int one = 0;
 
  // Traverse the array
  for (int i = 0; i < n; i++)
 
    // Update count of 1s
    if (A[i] == 1)
      one++;
 
  // Update the status of bulb
  int glows = 0, count = 0;
 
  if (one >= ceil(n / 2))
    glows = 1;
 
  // Traverse the array Q[]
  for (int i = 0; i < q; i++) {
 
    // Stores previous state
    // of the bulb
    int prev = glows;
 
    // Toggle the switch and
    // update count of 1s
    if (A[Q[i] - 1] == 1)
      one--;
    if (A[Q[i] - 1] == 0)
      one++;
 
    A[Q[i] - 1] ^= 1;
 
    if (one >= ceil(n / 2.0)) {
      glows = 1;
    }
    else {
      glows = 0;
    }
 
    // If the bulb switches state
    if (prev != glows)
      count++;
  }
 
  // Return count
  return count;
}
 
// Driver Code
int main()
{
   
  // Input
  int n = 3;
  int arr[] = { 1, 1, 0 };
  int q = 3;
 
  // Queries
  int Q[] = { 3, 2, 1 };
 
  // Function call to find number
  // of times the bulb toggles
  cout << solve(arr, n, Q, q);
 
  return 0;
}
 
// This code is contributed by splevel62.


Java
// Java implementation of
// the above approach
 
import java.util.*;
public class Main {
 
    // Function to find the number of
    // times a bulb switches its state
    static int solve(int[] A, int n,
                     int Q[], int q)
    {
        // Count of 1s
        int one = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++)
 
            // Update count of 1s
            if (A[i] == 1)
                one++;
 
        // Update the status of bulb
        int glows = 0, count = 0;
 
        if (one >= (int)Math.ceil(n / 2))
            glows = 1;
 
        // Traverse the array Q[]
        for (int i = 0; i < q; i++) {
 
            // Stores previous state
            // of the bulb
            int prev = glows;
 
            // Toggle the switch and
            // update count of 1s
            if (A[Q[i] - 1] == 1)
                one--;
            if (A[Q[i] - 1] == 0)
                one++;
 
            A[Q[i] - 1] ^= 1;
 
            if (one >= (int)Math.ceil(n / 2.0)) {
                glows = 1;
            }
            else {
                glows = 0;
            }
 
            // If the bulb switches state
            if (prev != glows)
                count++;
        }
 
        // Return count
        return count;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Input
        int n = 3;
        int arr[] = { 1, 1, 0 };
        int q = 3;
 
        // Queries
        int Q[] = { 3, 2, 1 };
 
        // Function call to find number
        // of times the bulb toggles
        System.out.println(
            solve(arr, n, Q, q));
    }
}


Python3
# Python program for
# the above approach
import math
 
# Function to find the number of
# times a bulb switches its state
def solve(A, n, Q, q):
   
    # count of 1's
    one = 0
     
    # Traverse the array
    for i in range(0, n):
       
        # update the array
        if (A[i] == 1):
            one += 1
             
    # update the status of bulb
    glows = 0
    count = 0
    if (one >= int(math.ceil(n / 2))):
        glows = 1
 
    # Traverse the array Q[]
    for i in range(0, q):
       
        # stores previous state of
        # the bulb
        prev = glows
 
        # Toggle the switch and
        # update the count of 1's
        if (A[Q[i] - 1] == 1):
            one -= 1
        if (A[Q[i] - 1] == 0):
            one += 1
        A[Q[i] - 1] ^= 1
        if (one >= int(math.ceil(n/2.0))):
            glows = 1
        else:
            glows = 0
        # if the bulb switches state
        if (prev != glows):
            count += 1
         
    # Retuen count
    return count
 
# Driver code
 
# Input
n = 3
arr = [1, 1, 0]
q = 3
 
# Queries
Q = [3, 2, 1]
 
# Function call to find number
# of times the bulb toggles
print(solve(arr, n, Q, q))
 
# This code id contributed by Virusbuddah


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the number of
  // times a bulb switches its state
  static int solve(int[] A, int n,
                   int[] Q, int q)
  {
 
    // Count of 1s
    int one = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
 
      // Update count of 1s
      if (A[i] == 1)
        one++;
 
    // Update the status of bulb
    int glows = 0, count = 0;
 
    if (one >= (int)Math.Ceiling((double)n / 2))
      glows = 1;
 
    // Traverse the array Q[]
    for (int i = 0; i < q; i++) {
 
      // Stores previous state
      // of the bulb
      int prev = glows;
 
      // Toggle the switch and
      // update count of 1s
      if (A[Q[i] - 1] == 1)
        one--;
      if (A[Q[i] - 1] == 0)
        one++;
 
      A[Q[i] - 1] ^= 1;
 
      if (one >= (int)Math.Ceiling((double)n / 2.0)) {
        glows = 1;
      }
      else {
        glows = 0;
      }
 
      // If the bulb switches state
      if (prev != glows)
        count++;
    }
 
    // Return count
    return count;
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Input
    int n = 3;
    int[] arr = { 1, 1, 0 };
    int q = 3;
 
    // Queries
    int[] Q = { 3, 2, 1 };
 
    // Function call to find number
    // of times the bulb toggles
    Console.WriteLine(
      solve(arr, n, Q, q));
  }
}
 
// This code is contributed by susmitakundugoaldanga.


输出:
1

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