给定票证的成本“25”和一个整数数组“arr”,它保存队列中人们所拥有的笔记的值(“25”、“50”或“100”卢比笔记)。
任务是找出是否可以按顺序向人们出售门票,从 0 Rs 开始。
例子:
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.
方法:跟踪 Rs 的数量。 25 和卢比。 50 注意到我们目前分别有 ‘c25’ 和 ‘c50’。无需跟踪 Rs 的数量。 100 张纸币,因为我们无法将它们退还给任何客户。现在有3种可能:
- 如果客户支付卢比。 25:增加c25,什么都不需要返还给客户。
- 如果客户支付卢比。 50:卢比25 必须返回给客户,检查 c25>0 是否增加 c50 并减少 c25。
- 如果客户支付卢比。 100:卢比75 必须退还给客户。有两种方法可以做到,要么使用一个卢比。 50 和 1 卢比。 25 笔记或使用三个卢比。 25 条笔记。我们更喜欢第一种方式,以便将来有人拥有卢比。 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
?>
Javascript
输出:
YES
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。