📌  相关文章
📜  计算最小右翻转以设置数组中的所有值

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

计算最小右翻转以设置数组中的所有值

N个灯泡用电线连接。每个灯泡都有一个与之关联的开关,但是由于接线错误,一个开关也会改变当前灯泡右侧所有灯泡的状态。给定所有灯泡的初始状态,找出打开所有灯泡所需按下的最少开关数。您可以多次按下同一个开关。
注:0 表示灯泡关闭,1 表示灯泡打开。
例子:

Input :  [0 1 0 1]
Output : 4
Explanation :
    press switch 0 : [1 0 1 0]
    press switch 1 : [1 1 0 1]
    press switch 2 : [1 1 1 0]
    press switch 3 : [1 1 1 1]

Input : [1 0 0 0 0] 
Output : 1

我们从左到右遍历给定的数组并按住开关关闭灯泡。到目前为止,我们一直在跟踪按下开关的次数。如果按下的次数是奇数,则表示当前开关处于其原始状态,否则处于另一个状态。根据灯泡所处的状态,我们可以增加按下次数的计数。

C++
// CPP program to find number switch presses to
// turn all bulbs on.
#include
using namespace std;
 
int bulbs(int a[],int n)
{
    // To keep track of switch presses so far
    int count = 0;
 
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        /* if the bulb's original state is on and count
        is even, it is currently on...*/
        /* no need to press this switch */
        if (a[i] == 1 && count % 2 == 0)
            continue;
 
        /* If the bulb's original state is off and count
        is odd, it is currently on...*/
        /* no need to press this switch */
        else if(a[i] == 0 && count % 2 != 0)
            continue;
 
        /* if the bulb's original state is on and count   
        is odd, it is currently off...*/
        /* Press this switch to on the bulb and increase
        the count */
        else if (a[i] == 1 && count % 2 != 0)
        {
            res++;
            count++;
        }
         
        /* if the bulb's original state is off and
        count is even, it is currently off...*/
        /* press this switch to on the bulb and
        increase the count */
        else if (a[i] == 0 && count % 2 == 0)
        {
            res++;
            count++;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    int states[] = {0,1,0,1};
    int n = sizeof(states)/sizeof(states[0]);
    cout << "The minimum number of switches needed are " << bulbs(states,n);
}
 
// This code is contributed by
// Sanjit_Prasad


Java
// Java program to find number switch presses to
// turn all bulbs on.
import java.util.*;
 
public class GFG
{
    public int bulbs(ArrayList a)
    {
        // To keep track of switch presses so far
        int count = 0;
 
        int res = 0;
        for (int i = 0; i < a.size(); i++)
        {
            /* if the bulb's original state is on and count
               is even, it is currently on...*/
            /* no need to press this switch */
            if (a.get(i) == 1 && count%2 == 0)
                continue;
 
            /* If the bulb's original state is off and count
               is odd, it is currently on...*/
            /* no need to press this switch */
            else if(a.get(i) == 0 && count%2 != 0)
                continue;
 
            /* if the bulb's original state is on and count
               is odd, it is currently off...*/
            /* Press this switch to on the bulb and increase
               the count */
            else if (a.get(i) == 1 && count%2 != 0)
            {
                res++;
                count++;
            }
 
            /* if the bulb's original state is off and
               count is even, it is currently off...*/
            /* press this switch to on the bulb and
               increase the count */
            else if (a.get(i) == 0 && count%2 == 0)
            {
                res++;
                count++;
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        GFG gfg = new GFG();
 
        ArrayList states = new ArrayList();
 
        states.add(0);
        states.add(1);
        states.add(0);
        states.add(1);
 
        System.out.println("The minimum number of switches" +
                           " needed are " + gfg.bulbs(states));
    }
}


Python3
# Python program to find number switch presses to
# turn all bulbs on.
 
 
def bulbs(a, n):
    # To keep track of switch presses so far
    count = 0
 
    res = 0
    for i in range(n):
        # if the bulb's original state is on and count
        # is even, it is currently on...
        # no need to press this switch
        if (a[i] == 1 and count % 2 == 0):
            continue
 
        # If the bulb's original state is off and count
        # is odd, it is currently on...
        # no need to press this switch
        elif(a[i] == 0 and count % 2 != 0):
            continue
 
        # if the bulb's original state is on and count
        # is odd, it is currently off...
        # Press this switch to on the bulb and increase
        # the count
        elif (a[i] == 1 and count % 2 != 0):
            res += 1
            count += 1
 
        # if the bulb's original state is off and
        # count is even, it is currently off...
        # press this switch to on the bulb and
        # increase the count
        elif (a[i] == 0 and count % 2 == 0):
            res += 1
            count += 1
    return res
 
 
# Driver code
states = [0, 1, 0, 1]
n = len(states)
print("The minimum number of switches needed are", bulbs(states, n))
 
# This code is contributed by ankush_953


C#
// C# program to find number switch presses
// to turn all bulbs on.
using System;
using System.Collections.Generic;
 
class GFG
{
public virtual int bulbs(List a)
{
    // To keep track of switch presses so far
    int count = 0;
 
    int res = 0;
    for (int i = 0; i < a.Count; i++)
    {
        /* if the bulb's original state is on
        and count is even, it is currently on...*/
        /* no need to press this switch */
        if (a[i] == 1 && count % 2 == 0)
        {
            continue;
        }
 
        /* If the bulb's original state is off
        and count is odd, it is currently on...*/
        /* no need to press this switch */
        else if (a[i] == 0 && count % 2 != 0)
        {
            continue;
        }
 
        /* if the bulb's original state is on
        and count is odd, it is currently off...*/
        /* Press this switch to on the bulb and
        increase the count */
        else if (a[i] == 1 && count % 2 != 0)
        {
            res++;
            count++;
        }
 
        /* if the bulb's original state is off and
        count is even, it is currently off...*/
        /* press this switch to on the bulb and
        increase the count */
        else if (a[i] == 0 && count % 2 == 0)
        {
            res++;
            count++;
        }
    }
    return res;
}
 
// Driver code
public static void Main(string[] args)
{
    GFG gfg = new GFG();
 
    List states = new List();
 
    states.Add(0);
    states.Add(1);
    states.Add(0);
    states.Add(1);
 
    Console.WriteLine("The minimum number of switches" +
                    " needed are " + gfg.bulbs(states));
}
}
 
// This code is contributed by Shrikant13


Javascript


输出:

The minimum number of switches needed are 4.