📌  相关文章
📜  能够得到想要的冰淇淋口味的顾客数量

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

能够得到想要的冰淇淋口味的顾客数量

给定两种口味的冰淇淋巧克力和香草,分别用01表示。人们排队等候从一堆冰淇淋中得到他们想要的冰淇淋口味。

  • 如果排在最前面的顾客更喜欢放在栈顶的冰淇淋包,他们就会拿走它并离开队列。
  • 否则,他们将离开它并走到队列的末尾。

该过程继续进行,直到队列中没有人想要拿走最上面的冰淇淋包。

给定两个数组customer[]icecream[]其中customer[i]是第i个客户的偏好(i =0 是队列的前面), icecream[i]表示堆栈中第i个冰淇淋的类型(i = 0 是栈顶)。任务是找出能够得到他们想要的冰淇淋口味的顾客数量。

例子:

方法一:这个问题可以通过使用堆栈和队列来解决。请按照以下步骤解决给定的问题。

  • 创建一个堆栈并在堆栈中推送icecream[]数组。
  • 创建队列并在队列中推送customer[]数组
  • 初始化一个变量topRejected=0来跟踪被拒绝的元素。
  • 如果队列的前面等于栈顶,则从中弹出元素并从队列中移除该元素并更新topRejected=0
  • 否则增加 topRejected 的计数并从队列的前面删除元素并添加到最后。
  • 如果队列大小等于topRejected则中断循环。
  • 打印icecream.lengthqueue.size作为所需的答案(因为队列中的剩余元素将无法获得所需的冰淇淋包)。
Java
/*Java implementation of above approach*/
 
import java.io.*;
import java.util.*;
class GFG {
    public static void NumberOfCustomer(
        int[] customer, int[] icecream)
    {
        Stack stack = new Stack<>();
        Queue queue = new LinkedList<>();
 
        for (int i = icecream.length - 1; i >= 0; i--) {
            stack.push(icecream[i]);
        }
 
        for (int i = 0; i < customer.length; i++) {
            queue.add(customer[i]);
        }
 
        int topRejected = 0;
 
        while (true) {
            if (topRejected == queue.size())
                break;
 
            if (queue.peek() == stack.peek()) {
                queue.remove();
                stack.pop();
                topRejected = 0;
            }
            else {
                topRejected++;
                queue.add(queue.remove());
            }
        }
 
        System.out.println(
            icecream.length - queue.size());
    }
 
    public static void main(String[] args)
    {
        int customer[] = { 1, 1, 0, 0 };
        int icecream[] = { 0, 1, 0, 1 };
        NumberOfCustomer(customer, icecream);
    }
}


C#
/*C# implementation of above approach*/
using System;
using System.Collections.Generic;
 
public class GFG {
    public static void NumberOfCustomer(
        int[] customer, int[] icecream)
    {
        Stack stack = new Stack();
        Queue queue = new Queue();
 
        for (int i = icecream.Length - 1; i >= 0; i--) {
            stack.Push(icecream[i]);
        }
 
        for (int i = 0; i < customer.Length; i++) {
            queue.Enqueue(customer[i]);
        }
 
        int topRejected = 0;
 
        while (true) {
            if (topRejected == queue.Count)
                break;
 
            if (queue.Peek() == stack.Peek()) {
                queue.Dequeue();
                stack.Pop();
                topRejected = 0;
            }
            else {
                topRejected++;
                queue.Enqueue(queue.Dequeue());
            }
        }
 
        Console.WriteLine(
            icecream.Length - queue.Count);
    }
 
    public static void Main(String[] args)
    {
        int[]customer = { 1, 1, 0, 0 };
        int[] icecream = { 0, 1, 0, 1 };
        NumberOfCustomer(customer, icecream);
    }
}
 
// This code is contributed by 29AjayKumar


C++
// c++ program for above approach
#include 
using namespace std;
 
// Function to find the number
// of customers who will
// get their desired flavor of ice cream
int NumberOfCustomer(int customer[], int icecream[], int n)
{
 
    // Count array stores the count
    // of preference of the customer
    int count[] = { 0, 0 };
 
    int k = 0;
    for (int a = 0; a < n; a++) {
        count[customer[a]]++;
    }
 
    for (k = 0; k < n && count[icecream[k]] > 0; ++k) {
        count[icecream[k]]--;
    }
 
    // Return k as the final answer
    return k;
}
 
int main()
{
    int customer[] = { 1, 1, 0, 0 };
    int icecream[] = { 0, 1, 0, 1 };
    int n = sizeof(customer) / sizeof(customer[0]);
 
    int ans = NumberOfCustomer(customer, icecream, n);
 
    // Print the final answer
    cout << (ans);
    return 0;
}
 
// This code is contributed by ukasp.


Java
// Java program for above approach
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to find the number
    // of customers who will
    // get their desired flavor of ice cream
    public static int NumberOfCustomer(
        int[] customer, int[] icecream)
    {
 
        // Count array stores the count
        // of preference of the customer
        int count[] = { 0, 0 };
        int n = customer.length;
        int k;
        for (int a : customer) {
            count[a]++;
        }
 
        for (k = 0;
             k < n && count[icecream[k]] > 0;
             ++k) {
            count[icecream[k]]--;
        }
 
        // Return k as the final answer
        return k;
    }
 
    public static void main(String[] args)
    {
        int customer[] = { 1, 1, 0, 0 };
        int icecream[] = { 0, 1, 0, 1 };
 
        int ans = NumberOfCustomer(
            customer, icecream);
 
        // Print the final answer
        System.out.print(ans);
    }
}


Python3
# Python3 program for above approach
 
