📜  检查是否可以用不同的注释为客户队列服务

📅  最后修改于: 2021-04-25 01:08:13             🧑  作者: Mango

给定票证的成本’25’和一个整数数组’arr’,该数组保存队列中人们的纸币的值(“ 25”,“ 50”或“ 100”卢比)。
任务是确定是否有可能从0卢比开始向人们出售门票。

例子:

Input: arr = {25, 25, 50, 50}
Output: YES
You can give the 25 you received from the 1st customer 
to the 3rd customer and then the 25 from the 2nd customer to the 4th.

Input: arr = {25, 100}
Output: NO
It is not possible to return the change to the 2nd customer.

方法:跟踪卢比数。 25和卢比。 50个音符,我们目前分别为’c25’和’c50’。无需跟踪Rs的数量。 100张笔记,因为我们无法将它们退还给任何客户。现在有3种可能性:

  • 如果客户支付卢比。 25:增量c25,什么都不必退还给客户。
  • 如果客户支付卢比。 50:卢比。必须将25退还给客户,检查c25>0然后递增c50并递减c25。
  • 如果客户支付卢比。 100:卢比。 75必须退还给客户。有两种方法可以使用一个Rs。 50和一卢比。 25音符或使用三个Rs。 25个音符。我们将首选第一种方式,以便将来有Rs的人。 50来了,我们还有25来了。检查是否有可能,并相应地减少计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns
// true is selling of
// the tickets is possible
bool isSellingPossible(int n, int a[])
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++) {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50) {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0) {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Program to
// test above function
int main()
{
    int a[] = { 25, 25, 50, 100 };
    int n = sizeof(a) / sizeof(a[0]);
  
    if (isSellingPossible(n, a)) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}


Java
// Java implementation of the approach
class GFG
{
// Function that returns
// true is selling of
// the tickets is possible
static boolean isSellingPossible(int n,
                                 int a[])
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++)
    {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50)
        {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else
        {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0) 
            {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Code
public static void main(String []args)
{
    int a[] = { 25, 25, 50, 100 };
    int n = a.length;
  
    if (isSellingPossible(n, a)) 
    {
        System.out.println("YES");
    }
    else 
    {
        System.out.println("NO");
    }
}
}
  
// This code is contributed
// by ihritik


Python3
# Python3 implementation of the approach
  
# Function that returns true is selling
# of the tickets is possible
def isSellingPossible(n, a):
  
    c25 = 0;
    c50 = 0;
    i = 0;
    while(i < n): 
      
  
        # Nothing to return to the customer
        if (a[i] == 25):
            c25 += 1;
        elif (a[i] == 50): 
          
            c50 += 1;
  
            # Check if 25 can be returned 
            # to customer.
            if (c25 == 0):
                break;
            c25 -= 1;
          
        else:
      
            # Try returning one
            # 50 and one 25
            if (c50 > 0 and c25 > 0):
              
                c50 -= 1;
                c25 -= 1;
              
            # Try returning three 25
            elif (c25 >= 3):
                c25 -= 3;
            else:
                break;
        i += 1;
      
    # If the loop did not break,
    # all the tickets were sold
    if (i == n):
        return True;
    else:
        return False;
  
# Driver Code
a = [ 25, 25, 50, 100 ];
n = len(a);
  
if (isSellingPossible(n, a)):
    print("YES");
else:
    print("NO");
  
# This code is contributed by mits


C#
// C# implementation of the approach
using System;
  
class GFG
{
// Function that returns
// true is selling of
// the tickets is possible
static bool isSellingPossible(int n, int []a)
{
    int i, c25 = 0, c50 = 0;
    for (i = 0; i < n; i++) 
    {
  
        // Nothing to return
        // to the customer
        if (a[i] == 25)
            c25++;
        else if (a[i] == 50)
        {
            c50++;
  
            // Check if 25 can be
            // returned to customer.
            if (c25 == 0)
                break;
            c25--;
        }
        else 
        {
  
            // Try returning one
            // 50 and one 25
            if (c50 > 0 && c25 > 0)
            {
                c50--;
                c25--;
            }
  
            // Try returning three 25
            else if (c25 >= 3)
                c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if (i == n)
        return true;
    else
        return false;
}
  
// Driver Code
public static void Main()
{
    int []a = { 25, 25, 50, 100 };
    int n = a.Length;
  
    if (isSellingPossible(n, a))
    {
        Console.WriteLine("YES");
    }
    else 
    {
        Console.WriteLine("NO");
    }
}
}
  
// This code is contributed
// by ihritik


PHP
 0 && $c25 > 0)
            {
                $c50--;
                $c25--;
            }
  
            // Try returning three 25
            else if ($c25 >= 3)
                $c25 -= 3;
            else
                break;
        }
    }
  
    // If the loop did not break,
    // all the tickets were sold
    if ($i == $n)
        return true;
    else
        return false;
}
  
// Driver Code
$a = array( 25, 25, 50, 100 );
$n = sizeof($a);
  
if (isSellingPossible($n, $a))
{
    echo "YES";
}
else
{
    echo "NO";
}
  
// This code is contributed
// by ihritik
?>


输出:
YES

时间复杂度: O(N)