
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if it is possible to serve customer queue with different notes in Python
Suppose we have an array called notes represents different rupee notes holding by customers in a queue. They are all waiting to buy tickets worth Rs 50. Here possible notes are [50, 100 and 200]. We have to check whether we can sell tickets to the people in order or not, initially we have Rs 0 in our hand.
So, if the input is like notes = [50, 50, 100, 100], then the output will be True for first two we do not need to return anything, but now we have two Rs 50 notes. So for last two we can return them Rs 50 notes and sell all tickets in order.
To solve this, we will follow these steps −
- freq := an empty map
- i := 0
- while i < size of notes, do
- if notes[i] is same as 50, then
- freq[50] := freq[50] + 1
- otherwise when notes[i] is same as 100, then
- freq[100] := freq[100] + 1
- if freq[50] is 0, then
- come out from loop
- freq[50] := freq[50] - 1
- otherwise,
- if freq[100] > 0 and freq[50] > 0, then
- freq[100] := freq[100] - 1
- freq[50] := freq[50] - 1
- otherwise when freq[50] >= 3, then
- freq[50] := freq[50] - 3
- otherwise,
- come out from loop
- if freq[100] > 0 and freq[50] > 0, then
- i := i + 1
- if notes[i] is same as 50, then
- if i is same as size of notes , then
- return True
- return False
Example
Let us see the following implementation to get better understanding −
from collections import defaultdict def solve(notes): freq = defaultdict(int) i = 0 while i < len(notes): if notes[i] == 50: freq[50] += 1 elif notes[i] == 100: freq[100] += 1 if freq[50] == 0: break freq[50] -= 1 else: if freq[100] > 0 and freq[50] > 0: freq[100] -= 1 freq[50] -= 1 elif freq[50] >= 3: freq[50] -= 3 else: break i += 1 if i == len(notes): return True return False notes = [50, 50, 100, 100] print(solve(notes))
Input
[50, 50, 100, 100]
Output
True
- Related Articles
- Check if it is possible to survive on Island in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to transform one string to another in Python
- Check if it possible to partition in k subarrays with equal sum in Python
- Check if it is possible to convert one string into another with given constraints in Python
- Check if moves in a stack or queue are possible or nots in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths in Python
- Check if it is possible to move from (0, 0) to (x, y) in N steps in Python
- Check if Queue Elements are pairwise consecutive in Python

Advertisements