# Function to find the number
# of customers who will
# get their desired flavor of ice cream
def NumberOfCustomer(customer, icecream) :
     
    # Count array stores the count
    # of preference of the customer
    count = [ 0, 0 ];
    n = len(customer);
 
    for a in customer :
        count[a] += 1;
     
    k = 0
    while k < n and count[icecream[k]] > 0 :
        count[icecream[k]] -= 1;
         
        k += 1;
 
    # Return k as the final answer
    return k;
 
if __name__ == "__main__" :
     
    customer = [1, 1, 0, 0];
    icecream = [0, 1, 0, 1];
    ans = NumberOfCustomer(customer, icecream);
     
    print(ans);
     
    # This code is contributed by AnkThon


C#
// C# program for above approach
using System;
class GFG {
 
    // Function to find the number
    // of customers who will
    // get their desired flavor of ice cream
    public static int NumberOfCustomer( int[] customer, int[] icecream)
    {
 
        // Count array stores the count
        // of preference of the customer
        int[] count = { 0, 0 };
        int n = customer.Length;
        int k;
        foreach(int a in customer) {
            count[a]++;
        }
 
        for (k = 0;
             k < n && count[icecream[k]] > 0;
             ++k) {
            count[icecream[k]]--;
        }
 
        // Return k as the final answer
        return k;
    }
 
    public static void Main()
    {
        int[] customer = { 1, 1, 0, 0 };
        int[] icecream = { 0, 1, 0, 1 };
 
        int ans = NumberOfCustomer( customer, icecream);
 
        // Print the final answer
        Console.Write(ans);
    }
}
 
// This code is contributed by gfgking


Javascript


输出
4

时间复杂度: O(N)

辅助空间: O(N)

方法 2:上述方法可以进一步优化,并且可以避免额外的 O(N) 空间。请按照以下步骤解决给定的问题。

  • 存储队列的顺序是没有用的。
  • 声明一个数组,比如count[] ,它将跟踪客户的偏好。
  • 如果customer[i]0则增加count[0] ,否则增加count[1]
  • 现在,初始化k ,它将存储将获得所需冰淇淋包的顾客数量。
  • 遍历冰淇淋阵列并检查左侧顾客中是否有人会吃东西。
  • 最后打印k作为最终答案。

C++

// c++ program for above approach
#include 
using namespace std;
 
// Function to find the number
// of customers who will
// get their desired flavor of ice cream
int NumberOfCustomer(int customer[], int icecream[], int n)
{
 
    // Count array stores the count
    // of preference of the customer
    int count[] = { 0, 0 };
 
    int k = 0;
    for (int a = 0; a < n; a++) {
        count[customer[a]]++;
    }
 
    for (k = 0; k < n && count[icecream[k]] > 0; ++k) {
        count[icecream[k]]--;
    }
 
    // Return k as the final answer
    return k;
}
 
int main()
{
    int customer[] = { 1, 1, 0, 0 };
    int icecream[] = { 0, 1, 0, 1 };
    int n = sizeof(customer) / sizeof(customer[0]);
 
    int ans = NumberOfCustomer(customer, icecream, n);
 
    // Print the final answer
    cout << (ans);
    return 0;
}
 
// This code is contributed by ukasp.

Java

// Java program for above approach
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to find the number
    // of customers who will
    // get their desired flavor of ice cream
    public static int NumberOfCustomer(
        int[] customer, int[] icecream)
    {
 
        // Count array stores the count
        // of preference of the customer
        int count[] = { 0, 0 };
        int n = customer.length;
        int k;
        for (int a : customer) {
            count[a]++;
        }
 
        for (k = 0;
             k < n && count[icecream[k]] > 0;
             ++k) {
            count[icecream[k]]--;
        }
 
        // Return k as the final answer
        return k;
    }
 
    public static void main(String[] args)
    {
        int customer[] = { 1, 1, 0, 0 };
        int icecream[] = { 0, 1, 0, 1 };
 
        int ans = NumberOfCustomer(
            customer, icecream);
 
        // Print the final answer
        System.out.print(ans);
    }
}

Python3

# Python3 program for above approach
 
# Function to find the number
# of customers who will
# get their desired flavor of ice cream
def NumberOfCustomer(customer, icecream) :
     
    # Count array stores the count
    # of preference of the customer
    count = [ 0, 0 ];
    n = len(customer);
 
    for a in customer :
        count[a] += 1;
     
    k = 0
    while k < n and count[icecream[k]] > 0 :
        count[icecream[k]] -= 1;
         
        k += 1;
 
    # Return k as the final answer
    return k;
 
if __name__ == "__main__" :
     
    customer = [1, 1, 0, 0];
    icecream = [0, 1, 0, 1];
    ans = NumberOfCustomer(customer, icecream);
     
    print(ans);
     
    # This code is contributed by AnkThon

C#

// C# program for above approach
using System;
class GFG {
 
    // Function to find the number
    // of customers who will
    // get their desired flavor of ice cream
    public static int NumberOfCustomer( int[] customer, int[] icecream)
    {
 
        // Count array stores the count
        // of preference of the customer
        int[] count = { 0, 0 };
        int n = customer.Length;
        int k;
        foreach(int a in customer) {
            count[a]++;
        }
 
        for (k = 0;
             k < n && count[icecream[k]] > 0;
             ++k) {
            count[icecream[k]]--;
        }
 
        // Return k as the final answer
        return k;
    }
 
    public static void Main()
    {
        int[] customer = { 1, 1, 0, 0 };
        int[] icecream = { 0, 1, 0, 1 };
 
        int ans = NumberOfCustomer( customer, icecream);
 
        // Print the final answer
        Console.Write(ans);
    }
}
 
// This code is contributed by gfgking

Javascript


输出
4

时间复杂度: O(N)

辅助空间: O(1)